Datepickers are actually sluggish and tedious to work together with. As Kate Paulk and lauda recommend, it might be price attempting to bypass this by simply filling textual content into the sector as an alternative of utilizing the interface when you’re not testing the datepicker itself.
If you really want to check the datepicker I might recommend a easy loop that figures out which course it wants to maneuver in based mostly on the present month. With out seeing the precise datepicker you are attempting to automate, that is only a imprecise suggestion somewhat than one thing you’ll be able to simply copy and paste in. This shall be extremely sluggish and will solely ever be used sparingly.
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import java.time.LocalDate;
import java.time.Month;
public class DatePicker {
personal WebDriver driver;
public DatePicker(WebDriver driver) {
this.driver = driver;
}
public void selectDate(LocalDate date) throws InterruptedException {
boolean monthFound = false;
int maxAttempts = 100;
int makes an attempt = 0;
int currentlySelectedMonth;
int currentlySelectedYear;
whereas (!monthFound && makes an attempt++ < maxAttempts) {
currentlySelectedMonth = getCurrentlySelectedMonth();
currentlySelectedYear = getCurrentYear();
// Be certain that we transfer to the right yr first
if(currentlySelectedYear != date.getYear()) {
if(currentlySelectedYear > date.getYear()) {
clickPreviousMonth();
} else {
clickNextMonth();
}
proceed;
}
if (currentlySelectedMonth == date.getMonthValue()) {
clickDay(date.getDayOfMonth());
break;
} else {
// Which course do we have to transfer?
if (currentlySelectedMonth > date.getMonthValue()) {
clickPreviousMonth();
} else {
clickNextMonth();
Thread.sleep(250); // Fast sleep to offer the UI an opportunity to replace, you most likely need one thing smarter
}
}
}
}
personal void clickNextMonth() {
driver.findElement(By.xpath("//a[@title="Next"]")).click on();
}
personal void clickPreviousMonth() {
driver.findElement(By.xpath("//a[@title="Prev"]")).click on();
}
personal int getCurrentlySelectedMonth() {
String monthName = driver.findElement(By.className("ui-datepicker-month")).getText();
return Month.valueOf(monthName.toUpperCase()).getValue();
}
personal int getCurrentYear() {
return Integer.valueOf(
driver.findElement(By.className("ui-datepicker-year")).getText()
);
}
personal void clickDay(int dayNumber) {
driver.findElement(By.linkText(String.valueOf(dayNumber))).click on();
}
}
and a check case from a publicly accessible demo of jquery datepicker:
import org.junit.jupiter.api.Take a look at;
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.assist.ui.ExpectedConditions;
import org.openqa.selenium.assist.ui.WebDriverWait;
import java.time.LocalDate;
public class QuickTest {
@Take a look at
public void datePickerTest() throws InterruptedException {
System.setProperty("webdriver.chrome.driver", System.getProperty("person.residence") + "/Downloads/chromedriver");
WebDriver driver = new ChromeDriver();
DatePicker datePicker = new DatePicker(driver);
driver.get("https://jqueryui.com/datepicker/#default");
WebElement body = driver.findElement(By.className("demo-frame"));
driver.switchTo().body(body);
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.till(ExpectedConditions.presenceOfElementLocated(By.id("datepicker")));
driver.findElement(By.id("datepicker")).click on();
datePicker.selectDate(LocalDate.now().plusMonths(24).minusDays(5));
}
}
The testcase will typically stale reference on finding the sector to click on, however that is one thing you’ll be able to simply repair on an actual implementation of this. Not price fixing for this demo.
Seems it isn’t that sluggish, however nonetheless preferable to skip this when you’re simply wanting a date to be crammed out.