Handle Bootstrap Date-picker in Selenium

Below are the two programs given to handle date picker in selenium.

1. Using loop:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class HandleDatePciker {
 WebDriver driver;

 @BeforeMethod
 public void setup() {
  System.setProperty("webdriver.chrome.driver",
    "C:\\Users\\Hitendra\\Downloads\\chromedriver_win32 (1)\\chromedriver.exe");
  driver = new ChromeDriver();
  driver.manage().window().maximize();
  driver.get("https://www.seleniumeasy.com/test/bootstrap-date-picker-demo.html");
 }

 @Test
 public void datePicker() {

  String month = "May 2019";
  String day = "13";

  driver.findElement(By.xpath("//*[@id=\"sandbox-container1\"]//input")).click();

  while (true) {

   String textString = driver.findElement(By.xpath("/html/body/div[3]/div[1]/table/thead/tr[2]/th[2]"))
     .getText();

   if (textString.equals(month)) {
    break;
   } else {
    driver.findElement(By.xpath("/html/body/div[3]/div[1]/table/thead/tr[2]/th[1]")).click();
   }
  }
  driver.findElement(By.xpath("//table/tbody/tr/td[contains(text()," + day + ")]")).click();
 }
}

2. Using JavaScript:
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class DatePickerByJS {
 WebDriver driver;
 
 @BeforeMethod
 public void setup() {
  System.setProperty("webdriver.chrome.driver",
    "C:\\Users\\Hitendra\\Downloads\\chromedriver_win32 (1)\\chromedriver.exe");
  driver = new ChromeDriver();
  driver.manage().window().maximize();
  driver.get("https://www.seleniumeasy.com/test/bootstrap-date-picker-demo.html");
 }
 
 @Test
 public void datePcikerTestbyJS() {
  WebElement element=driver.findElement(By.xpath("//*[@id=\"sandbox-container1\"]//input"));
  String dateString="04/08/1996";
  datePcikerByJS(driver, dateString, element);
 }
 
 public void datePcikerByJS(WebDriver driver, String date,WebElement element) {
  JavascriptExecutor js=(JavascriptExecutor) driver;
  js.executeScript("arguments[0].setAttribute('value','"+date+"')", element);
 }

}

Please refer below YouTube video :

No comments:

Post a Comment