Friday, May 8, 2015

Lesson #11 - Selenium WebDriver - Web Browser Elements (Checkbox) with C#


Overview

In this article I want to review the “Check-box” web elements that you will encounter no matter what is the tested software, during this article I will explain the different properties and methods that you can use to control and manipulate these element.

Checkbox

Definition: A checkbox is a graphical control element that permits the user to make binary choice between one of two possible mutually exclusive options. For example, the user may have to answer 'yes' (checked) or 'no' (not checked) on a simple yes/no question (Reference: Wiki)


Web Page that I Want to test:


 


Tasks:
Let’s assume that we have a new application with multiple checkbox fields, our task is to validate that each box is available for selection and Deselection.

Now, I can accomplish this task with more than 1K flows, but in this article, I really want to make things more complicated so you will see more methods and code snippets that you can use, when you encounter same scenarios. 


Few facts:

  • There is SIX unique checkboxes.
  • Each checkbox is unique.
  • The Checkboxes are mapped as described in this table:

Checkbox Name
Locator: by “XPath”
Green
driver.FindElement(By.XPath("//label[@for='checkbox1']"));
Orange
driver.FindElement(By.XPath("//label[@for='checkbox2']"));
Red
driver.FindElement(By.XPath("//label[@for='checkbox3']"));
Gray
driver.FindElement(By.XPath("//label[@for='checkbox4']"));
Blue
driver.FindElement(By.XPath("//label[@for='checkbox5']"));
Cyan
driver.FindElement(By.XPath("//label[@for='checkbox6']"));


Code:
//Create a new Web element

IWebElement WebElement;
 
//Create a new Firefox Driver

IWebDriver Firefox = new FirefoxDriver(); 

public void CheckBoxSelectionExample()
{

//Log-In to csscheckbox site :

Firefox.Navigate().GoToUrl("http://www.csscheckbox.com/");

//Maximize the page:

Firefox.Manage().Window.Maximize();

//Detailed list of the available Checkboxes(Initialized in Dictionary)

Dictionary<string, string> CheckboxMapp = new Dictionary<string, string>();
CheckboxMapp.Add("Green", @"//label[@for='checkbox1']");
CheckboxMapp.Add("Orange", @"//label[@for='checkbox2']");
CheckboxMapp.Add("Red", @"//label[@for='checkbox3']");
CheckboxMapp.Add("Gray", @"//label[@for='checkbox4']");
CheckboxMapp.Add("Blue", @"//label[@for='checkbox5']");
CheckboxMapp.Add("Cyan", @"//label[@for='checkbox6']");

string[] SelectedList = new string[] {"Green", "Red","Blue"};

SelectSpecificCheckbox(Firefox, WebElement, CheckboxMapp, SelectedList);
}

private void SelectSpecificCheckbox
(IWebDriver Firefox, IWebElement WebElement, Dictionary<string, string> Checkboxlist, string[] SelectionList)
{

//Task 1:Unselect all Checkboxes (All selected by default!)

ValidateAllcheckbox(Firefox, WebElement,Checkboxlist,1);

string CheckBoxXPath;
foreach (var item in SelectionList)
{
if ((Checkboxlist.TryGetValue(item, out CheckBoxXPath) == true))
{
WebElement = Firefox.FindElement(By.XPath(CheckBoxXPath));

if (WebElement.Enabled == true)
{
WebElement.Click(); //select it
}}}}



private static void ValidateAllcheckbox
(IWebDriver Firefox, IWebElement WebElement, Dictionary<string, string> Checkboxlist ,int Number_Of_Loops)
{

/*
Remember that the Default values, is that all Checkboxes are selected (Default)
First Iteration: Will "uncheck" all Checkboxes,
Second Iteration: Will "Check" all Checkboxes,
Done.
*/

for (int i = 0; i < Number_Of_Loops; i++)
{
foreach (var CheckBox in Checkboxlist)
{
WebElement = Firefox.FindElement(By.XPath(CheckBox.Value.ToString()));
WebElement.Click();
}}



No comments:

Post a Comment

My Presentations