1

I am trying to create a method to click particular date, based on the month and year (I want to pass only date, month and year in the method as parameter).

Also I searched over internet but I didn't get the solution for my query.

Below are the my approach and my testing sample application url.

URL I used: http://www.cleartrip.com/

My Approach:

public static WebElement selectDatefromMultiDate(WebDriver driver, String date, String year,String month) throws IOException, InterruptedException {
          driver.findElement(By.id("DepartDate")).click();
            WebElement dates = driver.findElement(By.className("ui-state-default"));
            List<WebElement> day=dates.findElements(By.tagName("a"));

            String  calMonth = driver.findElement(By.className("ui-datepicker-month")).getText();
              System.out.println(calMonth);
            String calYear = driver.findElement(By.className("ui-datepicker-year")).getText();
            System.out.println(calYear);
            for (WebElement cell: day){
                if(calYear.equalsIgnoreCase(year)){
                     if (calMonth.equalsIgnoreCase(month)){
                           if (cell.getText().equals(date)){  
                               cell.findElement(By.linkText(date)).click();
                           }
                     }
                               break;
            }

     }
            return element;
}
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
forumqaa
  • 121
  • 8

2 Answers2

0

You don't actually need to manipulate the date picker directly. You can type in the date string directly into the input field. It just has to be in the correct format, example:

WebElement dateInput = driver.findElement(By.id("DepartDate"));
dateInput.sendKeys("Sat, 6 Jun, 2015");

Now, you would need to create a date from year, month, day arguments passed in and format the date into the desired date string format using SimpleDateFormat.

Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • wow, Very nice answer. I used your sample and working fine. But in another case I want to select date by clicking the date (Do not want to enter date). Could you please let me know how to do that?. – forumqaa May 26 '15 at 20:47
  • @forumqaa thanks, is this going to be any date? The reason I ask is that I'm trying to identify whether going back and forth from year-to-year and month-to-month is going to be necessary or not. – alecxe May 26 '15 at 20:50
  • Yes, Year-to-year and month-to-month is necessary. I wanted to create method passing date, month and year as an arguments (Please see my method above), but I was unable to click on the particular date (Based on given date, month and year). And finally I will call this method in another class and pass the date, month and year in the method. – forumqaa May 27 '15 at 02:35
  • I can not upvote your answer because I do not have points, I will get these points soon and i will upvote. – forumqaa May 27 '15 at 02:40
  • Are you actually writing tests for the Date control itself. Otherwise, it is pragmatic to just set the date in the text field. You can covert the passed parameters into an acceptable string. – Dakshinamurthy Karra Aug 04 '15 at 07:53
0

This code might be of some help.

            try{
                      WebElement dateWidget = driver.findElement(By.xpath(OR.getProperty(object))); 
                      List<WebElement> rows = dateWidget.findElements(By.tagName("tr"));  
                      List<WebElement> columns = dateWidget.findElements(By.tagName("td"));  
                      for (WebElement cell: columns){
                           if (cell.getText().equals(data))
                              {
                                  cell.findElement(By.linkText(data)).click();
                      break; 
                      }
                  }
               }catch(Exception e){
        return Constants.KEYWORD_FAIL+" -- Not able to select the date"+e.getMessage(); 
}
Mohit Kanwar
  • 2,962
  • 7
  • 39
  • 59