0
0
Selenium Javatesting~20 mins

Date picker strategies in Selenium Java - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Date Picker Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Selenium code selecting a date
What will be the output of this Selenium Java code snippet when selecting a date from a date picker widget?
Selenium Java
WebElement dateInput = driver.findElement(By.id("datePicker"));
dateInput.click();
WebElement dateToSelect = driver.findElement(By.xpath("//td[@data-date='2024-06-15']"));
dateToSelect.click();
String selectedDate = dateInput.getAttribute("value");
System.out.println(selectedDate);
A2024-06-15
Bnull
C2024/06/15
DThrows NoSuchElementException
Attempts:
2 left
💡 Hint
Check how the date is stored in the input field after selection.
assertion
intermediate
1:30remaining
Correct assertion to verify date selection
Which assertion correctly verifies that the date '2024-12-25' is selected in the date picker input field?
Selenium Java
WebElement dateInput = driver.findElement(By.id("datePicker"));
String selectedDate = dateInput.getAttribute("value");
AassertEquals(selectedDate, "2024-12-25");
BassertTrue(selectedDate.contains("12-25-2024"));
CassertNotNull(selectedDate);
DassertFalse(selectedDate.isEmpty());
Attempts:
2 left
💡 Hint
Check the exact expected date format in the input value.
locator
advanced
2:00remaining
Best locator for dynamic date picker day cell
Given a date picker where day cells have dynamic classes but a stable data-date attribute in 'YYYY-MM-DD' format, which locator is best to select the day '2024-11-05'?
ABy.cssSelector("td.day[data-date='2024-11-05']")
BBy.className("day-2024-11-05")
CBy.id("day-2024-11-05")
DBy.xpath("//td[@data-date='2024-11-05']")
Attempts:
2 left
💡 Hint
Focus on stable attributes that do not change dynamically.
🔧 Debug
advanced
2:30remaining
Debugging date picker selection failure
A test script fails to select a date in a date picker. The code is: WebElement dateInput = driver.findElement(By.id("datePicker")); dateInput.sendKeys("2024-07-20"); Why might this fail to select the date properly?
AThe element with id 'datePicker' is not interactable.
BThe date format '2024-07-20' is invalid for sendKeys.
CThe date picker requires clicking the calendar UI, sendKeys does not trigger selection events.
DsendKeys is deprecated for date inputs.
Attempts:
2 left
💡 Hint
Think about how date pickers update their internal state.
framework
expert
3:00remaining
Designing a reusable date picker selection method
You want to create a reusable Selenium Java method to select any date in a date picker widget that uses a calendar UI with data-date attributes. Which method signature and approach is best?
A
public void selectDate(String date) {
  driver.findElement(By.id("datePicker")).sendKeys(date);
}
B
public void selectDate(LocalDate date) {
  driver.findElement(By.id("datePicker")).click();
  String dateStr = date.toString();
  driver.findElement(By.xpath("//td[@data-date='" + dateStr + "']")).click();
}
C
public void selectDate(int year, int month, int day) {
  driver.findElement(By.id("datePicker")).click();
  driver.findElement(By.xpath("//td[@data-year='" + year + "' and @data-month='" + month + "' and text()='" + day + "']")).click();
}
D
public void selectDate(String date) {
  driver.findElement(By.cssSelector("input.date-picker")).clear();
  driver.findElement(By.cssSelector("input.date-picker")).sendKeys(date);
}
Attempts:
2 left
💡 Hint
Consider maintainability and triggering UI events properly.