Test Overview
This test opens a webpage with a dropdown menu, selects an option by visible text using Selenium's Select class, and verifies the correct option is selected.
This test opens a webpage with a dropdown menu, selects an option by visible text using Selenium's Select class, and verifies the correct option is selected.
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.Assert; import org.junit.After; import org.junit.Before; import org.junit.Test; public class DropdownSelectTest { WebDriver driver; @Before public void setUp() { // System property for ChromeDriver should be set before initializing ChromeDriver // System.setProperty("webdriver.chrome.driver", "path/to/chromedriver"); driver = new ChromeDriver(); driver.get("https://example.com/dropdown"); } @Test public void testSelectDropdownOption() { WebElement dropdownElement = driver.findElement(By.id("dropdownMenu")); Select dropdown = new Select(dropdownElement); dropdown.selectByVisibleText("Option 2"); WebElement selectedOption = dropdown.getFirstSelectedOption(); String selectedText = selectedOption.getText(); Assert.assertEquals("Option 2", selectedText); } @After 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 | Page title or URL confirms correct page | PASS |
| 3 | Finds dropdown element by id 'dropdownMenu' | Dropdown element is located on the page | Element is present and visible | PASS |
| 4 | Creates Select object for the dropdown element | Select class instance ready to interact with dropdown | - | PASS |
| 5 | Selects option with visible text 'Option 2' | Dropdown option 'Option 2' is selected | - | PASS |
| 6 | Gets the first selected option text | Selected option text retrieved as 'Option 2' | Assert that selected option text equals 'Option 2' | PASS |
| 7 | Test ends and browser closes | Browser window closed | - | PASS |