Interacting with Dropdown and Lists During an Automation Script

The aim here is to create an automation script that interacts with dropdown and lists.

For the following example, we will assume that the dropdown element has been found by means of a combination of the Select method and its ID (we will go deeper into the findElement method in an upcoming section):

Select list = new Select(driver.findElement(By.
id("monthOfBirth")));

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. The steps for completion of this process are as follows:

  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:
package com.beginningselenium.selenium;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
  1. First, we will work with a single choice list. Locate the dropdown list of By.id "monthOfBirth":
Select singleChoiceList = new Select(driver.findElement(By.id("monthOfBirth")));
  1. If the dropdown is enabled and visible, verify that it does not allow multiple selections and that it contains 13 options (including "Choose…"):
if (!singleChoiceList.isMultiple() && singleChoiceList.
getOptions().size() == 13)
{
}
  1. Using the System.out.println method, display messages indicating whether the verification was successful or not:
System.out.println("The list does not accept multiple
choices and contains 13 options (including 'Choose...').");
  1. If the list does not allow multiple selection and its size is 13, select the option "February" by sending its value:
if (!singleChoiceList.isMultiple() && singleChoiceList.
getOptions().size() == 13)
{
singleChoiceList.selectByVisibleText("February");
}

You can also select an option by value:

singleChoiceList.selectByValue("february");

Or by index:

singleChoiceList.selectByIndex(2);
  1. Verify that "February" is selected as an option and use the System.out.println method, which displays messages indicating whether the verification was successful or not and the option chosen:
if (!singleChoiceList.isMultiple() && singleChoiceList.
getOptions().size() == 13)
{
singleChoiceList.selectByVisibleText("February");
if (singleChoiceList.getFirstSelectedOption().
getText().equalsIgnoreCase("February"))
{
} else {
}
}
  1. Now, we will work with a multiple-choice list. Locate the dropdown list of By.id "monthOfBirth":
Select multipleChoiceList = new Select(driver.
findElement(By.id("hobbies")));

  1. If the dropdown is enabled and visible, verify that it does allow for multiple selections and that it contains 4 options:
if (multipleChoiceList.isMultiple() && multipleChoiceList.
getOptions().size() == 4)
{
}
  1. Using the System.out.println method, display messages indicating whether the verification was successful or not.
System.out.println("The list does accept multiple choices
and contains 4 options.");
  1. If the list does allow multiple selection and its size is 4, select the different options by sending its values:
if (multipleChoiceList.isMultiple() && multipleChoiceList.
getOptions().size() == 4)
{
multipleChoiceList.selectByVisibleText("Reading");
multipleChoiceList.selectByVisibleText("Sports");
multipleChoiceList.selectByVisibleText("Traveling");
}
  1. Deselect an option using the value attribute:
multipleChoiceList.deSelectByValue("sports");

You can also deselect an option by its index:

multipleChoiceList.deselectByIndex(0);

Or by its visible text:

multipleChoiceList.deselectByVisibleText("Sports");
  1. Verify the number of choices selected and use the method to display messages indicating the number of options chosen:
if (multipleChoiceList.getAllSelectedOptions().size() == 2)
{
System.out.println("It worked, 2 options have been
chosen");
}
  1. Verify that the two options are actually selected:
List<String> expectedSelection = Arrays.asList("Reading",
"Traveling");
List<String> actualSelection = new ArrayList<String>();
for (WebElement element : multipleChoiceList.
getAllSelectedOptions()) {
actualSelection.add(element.getText());
}
if (actualSelection.containsAll(expectedSelection)) {
} else {
}
  1. Compile and run the script.
..................Content has been hidden....................

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