|
| 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