|
| 1 | +package com.baeldung.selenium.datepicker; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.*; |
| 4 | + |
| 5 | +import java.time.Duration; |
| 6 | +import org.junit.jupiter.api.AfterEach; |
| 7 | +import org.junit.jupiter.api.BeforeEach; |
| 8 | +import org.junit.jupiter.api.Test; |
| 9 | +import org.openqa.selenium.By; |
| 10 | +import org.openqa.selenium.WebDriver; |
| 11 | +import org.openqa.selenium.WebElement; |
| 12 | +import org.openqa.selenium.chrome.ChromeDriver; |
| 13 | +import org.openqa.selenium.support.ui.Select; |
| 14 | +import org.openqa.selenium.support.ui.Wait; |
| 15 | +import org.openqa.selenium.support.ui.FluentWait; |
| 16 | +import io.github.bonigarcia.wdm.WebDriverManager; |
| 17 | + |
| 18 | +public class SeleniumDatePickerLiveTest { |
| 19 | + |
| 20 | + private WebDriver driver; |
| 21 | + |
| 22 | + private static final String URL = "https://demoqa.com/automation-practice-form"; |
| 23 | + private static final String INPUT_XPATH = "//input[@id='dateOfBirthInput']"; |
| 24 | + private static final String INPUT_TYPE = "text"; |
| 25 | + private static final String INPUT_MONTH_XPATH = "//div[@class='react-datepicker__header']" |
| 26 | + + "//select[@class='react-datepicker__month-select']"; |
| 27 | + private static final String INPUT_YEAR_XPATH = "//div[@class='react-datepicker__header']" |
| 28 | + + "//select[@class='react-datepicker__year-select']"; |
| 29 | + private static final String INPUT_DAY_XPATH = "//div[contains(@class,\"react-datepicker__day\") and " |
| 30 | + + "contains(@aria-label,\"December\") and text()=\"2\"]"; |
| 31 | + |
| 32 | + @BeforeEach |
| 33 | + public void setUp() { |
| 34 | + WebDriverManager.chromedriver().setup(); |
| 35 | + driver = new ChromeDriver(); |
| 36 | + } |
| 37 | + |
| 38 | + @AfterEach |
| 39 | + public void tearDown() { |
| 40 | + driver.quit(); |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + public void givenDemoQAPage_whenFoundDateInput_thenContainsText() { |
| 45 | + driver.get(URL); |
| 46 | + WebElement inputElement = driver.findElement(By.xpath(INPUT_XPATH)); |
| 47 | + assertEquals("", inputElement.getText()); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + public void givenDemoQAPage_whenFoundDateInput_thenHasAttributeType() { |
| 52 | + driver.get(URL); |
| 53 | + WebElement inputElement = driver.findElement(By.xpath(INPUT_XPATH)); |
| 54 | + assertEquals(INPUT_TYPE, inputElement.getAttribute("type")); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + public void givenDemoQAPage_whenSelectDate_thenHasCorrectDate() { |
| 59 | + driver.get(URL); |
| 60 | + WebElement inputElement = driver.findElement(By.xpath(INPUT_XPATH)); |
| 61 | + inputElement.click(); |
| 62 | + |
| 63 | + // Select Year |
| 64 | + Wait<WebDriver> wait = new FluentWait(driver); |
| 65 | + WebElement yearElement = driver.findElement(By.xpath(INPUT_YEAR_XPATH)); |
| 66 | + wait.until(d -> yearElement.isDisplayed()); |
| 67 | + Select selectYear = new Select(yearElement); |
| 68 | + selectYear.selectByVisibleText("2024"); |
| 69 | + |
| 70 | + // Select Month |
| 71 | + WebElement monthElement = driver.findElement(By.xpath(INPUT_MONTH_XPATH)); |
| 72 | + wait.until(d -> monthElement.isDisplayed()); |
| 73 | + Select selectMonth = new Select(monthElement); |
| 74 | + selectMonth.selectByVisibleText("December"); |
| 75 | + |
| 76 | + // Select Day |
| 77 | + WebElement dayElement = driver.findElement(By.xpath(INPUT_DAY_XPATH)); |
| 78 | + wait.until(d -> dayElement.isDisplayed()); |
| 79 | + dayElement.click(); |
| 80 | + |
| 81 | + // Check selected date value |
| 82 | + assertEquals("02 Dec 2024", inputElement.getAttribute("value"), "Wrong Date Selected"); |
| 83 | + } |
| 84 | + |
| 85 | +} |
0 commit comments