Test Overview
This test opens a webpage with a dropdown menu and selects options using three different methods: by value, by visible text, and by index. It verifies that the correct option is selected each time.
This test opens a webpage with a dropdown menu and selects options using three different methods: by value, by visible text, and by index. It verifies that the correct option is selected each time.
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 org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; public class DropdownSelectTest { WebDriver driver; @BeforeEach public void setUp() { driver = new ChromeDriver(); driver.get("https://example.com/dropdown"); } @Test public void testSelectByValueVisibleTextIndex() { WebElement dropdownElement = driver.findElement(By.id("dropdownMenu")); Select dropdown = new Select(dropdownElement); // Select by value dropdown.selectByValue("option2"); assertEquals("Option 2", dropdown.getFirstSelectedOption().getText()); // Select by visible text dropdown.selectByVisibleText("Option 3"); assertEquals("Option 3", dropdown.getFirstSelectedOption().getText()); // Select by index dropdown.selectByIndex(1); // index starts at 0 assertEquals("Option 2", dropdown.getFirstSelectedOption().getText()); } @AfterEach public void tearDown() { driver.quit(); } }
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts and Chrome browser opens | Browser window is open, ready to navigate | — | PASS |
| 2 | Navigates to https://example.com/dropdown | Page with dropdown menu loaded | — | PASS |
| 3 | Finds dropdown element by id 'dropdownMenu' | Dropdown element is located on the page | — | PASS |
| 4 | Selects option with value 'option2' | Dropdown option 'Option 2' is selected | Verify selected option text is 'Option 2' | PASS |
| 5 | Selects option with visible text 'Option 3' | Dropdown option 'Option 3' is selected | Verify selected option text is 'Option 3' | PASS |
| 6 | Selects option by index 1 | Dropdown option at index 1 ('Option 2') is selected | Verify selected option text is 'Option 2' | PASS |
| 7 | Test ends and browser closes | Browser window closed | — | PASS |
Select class allows you to select an option from a dropdown by the exact text shown to the user?Select class provides methods to select dropdown options by different criteria: visible text, value attribute, or index.selectByVisibleText(String text) selects the option matching the exact text shown to the user.Select class?Select starts at 0, so the third option has index 2.selectByIndex(2) selects the third option correctly.<select id="colors"> <option value="r">Red</option> <option value="g">Green</option> <option value="b">Blue</option> </select>
Select select = new Select(driver.findElement(By.id("colors")));
select.selectByValue("g");Green.selectByValue("g") selects the option whose value attribute matches "g" exactly.Select select = new Select(driver.findElement(By.id("dropdown")));
select.selectByIndex(5);selectByIndex(5) throws NoSuchElementException because the index is out of range.<option value="1">Apple</option> <option value="2">Banana</option> <option value="3">Cherry</option> <option value="4">Date</option>
selectByIndex(2) selects the third option (index starts at 0).selectByValue("3") selects option with value "3".selectByVisibleText("Cherry") selects option with visible text "Cherry".Pass the test and this pattern moves unknown