0 votes
in Selenium by
How will you select a date from a datepicker in a webpage using Selenium for automated testing? Explain with a proper code.

1 Answer

0 votes
by

In such types of questions, the interviewer wants to assess how clear your understanding is of the framework. It is a good practice to explain the code while you write it so that the interviewer is engaged at all points and does not feel left out. We will be considering an example on MakeMyTrip.

Here, we will be using the chrome browser and so we will be implementing the code for the chrome browser only. You can implement similar code for firefox and other browsers as well. 

First of all, we create a package named browserSelection which contains a class defined for handling different types of browsers such as chrome, firefox that we may want to use. 

package browserSelection;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

public class SelectBrowser 

{

  static WebDriver driver;

  public static WebDriver useChrome()

  {

     System.setProperty("webdriver.chrome.driver", "E:\\SeleniumLibs\\\\chromedriver_win32\\chromedriver.exe"); 

     driver = new ChromeDriver(); 

     driver.manage().window().maximize();

     return driver;

  } 

}

Next, we create a package named datepicker which will contain a class containing methods defined for selecting a specific date on the website of MakeMyTrip. We need to import this package into our driver class and call the respective methods.

package datepicker;

import java.awt.AWTException;

import java.awt.Robot;

import java.awt.event.KeyEvent;

import java.util.List;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;

import org.openqa.selenium.JavascriptExecutor;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebDriverException;

import org.openqa.selenium.WebElement;

import org.testng.annotations.AfterMethod;

import org.testng.annotations.BeforeMethod;

import org.testng.annotations.Test;

import browserSelection.SelectBrowser;

public class DatePick 

{

  WebDriver driver;

  @BeforeMethod

  public void startBrowser()

  { 

     driver = SelectBrowser.useChrome(); 

  }

  @Test

  public void selectDateUtil() throws InterruptedException, AWTException

  {

     //Modify Wait time as per the Network Ability in the Thread Sleep method

     driver.get("https://www.makemytrip.com/"); 

     driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

     Thread.sleep(5000);

     try

     {

        driver.findElement(By.xpath("//input[@id='hp-widget__depart']")).click();

        Thread.sleep(2000);

        Date sampleDate = new Date(); // initialising the date object with the current date 

        SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM yyyy");

        String date = formatter.format(sampleDate); // formatting the date object in dd-MMM yyyy format

        String splitter[] = date.split("-");

        String monthYear = splitter[1]; // storing the month and year concatenated string excluding the day number

        String day = splitter[0]; // storing the day number in the current date

        System.out.println(monthYear);

        System.out.println(day);

        selectDate(monthYear,day); // function invocation

        Thread.sleep(3000);

        public void selectDate(String monthYear, String select_day) throws InterruptedException

        { 

           List<WebElement> elements = driver.findElements(By.xpath("//div[@class='ui-datepicker-title']/span[1]"));

           for (int i=0; i<elements.size();i++)

           {

              System.out.println(elements.get(i).getText());

              //Selecting the month

              if(elements.get(i).getText().equals(monthYear))

              {

                 //Selecting the date 

                 List<WebElement> days = driver.findElements(By.xpath("//div[@class='ui-datepicker-inline ui-datepicker ui-widget ui-widget-content ui-helper-clearfix ui-corner-all ui-datepicker-multi ui-datepicker-multi-2']/div[2]/table/tbody/tr/td/a"));

                 for (WebElement d:days)

                 { 

                    System.out.println(d.getText());

                    if(d.getText().equals(select_day))

                    {

                       d.click();

                       Thread.sleep(10000);

                       return;

                    }

                 }

              }

           }

           // if we do not find the matching month and year, we click on the arrow button to load new months.

           driver.findElement(By.xpath("//div[@class='ui-datepicker-inline ui-datepicker ui-widget ui-widget-content ui-helper-clearfix ui-corner-all ui-datepicker-multi ui-datepicker-multi-2']/div[2]/div/a/span")).click();

           selectDate(monthYear,select_day); // function invocation

        }

        @AfterMethod

        public void endBrowser()

        {

           driver.quit();

        }

     }

  }

}

In the above code, the function startBrowser() is used to invoke the useChrome() method from the imported package browserSelection. The function selectDateUtil() is used to select the current date from the date picker of the sample web page. The endBrowser() function is used to close the driver connections by invoking the quit() method.

Related questions

0 votes
asked Jan 5 in Selenium by rajeshsharma
0 votes
asked Aug 19, 2019 in Selenium by rahulsharma
...