package ff;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;
public class DatePicker {
@Test
public void drop() throws InterruptedException
{
System.setProperty("webdriver.chrome.driver", "D:\\chromedriver.exe");
WebDriver driver=new ChromeDriver();
//driver.manage().window().maximize();
driver.get("https://www.tripadvisor.in/");
//click on date fields
driver.findElement(By.id("date_picker_in_1")).click();
//date from user which he wants to select
String date = "15-October 2016";
//Split date and month and years
String splitter[]= date.split("-");
//split month and years from date
String month_year = splitter[1];
//split Day from date
String Day_Date = splitter[0];
//get month name
String[] monthName = month_year.split(" ");
System.out.println("Month from User provided date :"+monthName[0]);
//Month name from user's provied date
String UserMonthName =monthName[0];
//Convert month name into month number
int UserMonthNumber=ObtenerNumeroMes(UserMonthName);
System.out.println("User provided Month Number :"+UserMonthNumber);
//String day = splitter[0];
/*System.out.println(splitter[1]);
System.out.println(splitter[0]);*/
//define Month and year object for date picker hearder
List<WebElement> monthobj=driver.findElements(By.xpath("//div[@class='calendar']/div[2][@class='month']/table/thead/tr[1]/th[@class='caption']"));
//total number of month display there
int sizeOfmonth = monthobj.size();
for(int i=0;i<sizeOfmonth;i++)
{
//display the month text display in the field
System.out.println(driver.findElements(By.xpath("//div[@class='calendar']/div[2][@class='month']/table/thead/tr[1]/th[@class='caption']")).get(i).getText());
//get month from application calander fields
String getMonthFromApp = driver.findElements(By.xpath("//div[@class='calendar']/div[2][@class='month']/table/thead/tr[1]/th[@class='caption']")).get(i).getText();
//split mmonth from years by space
String Systemsplitter[] = getMonthFromApp.split(" ");
//get month from
String x=Systemsplitter[0];
System.out.println(x);
//month in calander
int ApplicationMonthNumber=ObtenerNumeroMes(x);
System.out.println("Month Number :"+ApplicationMonthNumber);
//month in expected date
//if expected Monyh is greater than application current Month
if(UserMonthNumber>ApplicationMonthNumber)
{
int m = UserMonthNumber-ApplicationMonthNumber;
int t=m;
for(int j=1;j<=t;j++)
{
System.out.println("HI");
driver.findElement(By.xpath("//*[@class='navCal']/span[2]")).click();
Thread.sleep(1000);
}
}
//if expected Month is equal to application current Month
else if(UserMonthNumber==ApplicationMonthNumber)
{
System.out.println("February");
}
else
//if expected Monyh is less than application current Month the number of time click on calender
{
int m = 12-ApplicationMonthNumber;
for(int j=0;j<=m;j++)
{
System.out.println("HI");
driver.findElement(By.xpath("//*[@class='navCal']/span[2]")).click();
Thread.sleep(2000);
}
}
//select the day from that calender
List<WebElement> datac=driver.findElements(By.xpath("//div[@class='calendar']/div[@class='month']/table/thead/tr/th[@class='caption']"));
int cx=datac.size();
for(int k=0;k<=cx+1;k++)
{
System.out.println("dhdeidhei");
String msg=driver.findElements(By.xpath("//div[@class='calendar']/div[@class='month']/table/thead/tr/th[@class='caption']")).get(k).getText();
System.out.println(msg);
//selecting month
if(datac.get(k).getText().equals(month_year))
{System.out.println("-----------------------------------------------");
List<WebElement> days = driver.findElements(By.xpath("//div[@class='calendar']/div[@class='month']["+(i+1)+"]/table/tbody/tr/td/div"));
for(WebElement d:days)
{
System.out.println(d.getText());
if(d.getText().equals(Day_Date))
{
d.click();
}
}
}
}
}
}
//convert name of month into number of month
public int ObtenerNumeroMes(String NombreMes){
int NumeroMes;
switch(NombreMes) {
case ("January") :
NumeroMes = 1;
return NumeroMes;
case ("February") :
NumeroMes = 2;
return NumeroMes;
case ("March") :
NumeroMes = 3;
return NumeroMes;
case ("April") :
NumeroMes = 4;
return NumeroMes;
case ("May") :
NumeroMes = 5;
return NumeroMes;
case ("June") :
NumeroMes = 6;
return NumeroMes;
case ("July") :
NumeroMes = 7;
return NumeroMes;
case ("August") :
NumeroMes = 8;
return NumeroMes;
case ("September") :
NumeroMes = 9;
return NumeroMes;
case ("October") :
NumeroMes = 10;
return NumeroMes;
case ("November") :
NumeroMes = 11;
return NumeroMes;
case ("December") :
NumeroMes = 11;
return NumeroMes;
default:
System.out.println("Error");
return 0;
}
}
}
Screenshot:
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;
public class DatePicker {
@Test
public void drop() throws InterruptedException
{
System.setProperty("webdriver.chrome.driver", "D:\\chromedriver.exe");
WebDriver driver=new ChromeDriver();
//driver.manage().window().maximize();
driver.get("https://www.tripadvisor.in/");
//click on date fields
driver.findElement(By.id("date_picker_in_1")).click();
//date from user which he wants to select
String date = "15-October 2016";
//Split date and month and years
String splitter[]= date.split("-");
//split month and years from date
String month_year = splitter[1];
//split Day from date
String Day_Date = splitter[0];
//get month name
String[] monthName = month_year.split(" ");
System.out.println("Month from User provided date :"+monthName[0]);
//Month name from user's provied date
String UserMonthName =monthName[0];
//Convert month name into month number
int UserMonthNumber=ObtenerNumeroMes(UserMonthName);
System.out.println("User provided Month Number :"+UserMonthNumber);
//String day = splitter[0];
/*System.out.println(splitter[1]);
System.out.println(splitter[0]);*/
//define Month and year object for date picker hearder
List<WebElement> monthobj=driver.findElements(By.xpath("//div[@class='calendar']/div[2][@class='month']/table/thead/tr[1]/th[@class='caption']"));
//total number of month display there
int sizeOfmonth = monthobj.size();
for(int i=0;i<sizeOfmonth;i++)
{
//display the month text display in the field
System.out.println(driver.findElements(By.xpath("//div[@class='calendar']/div[2][@class='month']/table/thead/tr[1]/th[@class='caption']")).get(i).getText());
//get month from application calander fields
String getMonthFromApp = driver.findElements(By.xpath("//div[@class='calendar']/div[2][@class='month']/table/thead/tr[1]/th[@class='caption']")).get(i).getText();
//split mmonth from years by space
String Systemsplitter[] = getMonthFromApp.split(" ");
//get month from
String x=Systemsplitter[0];
System.out.println(x);
//month in calander
int ApplicationMonthNumber=ObtenerNumeroMes(x);
System.out.println("Month Number :"+ApplicationMonthNumber);
//month in expected date
//if expected Monyh is greater than application current Month
if(UserMonthNumber>ApplicationMonthNumber)
{
int m = UserMonthNumber-ApplicationMonthNumber;
int t=m;
for(int j=1;j<=t;j++)
{
System.out.println("HI");
driver.findElement(By.xpath("//*[@class='navCal']/span[2]")).click();
Thread.sleep(1000);
}
}
//if expected Month is equal to application current Month
else if(UserMonthNumber==ApplicationMonthNumber)
{
System.out.println("February");
}
else
//if expected Monyh is less than application current Month the number of time click on calender
{
int m = 12-ApplicationMonthNumber;
for(int j=0;j<=m;j++)
{
System.out.println("HI");
driver.findElement(By.xpath("//*[@class='navCal']/span[2]")).click();
Thread.sleep(2000);
}
}
//select the day from that calender
List<WebElement> datac=driver.findElements(By.xpath("//div[@class='calendar']/div[@class='month']/table/thead/tr/th[@class='caption']"));
int cx=datac.size();
for(int k=0;k<=cx+1;k++)
{
System.out.println("dhdeidhei");
String msg=driver.findElements(By.xpath("//div[@class='calendar']/div[@class='month']/table/thead/tr/th[@class='caption']")).get(k).getText();
System.out.println(msg);
//selecting month
if(datac.get(k).getText().equals(month_year))
{System.out.println("-----------------------------------------------");
List<WebElement> days = driver.findElements(By.xpath("//div[@class='calendar']/div[@class='month']["+(i+1)+"]/table/tbody/tr/td/div"));
for(WebElement d:days)
{
System.out.println(d.getText());
if(d.getText().equals(Day_Date))
{
d.click();
}
}
}
}
}
}
//convert name of month into number of month
public int ObtenerNumeroMes(String NombreMes){
int NumeroMes;
switch(NombreMes) {
case ("January") :
NumeroMes = 1;
return NumeroMes;
case ("February") :
NumeroMes = 2;
return NumeroMes;
case ("March") :
NumeroMes = 3;
return NumeroMes;
case ("April") :
NumeroMes = 4;
return NumeroMes;
case ("May") :
NumeroMes = 5;
return NumeroMes;
case ("June") :
NumeroMes = 6;
return NumeroMes;
case ("July") :
NumeroMes = 7;
return NumeroMes;
case ("August") :
NumeroMes = 8;
return NumeroMes;
case ("September") :
NumeroMes = 9;
return NumeroMes;
case ("October") :
NumeroMes = 10;
return NumeroMes;
case ("November") :
NumeroMes = 11;
return NumeroMes;
case ("December") :
NumeroMes = 11;
return NumeroMes;
default:
System.out.println("Error");
return 0;
}
}
}
Screenshot:
No comments:
Post a Comment