Test Overview
This test opens a web page with a date picker, selects a specific date using a calendar widget, and verifies the selected date is correctly displayed in the input field.
This test opens a web page with a date picker, selects a specific date using a calendar widget, and verifies the selected date is correctly displayed in the input field.
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.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; import java.time.Duration; import org.junit.Assert; import org.junit.After; import org.junit.Before; import org.junit.Test; public class DatePickerTest { WebDriver driver; WebDriverWait wait; @Before public void setUp() { driver = new ChromeDriver(); wait = new WebDriverWait(driver, Duration.ofSeconds(10)); } @Test public void testSelectDate() { driver.get("https://example.com/datepicker"); // Open the date picker widget WebElement dateInput = wait.until(ExpectedConditions.elementToBeClickable(By.id("date-input"))); dateInput.click(); // Wait for calendar to be visible wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("ui-datepicker-calendar"))); // Select the date 15 from the calendar WebElement dateToSelect = driver.findElement(By.xpath("//table[contains(@class,'ui-datepicker-calendar')]//a[text()='15']")); dateToSelect.click(); // Verify the input field has the selected date String selectedDate = dateInput.getAttribute("value"); Assert.assertEquals("15/06/2024", selectedDate); } @After public void tearDown() { if (driver != null) { 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/datepicker | Date picker page loads with input field visible | Page loaded and date input is present | PASS |
| 3 | Waits until date input field is clickable and clicks it | Date picker calendar widget appears on screen | Date picker calendar is visible | PASS |
| 4 | Finds the date '15' in the calendar and clicks it | Date '15' is selected in the calendar | - | PASS |
| 5 | Reads the value from the date input field | Input field shows '15/06/2024' | Assert that input value equals '15/06/2024' | PASS |
| 6 | Test ends and browser closes | Browser window is closed | - | PASS |