Sunday, 25 January 2015

Selecting a date from DatePicker

Hi Friends, in this post I'm gonna explain you how to select a date from datepicker using selenium webdriver, well it sounds easy but it is not yet easy as you think because mostly datepicker has dynamically changing IDs so coding is not straight forward here.

Just visit the URL:http://demos.telerik.com/kendo-ui/datetimepicker/index and you will see the datepicker as shown below


Now suppose you want to select a date and time like: 8/8/2016 5.00 PM then follow the below code and you will be able to select that value..:)

Code:

package NewTabtest;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;




//Select date 8/August/2016 5.00 PM
public class DatePickerProgram {

   
    public static void main(String[] args) {
       
        try{
           
//Just Put value into variable and Print for the name sake
            String tobeenterdate = "8/8/2016 5:00 PM";
           
            System.out.println(tobeenterdate);
           
            WebDriver driver = new FirefoxDriver();
           
            driver.get("http://demos.telerik.com/kendo-ui/datetimepicker/index");
           
            driver.manage().window().maximize();


//Applying Explicit wait

            WebDriverWait wait = new WebDriverWait(driver, 300);
            wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//*[@id='to-do']/span/span/span/span[1]")));
           
            driver.findElement(By.xpath(".//*[@id='to-do']/span/span/span/span[1]")).click();
           
            Thread.sleep(2000);
           
            driver.findElement(By.xpath(" //a[text()='January 2015']")).click();
           
            Thread.sleep(3000);
           
            driver.findElement(By.xpath(" //a[text()='2015']")).click();
           
            Thread.sleep(3000);
           
            driver.findElement(By.xpath(" //td/a[text()='2016']")).click();
           
            Thread.sleep(3000);
           
            driver.findElement(By.xpath(" //td/a[text()='Aug']")).click();
           
            Thread.sleep(3000);
           
            driver.findElement(By.xpath(" //td/a[text()='8']")).click();
           
            Thread.sleep(3000);
           
            driver.findElement(By.xpath(".//*[@id='to-do']/span/span/span/span[2]")).click();
           
            Thread.sleep(3000);
           
            driver.findElement(By.xpath(" //li[text()='5:00 PM']")).click();
           
            Thread.sleep(3000);
           
//Fetching the value selected from datepicker for comparison.
            String getdatepicker = driver.findElement(By.xpath(".//*[@id='datetimepicker']")).getAttribute("value");
           
            System.out.println(getdatepicker);
           
//Comparing actual value with expected value
            if (tobeenterdate.equals(getdatepicker))
            {
                System.out.println("DatePicker value is correct");
            }
            else
            {
                System.out.println("DatePicker value is incorrect");
            }
           
            driver.close();
        }
        catch (Exception e)
        {
           
        }
                
        }
}

Well, Execute the above code and it will run perfectly and moreover you can modify the values mentioned in the code according to your requirements..cheers..:)

No comments:

Post a Comment