Hello Friends,
In this post we will see how to select specific date in JQuery Datepicker.
Its very easy to enter date as text by .sendKeys() method, but mostly when application uses JQuery Datepicker, it disables typing on the text-box. the text-box sets to read only.
While automating such screens we need to write logic which can interact with Datepicker and selects desired date.
Consider below example of Datepicker.
Html code for above example is
To automate this, we first break apart a problem.
Lets implement the above logic.
Note: we have used Joda Date Time Library for calculate date and time difference
Hope this will help you.... you can directly use above code if your application is using JQuery Datepicker. Keep automating......
In this post we will see how to select specific date in JQuery Datepicker.
Its very easy to enter date as text by .sendKeys() method, but mostly when application uses JQuery Datepicker, it disables typing on the text-box. the text-box sets to read only.
While automating such screens we need to write logic which can interact with Datepicker and selects desired date.
Consider below example of Datepicker.
Html code for above example is
To automate this, we first break apart a problem.
- Decide which date we want to set
- Read the current date from datepicker
- Calculate difference between both dates in Months(difference will positive in case of set date is in future respective to current date & will negative when set date is in past respective to current date of datepicker)
- Set loop depends on month difference and navigate the datepicker to specific month
- Select the Day.
Lets implement the above logic.
Note: we have used Joda Date Time Library for calculate date and time difference
SimpleDateFormat myDateFormat = new SimpleDateFormat("dd/MM/yyyy"); SimpleDateFormat calDateFormat = new SimpleDateFormat("MMMM yyyy"); Date setDate=myDateFormat.parse("15/08/2014"); WebDriver driver = new FirefoxDriver(); driver.get("http://seleniumbyneeds.blogspot.in/2015/01/selecting-date-on-jquery-datepicker.html"); // Switch to frame where the Menu Example is loaded driver.switchTo().frame(driver.findElement(By.id("myFiddler"))).switchTo().frame(0); driver.findElement(By.id("datepicker")).click(); Date curDate = calDateFormat.parse(driver.findElement(By.className("ui-datepicker-title")).getText()); // Joda org.joda.time.Months class to calculate difference // to do this converted Date to joda DatTime int monthDiff = Months.monthsBetween(new DateTime(curDate).withDayOfMonth(1), new DateTime(setDate).withDayOfMonth(1)).getMonths(); boolean isFuture = true; // decided whether set date is in past or future if(monthDiff<0){ isFuture = false; monthDiff*=-1; } // iterate through month difference for(int i=1;i<=monthDiff;i++){ driver.findElement(By.className("ui-datepicker-" + (isFuture?"next":"prev"))).click(); } // Click on Day of Month from table driver.findElement(By.xpath("//table[@class='ui-datepicker-calendar']//a[text()='" + (new DateTime(setDate).getDayOfMonth()) + "']")).click();
Hope this will help you.... you can directly use above code if your application is using JQuery Datepicker. Keep automating......
No comments:
Post a Comment