Checking options in the Select element

While testing the dropdowns and lists created with the <select> element, there will be a need to check to see that correct options are displayed for user selection. These options may be static or populated from a database via AJAX calls.

In this recipe, we will see how options can be checked against the expected values.

Getting ready

This recipe will need the test created from the earlier Automating dropdowns and lists recipe. We will add additional steps for checking the options.

How to do it...

Let's modify the testDropdown() test method for checking the options. Add the following highlighted code to the test:

@Test
public void testDropdown() {

  // Get the Dropdown as a Select using it's name attribute
  Select make = new Select(driver.findElement(By.name("make")));

  // Verify Dropdown does not support multiple selection
  assertFalse(make.isMultiple());
  // Verify Dropdown has four options for selection
  assertEquals(4, make.getOptions().size());

  // We will verify Dropdown has expected values as listed in a array
  List<String> expectedOptions = Arrays.asList("BMW", "Mercedes", "Audi",
      "Honda");
  List<String> actualOptions = new ArrayList<String>();

  // Retrieve the option values from Dropdown using getOptions() method
  for (WebElement option : make.getOptions()) {
    actualOptions.add(option.getText());
  }

  // Verify expected options array and actual options array match
  assertArrayEquals(expectedOptions.toArray(), actualOptions.toArray());

  // With Select class we can select an option in Dropdown using Visible
  // Text
  make.selectByVisibleText("Honda");
  assertEquals("Honda", make.getFirstSelectedOption().getText());

  // or we can select an option in Dropdown using value attribute
  make.selectByValue("audi");
  assertEquals("Audi", make.getFirstSelectedOption().getText());

  // or we can select an option in Dropdown using index
  make.selectByIndex(0);
  assertEquals("BMW", make.getFirstSelectedOption().getText());
}

How it works...

Checking options in a dropdown or list needs a slightly different approach, as there is no in-built method available in the Select class. In this approach, we create a list of expected values that we want to check in the dropdown or list, as shown in the following code:

List<String> expectedOptions = Arrays.asList("BMW", "Mercedes", "Audi","Honda");

The text labels for all the options will be retrieved in a similar list. For this, we will iterate through all the options using the getOptions() method of the Select class. The getOptions() method returns all the options as instances of the WebElement class in a list. Using the getText() method of the WebElement class, the text label of all the options will be added in the actualOptions array list:

List<String> act_options = new ArrayList<String>();

  //Retrieve the option values from Dropdown using getOptions() //method
  for(WebElement option : make.getOptions()) {
actualOptions.add(option.getText()); }

We will compare the expectedOptions list with actualOptions for any mismatch at the end:

assertArrayEquals(expectedOptions.toArray(),actualOptions.toArray());

There's more...

To check whether a specific option is available for selection, we can simply perform a check on the actualOptions array list in the following way:

assertTrue(actualOptions.contains("BMW"));

See also

  • The Automating dropdowns and lists recipe
  • The Checking selected options in dropdowns and lists recipe
..................Content has been hidden....................

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