0% found this document useful (0 votes)
62 views4 pages

Datepicker

This Java code defines a TestFlights class to test flight functionality on a travel website. It sets up a ChromeDriver, navigates to the site's home page, clicks the flights link, and selects a date from the date picker. JavaScript is used to select the desired year, month, and date by parsing the date elements and clicking the appropriate options. The test closes the driver after date selection.

Uploaded by

Kabilan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views4 pages

Datepicker

This Java code defines a TestFlights class to test flight functionality on a travel website. It sets up a ChromeDriver, navigates to the site's home page, clicks the flights link, and selects a date from the date picker. JavaScript is used to select the desired year, month, and date by parsing the date elements and clicking the appropriate options. The test closes the driver after date selection.

Uploaded by

Kabilan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

import java.util.

List;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;

public class TestFlights {

private static WebDriver driver;


private static String baseUrl = "https://www.phptravels.net/home";

public static WebDriver setup(String browser){

if(browser.equals("Chrome")){

System.setProperty("webdriver.chrome.driver",
".\\chromedriver.exe");
driver = new ChromeDriver();
}
return driver;
}

// F L I G H T S
public static WebElement HomePage_Flights(WebDriver driver){

return driver.findElement(By.xpath("//a[@data-name = 'flights']"));


}

public static void tearDown(){


driver.close();
}

public static void main(String[] args){

setup("Chrome");

driver.navigate().to(baseUrl);
driver.manage().window().maximize();

HomePage_Flights(driver).click();

Actions actions = new Actions(driver);

actions.moveToElement(driver.findElement(By.id("FlightsDateStart")));
actions.click();
actions.build().perform();

// D A T E P I C K E R
//Thread.sleep(10000);

int myMonth = 5;
int myDay = 2;
int myYear = 2710;
JavascriptExecutor jsexecutor = (JavascriptExecutor)driver;

WebElement calendarTitle =
driver.findElement(By.xpath("//div[8]//nav[1]//div[2]"));

calendarTitle.click();

/*********************************************************************/

WebElement currentYear = driver.findElement(


By.xpath("//div[8]//nav[1]//div[2]"));

if(Integer.parseInt(currentYear.getText()) != myYear){

jsexecutor.executeScript("arguments[0].click();", currentYear);

boolean quit = false;

while(!quit){

WebElement yearRange =
driver.findElement(By.xpath("//div[8]//nav[1]//div[2]"));
int yearStart =
Integer.parseInt(yearRange.getText().substring(0,4));
int yearEnd =
Integer.parseInt(yearRange.getText().substring(7));

if((myYear >= yearStart) && (myYear <= yearEnd)){

List<WebElement> activeYears = driver.findElements(


By.xpath("//div[@class = 'datepicker--
cell datepicker--cell-year']"));
for(WebElement year: activeYears){

if(Integer.parseInt(year.getAttribute("data-
year")) == myYear){

jsexecutor.executeScript("arguments[0].click();", year);
quit = true;
}

}
}else{
WebElement nextDecade =
driver.findElement(By.xpath("//div[8]//nav[1]//div[3]"));
jsexecutor.executeScript("arguments[0].click();",
nextDecade);
}
}

/*********************************************************************/
try {
WebElement currentMonth = driver.findElement(
By.xpath("//div[@class = 'datepicker--cell
datepicker--cell-month -current-']"));
if((Integer.parseInt(currentMonth.getAttribute("data-month"))+1)
> myMonth){

System.out.println("Inavlid month Entry!");

}else if ((Integer.parseInt(currentMonth.getAttribute("data-
month"))+1) != myMonth) {

WebElement month =
driver.findElement(By.xpath("//div[@class = 'datepicker--cell datepicker--cell-
month' and "
+ "@data-month = '" + (myMonth-1) + "']"));

jsexecutor.executeScript("arguments[0].click();", month);

}else{
jsexecutor.executeScript("arguments[0].click();",
currentMonth);
}
} catch (NoSuchElementException e) {
// TODO: handle exception
WebElement month = driver.findElement(By.xpath("//div[@class =
'datepicker--cell datepicker--cell-month' and "
+ "@data-month = '" + (myMonth-1) + "']"));

jsexecutor.executeScript("arguments[0].click();", month);
}
/*********************************************************************/

/*********************************************************************/

try {
WebElement currentDate = driver.findElement(
By.xpath("//div[8]//div[1]//div[1]//div[2]"
+ "//div[@class = 'datepicker--cell
datepicker--cell-day -current-']"));

if(Integer.parseInt(currentDate.getAttribute("data-date")) >
myDay){

System.out.println("Inavlid Date Entry!");

}else if ((Integer.parseInt(currentDate.getAttribute("data-
date"))) != myDay) {

WebElement date =
driver.findElement(By.xpath("//div[8]//div[1]//div[1]//div[2]//div["
+ "@data-date = '" + myDay + "' "
+ "and @data-month = '" + (myMonth-
1)+ "']"));

jsexecutor.executeScript("arguments[0].click();", date);

}else{
jsexecutor.executeScript("arguments[0].click();",
currentDate);
}
} catch (NoSuchElementException e) {
// TODO: handle exception

WebElement date =
driver.findElement(By.xpath("//div[8]//div[1]//div[1]//div[2]//div["
+ "@data-date = '" + myDay + "' "
+ "and @data-month = '" + (myMonth-1)+
"']"));

jsexecutor.executeScript("arguments[0].click();", date);

You might also like