Reading Time: 1 minute

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; example class presented below:

import org.openqa.selenium.WebElement;

public class PageObject {

public String[] occupantNoOptionExpectedArray = {"1", "2", "3", "4", "5+"};

@FindBy(xpath = "//select[@id='numberofoccupants']")
public WebElement noOfOccupantsDropdown;


2. In the test you will create a Select object (class like in the below example:

import java.util.List;

import org.openqa.selenium.WebElement;
import org.testng.Assert;
import org.testng.annotations.Test;

public class OccupantPageTest {

PageObject pageObject = new PageObject();
public void test_Occupants_Drop_Down_Values() throws Exception{
Select selectBedroom = new Select(pageObject.noOfOccupantsDropdown);
List<WebElement> optionSelect = selectBedroom.getOptions();

for (int i = 0; i < optionSelect.size(); i++){
Assert.assertEquals(optionSelect.get(i).getText(), pageObject.occupantNoOptionExpectedArray[i], "Values in the drop down for number of occupants are wrong"  );

That’s it. Simple. Enjoy.

Thanks for reading. I hope you learned something interesting about React!
If you found this article useful, please share it with others. Don’t forget to subscribe to get notified for the upcoming articles.