Saturday, March 23, 2013


To browse through the contents in a child tag under a <div>.  Find the following HTML code and now write a Selenium WebDriver- C# program to check all the CheckBoxe's under a <div> tag container.
<div class="facetContainerDiv">
    <div>
        <label class="facetLabel">
            <input class="facetCheck" type="checkbox" />
        </label>
        <label class="facetLabel">
            <input class="facetCheck" type="checkbox" />
        </label>
        <label class="facetLabel">
            <input class="facetCheck" type="checkbox" />
        </label>
        <label class="facetLabel">
            <input class="facetCheck" type="checkbox" />
        </label>
        <label class="facetLabel">
            <input class="facetCheck" type="checkbox" />
        </label>
    </div>
</div>

Steps to follow when writing a selenium program for this condition.

  1. Create an interface of the web element for the div under div with class as facetContainerDiv
ie for
<div class="facetContainerDiv">
    <div>

    </div>
</div>
2. Create an IList with all the elements inside the second div i.e for,
<label class="facetLabel">
   <input class="facetCheck" type="checkbox" />
</label>
<label class="facetLabel">
   <input class="facetCheck" type="checkbox" />
</label>
<label class="facetLabel">
   <input class="facetCheck" type="checkbox" />
</label>
<label class="facetLabel">
   <input class="facetCheck" type="checkbox" />
</label>
<label class="facetLabel">
   <input class="facetCheck" type="checkbox" />
</label>
3. Access each check boxes using the index
Please find the code below
using System;
using System.Collections.Generic;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Support.UI;

namespace SeleniumTests
{
  class ChechBoxClickWthIndex
    {
        static void Main(string[] args)
        {

            IWebDriver driver = new FirefoxDriver();

            driver.Navigate().GoToUrl("file:///C:/Users/chery/Desktop/CheckBox.html");

            // Create an interface WebElement of the div under div with **class as facetContainerDiv**
            IWebElement WebElement =    driver.FindElement(By.XPath("//div[@class='facetContainerDiv']/div"));
            // Create an IList and intialize it with all the elements of div under div with **class as facetContainerDiv**
            IList<IWebElement> AllCheckBoxes = WebElement.FindElements(By.XPath("//label/input"));
            // Get the count of check boxes 
            int RowCount = WebElement.FindElements(By.XPath("//label/input")).Count;
            for (int i = 0; i < RowCount; i++)
            {
            // Check the check boxes based on index
               AllCheckBoxes[i].Click();

            }
            Console.WriteLine(RowCount);
            Console.ReadLine(); 

        }
    }
}



No comments:

Post a Comment