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

In this article, I want to review the “Check-box” element and the different methods selenium provides to handle it.


Definition: A checkbox is a graphical control element that permits the user to make a 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).


In the next example, i will use the following site:

https://mdbootstrap.com/docs/standard/forms/checkbox/



Example 1: Validate the status of a checkbox (Checked/Unchecked)

in the next example we will validate rather a checkbox is selected or not and print its status to the console. To validate the checkbox status we will use the "element.Selected" Property.


Steps:

  1. validate the status of "Default" checkbox (Return False).

  2. Click on the checkbox.

  3. validate the status of "Default" checkbhox (Return True).

Code:


class Program

{

public int MyProperty { get; set; }

static void Main(string[] args)

{


string checkboxElement = "flexCheckDefault";

IWebDriver firefoxDriver = new FirefoxDriver();

Backlog.TestPreperationGeneralSites(firefoxDriver, "https://mdbootstrap.com/docs/standard/forms/checkbox/");

Backlog.ValidateCheckboxStatus(firefoxDriver, checkboxElement);

}

}


static public class Backlog

{

static IWebElement searchFieldtextBox;

static IWebElement searchFieldButton;

static IWebElement checkBoxToValidate;


internal static void TestPreperationForMachTested(IWebDriver webDriver)

{

webDriver.Navigate().GoToUrl("http://www.machtested.com/");

Thread.Sleep(15000);

webDriver.FindElement(By.Id("fclose-button")).Click();//Approve FaceBook Bunner

}


internal static void ValidateCheckboxStatus(IWebDriver driver, string checkbox)

{

checkBoxToValidate = driver.FindElement(By.Id(checkbox));

Console.ForegroundColor = ConsoleColor.Red;


if (checkBoxToValidate.Selected == true)

{

Console.WriteLine("Checkbox Status of test #1: Checkbox is selected");

}

else

{

Console.WriteLine("Checkbox Status of test #1: Checkbox is unselected");

}


checkBoxToValidate.Click();


Console.ForegroundColor = ConsoleColor.Green;

if (checkBoxToValidate.Selected == true)

{

Console.WriteLine("Checkbox Status of test #2: Checkbox is selected");

}

else

{

Console.WriteLine("Checkbox Status of test #2: Checkbox is unselected");

}

}


internal static void TestPreperationForAgileQuality(IWebDriver webDriver)

{

webDriver.Navigate().GoToUrl("http://www.agilequalitymadeeasy.com/");

Thread.Sleep(15000);

webDriver.FindElement(By.XPath("/html/body/div[2]/div/div[2]/button[2]")).Click();

}

internal static void TestPreperationGeneralSites(IWebDriver webDriver, string siteName)

{

webDriver.Navigate().GoToUrl(siteName);

Thread.Sleep(3000);

webDriver.Manage().Window.Maximize();

}

internal static void LocateSearchFieldInBlog(IWebDriver driver, string elementLocation, string textToAdd)

{

try

{

searchFieldtextBox = driver.FindElement(By.Name(elementLocation));

searchFieldtextBox.SendKeys(textToAdd); //Add Text

searchFieldtextBox.Clear();//Clear Text

searchFieldtextBox.SendKeys(textToAdd + "Re-Adding test"); //Re-add test

Console.WriteLine(searchFieldtextBox.GetAttribute("value"));//Get Text in the field

}

catch (NoSuchElementException)

{

Console.WriteLine("Element is not found");

}

}

internal static void LocateButton(IWebDriver driver, string elementLocation)

{

try

{

searchFieldButton = driver.FindElement(By.ClassName(elementLocation));

searchFieldButton.Click();

}

catch (NoSuchElementException)

{

Console.WriteLine("Element is not found");

}

}

}


Result


Example 2: Validate the status of a checkbox (Checked/Unchecked)

In this example, i will use the chckbox attribute insted of the "Selceted" proporty to validate the status of the checkbox. As you can see in the example below, when a checkbox is selected we will see the "checked" attribute wich we can use to ensure the selection status of the element.


Code

Here is the small change i made in the "ValidateCheckboxStatus" methoed:


internal static void ValidateCheckboxStatus(IWebDriver driver, string checkbox)

{

checkBoxToValidate = driver.FindElement(By.Id(checkbox));

Console.ForegroundColor = ConsoleColor.Red;


if (checkBoxToValidate.GetAttribute("checked") == "true")

{

Console.WriteLine("Checkbox Status of test #1: Checkbox is selected");

}

else

{

Console.WriteLine("Checkbox Status of test #1: Checkbox is unselected");

}


checkBoxToValidate.Click();


Console.ForegroundColor = ConsoleColor.Green;

if (checkBoxToValidate.GetAttribute("checked") == "true")

{

Console.WriteLine("Checkbox Status of test #2: Checkbox is selected");

}

else

{

Console.WriteLine("Checkbox Status of test #2: Checkbox is unselected");

}


Result



40 views0 comments