In earlier recipes, we saw how to select options in the dropdown and list controls as well as how to check what options are available for selection. We also need to verify that the correct options are selected in these controls, either by default or by the user.
In this recipe, we will see how to check options that are selected in a dropdown or list.
This recipe will need the test created from the earlier Automating dropdowns and lists recipe. We will add additional steps for checking the options.
Let's modify the testDropdown()
test method for checking the options. Add the following highlighted code to the test:
@Test public void testDropdown() { ... //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()); }
Also, modify the testMultipleSelectList()
test method for checking the options. Add the following highlighted code to the test:
@Test public void testMultipleSelectList() { ... //Select multiple options in the list using visible text color.selectByVisibleText("Black"); color.selectByVisibleText("Red"); color.selectByVisibleText("Silver"); //We will verify list has multiple options selected as listed //in a array List<String> expectedSelection = Arrays.asList("Black", "Red", "Silver"); List<String> actualSelection = new ArrayList<String>(); for(WebElement option : color.getAllSelectedOptions()) {actualSelection.add(option.getText()); } //Verify expected array for selected options match with actual //options selected assertArrayEquals(expectedSelection.toArray(),actualSelection.toArray()); //Verify there 3 options selected in the list assertEquals(3,color.getAllSelectedOptions().size()); //Deselect an option using visible text color.deselectByVisibleText("Silver"); //Verify selected options count assertEquals(2,color.getAllSelectedOptions().size()); //Deselect an option using value attribute of the option color.deselectByValue("rd"); //Verify selected options count assertEquals(1,color.getAllSelectedOptions().size()); //Deselect an option using index of the option color.deselectByIndex(0); //Verify selected options count assertEquals(0,color.getAllSelectedOptions().size()); }
When the user selects an option from a dropdown or list that supports only single option selection, the selected option can be queried through the getFirstSelectedOption()
method of the Select
class. It returns the option as an instance of WebElement
. For example, in the Make
dropdown, we selected the Honda
option using the selectByVisible()
method. To check this selection, we can use the getFirstSelectedOption()
and the getText()
methods in the following way:
//With Select class we can select an option in Dropdown using Visible Text make.selectByVisibleText("Honda"); assertEquals("Honda", make.getFirstSelectedOption().getText());
To check selected options in a multi-select dropdown or list, we can use the getAllSelectedOptions()
method of the Select
class. It returns all the selected options as a list of WebElement
. In this test, we created a list of expected selected items and then retrieved the selected options in a list by iterating WebElement
, returned by getAllSelectedOptions()
:
//We will verify list has multiple options selected as listed in a //array List<String> expectedSelection = Arrays.asList("Black", "Red", "Silver"); List<String> actualSelection = new ArrayList<String>(); for(WebElement option : color.getAllSelectedOptions()) { actualSelection.add(option.getText()); }
Using the assertArrayEquals()
method of JUnit, we will compare both expectedSelection
and actualSelection
to check that correct options are selected in the list, as shown in the following code:
//Verify expected array for selected options match with actual //options selected assertArrayEquals(expectedSelection.toArray(),actualSelection.toArray());
We can also check the number of options selected in a list by querying the size from the getAllSelectedOptions()
method. For example, as shown in the following code, we selected three options in the list, so the getAllSelectedOptions().size()
method should return 3
:
assertEquals(3,color.getAllSelectedOptions().size());
3.15.237.123