0
0
Selenium Javatesting~10 mins

Date picker strategies in Selenium Java - Test Execution Trace

Choose your learning style9 modes available
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.

Test Code
Selenium Java
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();
        }
    }
}
Execution Trace - 6 Steps
StepActionSystem StateAssertionResult
1Test starts and Chrome browser opensBrowser window is open, ready to navigate-PASS
2Navigates to https://example.com/datepickerDate picker page loads with input field visiblePage loaded and date input is presentPASS
3Waits until date input field is clickable and clicks itDate picker calendar widget appears on screenDate picker calendar is visiblePASS
4Finds the date '15' in the calendar and clicks itDate '15' is selected in the calendar-PASS
5Reads the value from the date input fieldInput field shows '15/06/2024'Assert that input value equals '15/06/2024'PASS
6Test ends and browser closesBrowser window is closed-PASS
Failure Scenario
Failing Condition: The date '15' is not found or clickable in the calendar widget
Execution Trace Quiz - 3 Questions
Test your understanding
What action opens the date picker calendar in the test?
ARefreshing the page
BClicking the date input field
CTyping the date manually
DClicking a submit button
Key Result
Always wait explicitly for the date picker calendar to be visible before interacting with date elements to avoid timing issues.