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 |