Saturday, December 27, 2014

Datepicker using Selenium WebDriver


In This Program :
  • Navigating to http://jqueryui.com/datepicker/
      

  • Clicking on TextBox By ID driver.findElement(By.id("datepicker")).click();
          

  • Click on next so that we will be in next month

   

  • Date Widget is a table so we taking the td and tr store it in List
        WebElement datewidget=driver.findElement(By.id("ui-datepicker-div"));

    List<WebElement> rows=datewidget.findElements(By.tagName("tr"));
       List<WebElement> columns=datewidget.findElements(By.tagName("td"));

  • Loop the Column and get the date
for(WebElement cell : columns) {

           if(cell.getText().equals("23"){
                            cell.findElement(By.linkText("23")).click();
  break;
 }}



Source code  :

import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;


public class datepick {
      
       WebDriver driver;
      
       @BeforeTest
       public void setup()
       {
              driver=new FirefoxDriver();
             
       }
       @Test
       public void testdate()
       {
               driver.get("http://jqueryui.com/datepicker/"); 
               driver.switchTo().frame(0); 
               driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
               driver.findElement(By.id("datepicker")).click();
               driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
               driver.findElement(By.xpath("//*[@id='ui-datepicker-div']/div/a[2]/span")).click();
               //datewidget is a table it contain rows and colums
               WebElement datewidget=driver.findElement(By.id("ui-datepicker-div"));
               List<WebElement> rows=datewidget.findElements(By.tagName("tr"));
               List<WebElement> columns=datewidget.findElements(By.tagName("td"));
               
               for(WebElement cell : columns)
               {
                      if(cell.getText().equals("23"))
                      {
                            cell.findElement(By.linkText("23")).click();
                            break;
                      }
               }
       }
      
       @AfterTest
       public void end()
       {
              driver.quit();
       }

}


Hope you Likke this post. 




No comments:

Post a Comment