Friday, September 13, 2019

Post#48.Handling DatePicker in Selenium


How to Select Date from Date Picker in Selenium WebDriver

In this, we’ll learn how to handle date time picker controls like jQuery date picker and Kendo date picker using Selenium WebDriver.
I will use JQuery Date picker for example and you can take any example because approach will remain same.

Below image is an example of Date picker which is JQuery widget

Steps to follow:
Step 1- Click on calendar
Step 2 – Get the month and year from the Calander by getText() method.
Step 3- Compare the month & Year with given month & Year (Here I want to Select 15-January-2020)
Step 4- If month & Year does not matches then click on next button and repeat Step no 3
Step 5- If month & Year matches then select date

package pack4;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class DatePickerEx {

            public static void main(String[] args) throws InterruptedException {
                       
                        String expDate = "15-April-2020";
                        String[] dateArray = expDate.split("-");
                        String eDay = dateArray[0];
                        String eMonth = dateArray[1];
                        String eYear = dateArray[2];
                       
                        System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir")+"\\browserDrivers\\chromedriver.exe");
                        ChromeDriver driver = new  ChromeDriver();
                       
                        driver.get("http://jqueryui.com/resources/demos/datepicker/other-months.html");
                        driver.manage().window().maximize();
                        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
                        driver.findElement(By.id("datepicker")).click();
                       
                        String actYear = driver.findElement(By.xpath("//span[@class='ui-datepicker-year']")).getText();
                        String actMonth = driver.findElement(By.xpath("//span[@class='ui-datepicker-month']")).getText();
                       
                        WebElement nextBtn = driver.findElement(By.xpath("//span[text()='Next']"));
                       
                        while(!(eYear.equals(actYear) && eMonth.equalsIgnoreCase(actMonth))) {
                                    nextBtn.click();
                                    Thread.sleep(2000);
                                    //Re-initializing the elements
                                    actYear = driver.findElement(By.xpath("//span[@class='ui-datepicker-year']")).getText();
                                    actMonth = driver.findElement(By.xpath("//span[@class='ui-datepicker-month']")).getText();
                                    nextBtn = driver.findElement(By.xpath("//span[text()='Next']"));
                        }
                       
                        List<WebElement> days = driver.findElements(By.xpath("//a[@class='ui-state-default']"));
                       
                        for(int i=0;i<days.size();i++) {
                                    if(days.get(i).getText().equals(eDay)) {
                                                days.get(i).click();
                                                break;
                                    }
                        }

            }

}


No comments:

Post a Comment