Skip to content

Instantly share code, notes, and snippets.

@KrishnB
Last active January 17, 2019 06:26
Show Gist options
  • Save KrishnB/9ff92e242e4a3ced10b7860b4772fe99 to your computer and use it in GitHub Desktop.
Save KrishnB/9ff92e242e4a3ced10b7860b4772fe99 to your computer and use it in GitHub Desktop.
This code performs date selection in a calendar.
public static void calendarHandling(String date, WebDriver driver) {
// elements for clicking calendar buttons
String PrevButtonM = "//div[@id='ui-datepicker-div']/descendant::div[@class='header']/a[@title='Prev']";
String NextButtonM = "//div[@id='ui-datepicker-div']/descendant::div[@class='header']/a[@title='Next']";
String currentReturnedYear = "//div[@id='ui-datepicker-div']/descendant::div[@class='monthBlock first']//div[@class='title']/span[@class='ui-datepicker-year']";
String returnedMonth = "//div[@id='ui-datepicker-div']/descendant::div[@class='monthBlock first']//div[@class='title']/span[@class='ui-datepicker-month']";
date = date.trim();
String DateArr[] = date.split("/");
int Day = Integer.parseInt(DateArr[0]);
int month = Integer.parseInt(DateArr[1]);
int Year = Integer.parseInt(DateArr[2]);
HashMap<String, Integer> calendarMonths = new HashMap<String, Integer>();
calendarMonths.put("January", 1);
calendarMonths.put("February", 2);
calendarMonths.put("March", 3);
calendarMonths.put("April", 4);
calendarMonths.put("June", 6);
calendarMonths.put("July", 7);
calendarMonths.put("August", 8);
calendarMonths.put("September", 9);
calendarMonths.put("October", 10);
calendarMonths.put("November", 11);
calendarMonths.put("December", 12);
int retYear;
try {
System.out.println(Day + "-" + month + "-" + Year);
do {
// return the Year displaying on the application calendar
retYear = Integer.parseInt(driver.findElement(By.xpath(currentReturnedYear)).getText());
if (retYear < Year) {
//taking calendar to year provided by user
driver.findElement(By.xpath(NextButtonM)).click();
}
if (retYear > Year) {
//year less than current year displayed on calendar can be propagated
throw new RuntimeException("Year cannot be less than current year");
}
// If same checking for the date input by user and comparing with that of
// calendar display
int retmonthcount = 0;
if (retYear == Year) {
for (int i = 1; i <= 12; i++) {
// fetching current calendar month displayed
String returnedmonth = driver.findElement(By.xpath(returnedMonth)).getText();
if (calendarMonths.containsKey(returnedmonth))
retmonthcount = calendarMonths.get(returnedmonth);
// propagating to future months as per given input
if ((retmonthcount < month) && (driver.findElement(By.xpath(NextButtonM)).isDisplayed())) {
driver.findElement(By.xpath(NextButtonM)).click();
}
//propagating to earlier months as per provided input
if ((retmonthcount > month) && (driver.findElement(By.xpath(PrevButtonM))).isDisplayed()) {
driver.findElement(By.xpath(PrevButtonM)).click();
} else if (retmonthcount == month) {
try {
driver.findElement(By.xpath(
"//div[@id='ui-datepicker-div']/descendant::div[@class='monthBlock first']//a[text()='"
+ Day + "']"))
.click();
break;
} catch (Exception e) {
throw new Exception("You may have entered a wrong date");
}
}
}
}
} while (retYear != Year);
} catch (Throwable e) {
// e.printStackTrace();
System.out.println(e.getMessage());
// System.out.println("Invalid input Date as per Site Calendar");
// System.out.println());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment