Interacting with Checkboxes During an Automation Script

The aim is to create an automation script that interacts with checkboxes. For the following example, we will assume that the checkbox element has been found by means of the findElement method (we will go into more detail about this method in an upcoming section):

WebElement receiveEmails = driver.findElement(By.
id("emailUpdates"));

Open the https://trainingbypackt.github.io/Beginning-Selenium/lesson_3/exercise_3_1.html file and use IntelliJ IDEA for the creation of a Selenium script.

  1. Review and analyze the structure and behavior of the https://trainingbypackt.github.io/Beginning-Selenium/lesson_3/exercise_3_1.html file.
  2. Create a new Java file for the automation script. Make sure that you include the required libraries.
  3. Locate the checkbox "emailUpdates".
WebElement emailUpdates = driver.findElement(By.
id("emailUpdates"));
  1. Verify that the checkbox is enabled and visible:
if (emailUpdates.isEnabled() && emailUpdates.isDisplayed())
{

}
  1. Using the System.out.println method, display messages indicating whether the verification was successful or not.
  1. If the checkbox is enabled and visible, verify that it is not selected before clicking on it:
if (emailUpdates.isEnabled() && emailUpdates.isDisplayed())
{
if (!emailUpdates.isSelected())
{
emailUpdates.click();
}
}

We can deselect a checkbox by clicking on it after it has been selected.

  1. Verify that the checkbox is selected and use the System.out.println method to display messages, indicating whether the verification was successful or not and that the checkbox has been checked:
if (receiveEmails.isEnabled() && receiveEmails.
isDisplayed())
{
if (!emailUpdates.isSelected())
{
emailUpdates.click();
if (emailUpdates.isSelected())
System.out.println("Load of the test worked,
checkbox has been selected");
else
System.out.println("Something went wrong with
the test, checkbox has not been selected");
}
}
  1. Compile and run the script.

We now have a better understanding of how to interact with the elements of a web page.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.17.68.14