Key takeaways
- The Select class exposes getOptions() for real select elements.
- Custom dropdowns built from divs need normal element queries instead.
- Assert on the option text or value, not the count alone, which passes on the wrong list.
During UI test automation you will need to write tests that validate the options that are present in a select tag.
Getting all the options in the select tag is very simple using Selenium Webdriver and below is how it is done.
1. In the page object model of the page you create a WebElement that identifies the select tag. The constructor takes the driver and calls PageFactory.initElements, which is what fills in the @FindBy field; without it the field stays null and the test throws a NullPointerException:
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
public class PageObject {
public String[] occupantNoOptionExpectedArray = {"1", "2", "3", "4", "5+"};
@FindBy(xpath = "//select[@id='numberofoccupants']")
public WebElement noOfOccupantsDropdown;
public PageObject(WebDriver driver) {
PageFactory.initElements(driver, this);
}
}
2. In the test you will create a Select object (class org.openqa.selenium.support.ui.Select), read the option texts into a list and compare the whole list with the expected values. Comparing lists checks the count and the order in one assertion, so an extra or a missing option fails the test:
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
public class OccupantPageTest {
private WebDriver driver;
private PageObject pageObject;
@BeforeMethod
public void setUp() {
driver = new ChromeDriver();
driver.get("https://example.com/occupants"); // the page with the select tag
pageObject = new PageObject(driver);
}
@Test
public void test_Occupants_Drop_Down_Values() {
Select selectOccupants = new Select(pageObject.noOfOccupantsDropdown);
List<String> actual = selectOccupants.getOptions().stream()
.map(WebElement::getText)
.collect(Collectors.toList());
Assert.assertEquals(actual, Arrays.asList(pageObject.occupantNoOptionExpectedArray),
"Values in the drop down for number of occupants are wrong");
}
@AfterMethod
public void tearDown() {
driver.quit();
}
}
That’s it.
Frequently Asked Questions
How do I read every option from a dropdown?
Wrap the element in Selenium’s Select class and call getOptions(), which returns the option elements as a list. Read getText() from each one to compare against your expected values.
Does this work for custom dropdowns?
No. The Select class only works on a real HTML select element. A dropdown built from divs and list items is just ordinary markup, so you locate and read those elements the normal way.
What should the assertion check?
The text or the values, not only the count. A test that asserts five options pass happily when the list is five wrong options, which is exactly the regression you were trying to catch.