Tuesday, 23 June 2015

Selenium DatePicker Calendar

Selenium WebDriver DatePicker Calendar



This video will tell how to select date from DatePicker i.e. Calender using Selenium WebDriver.






import java.util.List;
import java.util.concurrent.TimeUnit;

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


public class datePicket {

static WebDriver driver;

public static void main(String[] args) throws InterruptedException {

driver = new FirefoxDriver();
driver.get("http://www.tripadvisor.in/");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);

driver.findElement(By.xpath("//*[@id='rdoFlights']/img")).click();
driver.findElement(By.xpath("//*[@id='metaFlightFrom']")).sendKeys("Mumbai");
driver.findElement(By.xpath("//*[@id='metaFlightTo']")).sendKeys("Delhi");
driver.findElement(By.xpath("//*[@id='checkIn']")).click();

String date = "10-July 2015";
String splitter[] = date.split("-");
String checkInmonth_year = splitter[1];
String checkInday = splitter[0];

selectDate(checkInmonth_year,checkInday);

driver.findElement(By.xpath("//*[@id='checkOut']")).click();
date = "15-July 2015";
splitter = date.split("-");
String checkOutmonth_year = splitter[1];
String checkOutday = splitter[0];

selectDate(checkOutmonth_year,checkOutday);

driver.findElement(By.xpath("//*[@id='SUBMIT_VRS']")).click();
}



public static void selectDate(String month_year, String day) throws InterruptedException{

List<WebElement> elements = driver.findElements(By.xpath
      ("//div[@class='calendar']/div[@class='month']/table/thead/tr/th[@class='caption']"));

for(int i=0; i<elements.size(); i++){
System.out.println(elements.get(i).getText());
//Selecting the month
if(elements.get(i).getText().equals(month_year)){
//Selecting the date
System.out.println("-------------");
List<WebElement> days= driver.findElements(By.xpath
                 ("//div[@class='calendar']/div[@class='month']["+(i+1)+"]/table/tbody/tr/td/a"));
for(WebElement d:days){
System.out.println(d.getText());
if(d.getText().equals(day)){
d.click();
return;
}
}
}
}

driver.findElement(By.xpath("//*[@id='overlayInnerDiv']/div/div[1]/span[2]/a")).click();
driver.findElement(By.xpath("//*[@id='overlayInnerDiv']/div/div[1]/span[2]/a")).click();
Thread.sleep(2000);
selectDate(month_year, day);

}

}

7 comments:

  1. Best place to learn Appium Android IOS Automation Testing Training in Chennai

    Go for Appium training in chennai for real time mobile automation testing training with real time project support.
    Mr. Vishwa is a best person who can teach and develop people in Appium and Selenium,Coded UI Automation Tools with real time scenarios.

    ReplyDelete
  2. Thanks for sharing this Information, Got to learn new things from your Blog on Appium.
    Appium training in chennai

    ReplyDelete
  3. Thanks for sharing this Information, Got to learn new things from your Blog on Appium.
    Talk to eight one two two two four one two eight six.
    Appium training in chennai

    ReplyDelete
  4. Hey guys, I joined with them for Appium training and going on wonderful.
    Feeling confident and sure to be placed well.
    Talk to eight one two two two four one two eight six.
    Appium training in chennai

    ReplyDelete
  5. We provide the Advance Level Appium training in Chennai with real time Scenarios. We are the best training institute in Chennai providing real time training. Here the trainers provide the practical classes with real time scenarios. call@ 8122241286
    Appium training in chennai

    ReplyDelete
  6. I joined with them for selenium training and its going on good.
    Hope I will be placed in a good company.
    They are cool team so far in chennai .
    Call them at 8122241286.
    www.thecreatingexperts.com
    selenium training in chennai

    ReplyDelete
  7. I strongly recommend them for Appium training in Chennai.
    Call at 8122241286.
    www.thecreatingexperts.com
    Appium training in chennai

    ReplyDelete