enthusiasticCoder
enthusiasticCoder

Reputation: 327

Automate date-picker based on month text

I am trying to automate date-picker on a page, and I want to hit the previous/next button until I reach a desired month in the calendar. But the button does not click, only once it clicks and test cases passes without going any further. Current month is September it clicks and goes to August and the test case passes without being clicked further, lets say if I wish to click till January it does not happen

@FindBy(css=".ui-datepicker-month") //month displayed in date-picker
WebElement labelCalendarMonth;

@FindBy(id="txtStartDate_calendarButton") // button to open the date-picker
WebElement searchStartDateButton;

@FindBy(xpath="//a[@title='Prev']") // previous button in date-picker
WebElement calendarSearchStartDatePreviousBtn;

public void selectStartMonth(WebElement ele, String value) {
  if(!labelCalendarMonth.getText().equals(value))
  {
    ele.click();
  }
}

public void clickCalendarSearchStartDatePreviousBtn() throws InterruptedException
{
    selectStartMonth(calendarSearchStartDatePreviousBtn, "January");
}

@Test()
public void testAddResourceSchedule() throws InterruptedException 
{
    resourceSchedulePage.clickCalendarSearchStartDatePreviousBtn();
}

Upvotes: 1

Views: 979

Answers (1)

cruisepandey
cruisepandey

Reputation: 29362

I have worked with https://www.booking.com/ and they do have date picker, so just to give you little head up how do we handle this situation where we have to click till certain amount of time, for e.g: till Jan in your case, we can do the following :-

It's kind of obvious that we have to have infinite while loop.

while(true){
    if(driver.findElement(By.xpath("month xpath which you wanna selct ")).getText().equalsIgnoreCase(month) && driver.findElement(By.xpath("year xpath that you wanna select")).getText().equalsIgnoreCase(year)) {
        break;
    }
    else {
        wait.until(ExpectedConditions.elementToBeClickable(By.xpath("next botton xpath here, which is on date picker."))).click();
        Thread.sleep(500);
    }
}

Basically, the thing is you need to define month and year in advance in a string format. before running this code.

I have written a good solution here as well where I am clicking till end of Nov-2022

Upvotes: 1

Related Questions