Skip to content

Commit f0bd37a

Browse files
authored
Pull request for code related to BAEL-7209 (#16983)
* BAEL-7209 commit with tests for using Selenium to selection options within a Select Dropbdown * BAEL-7209 minor changes to the tests --------- Co-authored-by: vshanbha <>
1 parent 49e5f1f commit f0bd37a

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package com.baeldung.selenium.select;
2+
3+
import static org.junit.jupiter.api.Assertions.*;
4+
5+
import java.util.List;
6+
7+
import org.junit.jupiter.api.AfterEach;
8+
import org.junit.jupiter.api.BeforeEach;
9+
import org.junit.jupiter.api.Test;
10+
import org.openqa.selenium.By;
11+
import org.openqa.selenium.Keys;
12+
import org.openqa.selenium.WebDriver;
13+
import org.openqa.selenium.WebElement;
14+
import org.openqa.selenium.support.ui.Select;
15+
import org.openqa.selenium.chrome.ChromeDriver;
16+
import io.github.bonigarcia.wdm.WebDriverManager;
17+
18+
public class SeleniumSelectLiveTest {
19+
20+
private WebDriver driver;
21+
22+
private static final String URL = "https://www.baeldung.com/contact";
23+
private static final String INPUT_XPATH = "//select[@name='question-recipient']";
24+
private static final String OPTION_XPATH = "//select[@name='question-recipient']/option[@value='Bug reporting']";
25+
private static final String OPTION_TEXT = "Bug reporting";
26+
private static final int OPTION_INDEX = 4;
27+
@BeforeEach
28+
public void setUp() {
29+
WebDriverManager.chromedriver().setup();
30+
driver = new ChromeDriver();
31+
}
32+
33+
@AfterEach
34+
public void tearDown() {
35+
driver.quit();
36+
}
37+
38+
@Test
39+
public void givenBaeldungContactPage_whenSelectQuestion_thenContainsOptionBugs() {
40+
driver.get(URL);
41+
WebElement inputElement = driver.findElement(By.xpath(OPTION_XPATH));
42+
assertEquals(OPTION_TEXT, inputElement.getText());
43+
}
44+
45+
@Test
46+
public void givenBaeldungContactPage_whenSelectQuestion_thenSelectOptionBugsByValue() {
47+
driver.get(URL);
48+
WebElement inputElement = driver.findElement(By.xpath(INPUT_XPATH));
49+
WebElement optionBug = driver.findElement(By.xpath(OPTION_XPATH));
50+
51+
Select select = new Select(inputElement);
52+
select.selectByValue(OPTION_TEXT);
53+
assertTrue(optionBug.isSelected());
54+
}
55+
56+
@Test
57+
public void givenBaeldungContactPage_whenSelectQuestion_thenSelectOptionBugsByText() {
58+
driver.get(URL);
59+
WebElement inputElement = driver.findElement(By.xpath(INPUT_XPATH));
60+
WebElement optionBug = driver.findElement(By.xpath(OPTION_XPATH));
61+
62+
Select select = new Select(inputElement);
63+
select.selectByVisibleText(OPTION_TEXT);
64+
assertTrue(optionBug.isSelected());
65+
}
66+
67+
@Test
68+
public void givenBaeldungContactPage_whenSelectQuestion_thenSelectOptionBugsByIndex() {
69+
driver.get(URL);
70+
WebElement inputElement = driver.findElement(By.xpath(INPUT_XPATH));
71+
WebElement optionBug = driver.findElement(By.xpath(OPTION_XPATH));
72+
73+
Select select = new Select(inputElement);
74+
select.selectByIndex(OPTION_INDEX);
75+
assertTrue(optionBug.isSelected());
76+
}
77+
78+
}

0 commit comments

Comments
 (0)