Module 15 - Calendar_MakeMyTrip Example. | Selenium Forum
M
Posted on 27/08/2016
Hi team,
The below code is not able to set the date in makemytrip calender, but no error in console. Please help me at the earliest.

public static void setDate(String StartDate) throws ParseException
{

SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
Date dateToBeSelected = df.parse(StartDate);
Date currentDate = new Date();
String monthYearDisplayed=driver.findElement(By.xpath(".//*[@id='ui-datepicker-div']/div[1]/div/div")).getText();
System.out.println(monthYearDisplayed);
String month = new SimpleDateFormat("MMMM").format(dateToBeSelected);
String year = new SimpleDateFormat("yyyy").format(dateToBeSelected);
String day = new SimpleDateFormat("d").format(dateToBeSelected);
String monthYearToBeSelected=month+" "+year;
System.out.println(monthYearToBeSelected);

while(true){
if(monthYearToBeSelected.equals(monthYearDisplayed)){
//select date
driver.findElement(By.xpath("//a[text()='"+day+"']")).click();;
System.out.println("Found and selected");
break;

}else{ // navigate to correct month and year

if(dateToBeSelected.after(currentDate)){
driver.findElement(By.xpath(".//*[@id='ui-datepicker-div']/div[1]/div/a")).click();
}else{
driver.findElement(By.xpath(".//*[@id='ui-datepicker-div']/div[2]/div/a")).click();
}

}

monthYearDisplayed=driver.findElement(By.xpath("//*[@id='ui-datepicker-div']/div[2]/div/div")).getText();
}
}

M
Replied on 29/08/2016

[code:2mtqk03i]package com.soapuitutorial.propertie;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Select_date {
static WebDriver driver;

public static void main(String[] args) throws ParseException {
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.manage().window().maximize();
driver.manage().timeouts().pageLoadTimeout(20, TimeUnit.SECONDS);
driver.get("https://www.makemytrip.com");
selectDate("25/07/2016");
}



public static void selectDate(String date) throws ParseException {

driver.findElement(By.id("start_date_sec")).click();

SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
Date dateToBeSelected = df.parse(date);
Date currentDate = new Date();
String monthYearDisplayed = driver.findElement(
By.xpath("//*[@id='ui-datepicker-div']/div[1]/div/div"))
.getText();
System.out.println("month year displayed " + monthYearDisplayed);
String month = new SimpleDateFormat("MMMM").format(dateToBeSelected);
String year = new SimpleDateFormat("yyyy").format(dateToBeSelected);
String day = new SimpleDateFormat("d").format(dateToBeSelected);
System.out.println("day is " + day);
String monthYearToBeSelected = month + " " + year;
System.out
.println("month year to be selected " + monthYearToBeSelected);

while (true) {
if (monthYearToBeSelected.equals(monthYearDisplayed)) {

driver.findElement(By.xpath("//td/a[text()=" + day + "]"))
.click();
System.out.println("found and selected");
break;

} else {
if (dateToBeSelected.after(currentDate)) {

driver.findElement(
By.xpath(".//*[@id='ui-datepicker-div']/div[2]/div/a"))
.click();
} else {
driver.findElement(
By.xpath(".//*[@id='ui-datepicker-div']/div[1]/div/a"))
.click();
}
}
monthYearDisplayed = driver.findElement(
By.xpath("//*[@id='ui-datepicker-div']/div[1]/div/div"))
.getText();
}
}

}
[/code:2mtqk03i]