5.2. Drop Down

Inspect

<select id="dropdown">
    <option value="" disabled="disabled" selected="selected">Please select an option</option>
    <option value="1">Option 1</option>
    <option value="2">Option 2</option>
</select>

Java

public class DropDownTest {
    WebDriver driver;
    @BeforeClass
    void setUp(){
        driver = new ChromeDriver();
        driver.get("https://the-internet.herokuapp.com/dropdown");

    }

    @Test
    void option1ShouldSelected(){
        WebElement select = driver.findElement(By.id("dropdown"));
        Select dropdown = new Select(select);

        dropdown.selectByVisibleText("Option 1"); // text()

        Assert.assertTrue(driver.findElement(By.xpath("//select[@id='dropdown']/option[.='Option 1']")).isSelected());// text()
        Assert.assertTrue(driver.findElement(By.xpath("//select[@id='dropdown']/option[text()='Option 1']")).isSelected());// text()
    }

    @AfterClass
    void tearDown(){
        driver.quit();
    }
}

Last updated

Was this helpful?