# Datepicker functionality

**URL:** <https://forum.katalon.com/t/datepicker-functionality/34516>\
**Category:** Web Testing\
**Tags:** katalon-studio\
**Created:** [October 11, 2019, 6:48am UTC](https://forum.katalon.com/t/datepicker-functionality/34516 "2019-10-11T06:48:39Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![swetha.ravula](https://avatars.discourse-cdn.com/v4/letter/s/a587f6/32.png) [@swetha.ravula](https://forum.katalon.com/u/swetha.ravula)\
**Post date:** [October 11, 2019, 6:48am UTC](https://forum.katalon.com/t/datepicker-functionality/34516/1 "2019-10-11T06:48:39Z")

</div>

Hello,

The below is the code for date picker the given date is not getting selected but the date is displayed as 11-09-2019. Can you please help me to resolve this issue.

package customename  
import static com.kms.katalon.core.checkpoint.CheckpointFactory.findCheckpoint  
import static com.kms.katalon.core.testcase.TestCaseFactory.findTestCase  
import static com.kms.katalon.core.testdata.TestDataFactory.findTestData  
import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject

import com.kms.katalon.core.annotation.Keyword  
import com.kms.katalon.core.checkpoint.Checkpoint  
import com.kms.katalon.core.checkpoint.CheckpointFactory  
import com.kms.katalon.core.logging.KeywordLogger  
import com.kms.katalon.core.mobile.keyword.MobileBuiltInKeywords  
import com.kms.katalon.core.model.FailureHandling  
import com.kms.katalon.core.testcase.TestCase  
import com.kms.katalon.core.testcase.TestCaseFactory  
import com.kms.katalon.core.testdata.TestData  
import com.kms.katalon.core.testdata.TestDataFactory  
import com.kms.katalon.core.testobject.ObjectRepository  
import com.kms.katalon.core.testobject.TestObject  
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords  
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords

import internal.GlobalVariable

import MobileBuiltInKeywords as Mobile  
import WSBuiltInKeywords as WS  
import WebUiBuiltInKeywords as WebUI

import org.openqa.selenium.WebElement  
import org.openqa.selenium.support.ui.\*  
import org.openqa.selenium.WebDriver  
import org.openqa.selenium.By

import com.kms.katalon.core.mobile.keyword.internal.MobileDriverFactory  
import com.kms.katalon.core.webui.driver.DriverFactory

import com.kms.katalon.core.testobject.RequestObject  
import com.kms.katalon.core.testobject.ResponseObject  
import com.kms.katalon.core.testobject.ConditionType  
import com.kms.katalon.core.testobject.TestObjectProperty

import com.kms.katalon.core.mobile.helper.MobileElementCommonHelper  
import com.kms.katalon.core.util.KeywordUtil

import com.kms.katalon.core.webui.exception.WebElementNotFoundException  
import com.kms.katalon.core.webui.common.WebUiCommonHelper

class sample {  
@Keyword  
public static void Datepicker(TestObject calender, String exp\_Date, String exp\_Month,String exp\_Year)throws Exception {  
String expDate = null, calYear = null,datepickerText=null,minYear=null,maxYear=null;  
//KeywordLogger log = new KeywordLogger()  
[//log.logInfo](https://log.logInfo)(exp\_Date+“yes”+exp\_Month+“yes”+exp\_Year);  
int expMonth = 0, expYear = 0;

```
	WebElement datePicker1;
	WebElement datePickerY;
	WebElement datePickerM;
	WebElement datePickerD;

	List<WebElement> noofDayss=null,noOfDays=null,noOfMonths=null,noOfYears=null;
	boolean dateNotFound = true;
	List<String> monthList = Arrays.asList("None","Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");

	def driver = DriverFactory.getWebDriver()
	//DriverFactory.changeWebDriver(driver)
	//driver.findElement(By.xpath("(//div[2]/div[2]/div/div/div/input)[1]")).click();
	WebElement SelectCalender = WebUiCommonHelper.findWebElement(calender, 20);
	SelectCalender.click()

	expDate = (exp_Date);
	println(expDate);
	expMonth = Integer.parseInt(exp_Month);
	println(expMonth);
	expYear = Integer.parseInt(exp_Year);
	println(expYear);

	Thread.sleep(9000);

	driver.findElement(By.xpath("//select[2]")).click();
	WebElement datePicker_Heading3 =(driver).findElement(By.xpath("(//select[@class='ui-datepicker-year'])/option"));
	println(datePicker_Heading3.getText());

	//wait.until(ExpectedConditions.visibilityOf(datePicker_Heading3));
	datepickerText =datePicker_Heading3.getText();

	String[] datepickerYear = datepickerText.split("-");

	minYear =datepickerYear[0];

	maxYear = datepickerYear[180];

	if((expYear >= Integer.parseInt(minYear)) && (expYear<=Integer.parseInt(maxYear))) {
		datePickerY = (driver).findElement(By.xpath("//select[@class='ui-datepicker-year']"));
		noOfYears = datePickerY.findElements(By.xpath("(//select[@class='ui-datepicker-year'])/option"));
		firstloop:
		for (WebElement year : noOfYears) {
			// Select the date from date picker when condition
			// match.
			if (year.getText().equalsIgnoreCase((String)exp_Year)) {
				year.click();
				Thread.sleep(1500);
				datePickerM = (driver).findElement(By.xpath("//select[@class='ui-datepicker-month']"));
				noOfMonths = datePickerM.findElements(By.xpath("//select[@class='ui-datepicker-month']/option"));
				Thread.sleep(1000);
				for (WebElement month : noOfMonths) {
					System.out.println(" the expected month in int is : "+expMonth);
					System.out.println(" the expected month is : "+monthList.get(expMonth));
					System.out.println(" the Actual month is : "+month.getText());
					if ((monthList.get(expMonth)).equalsIgnoreCase(month.getText())) {
						month.click();
						datePickerD = (driver).findElement(By.xpath("(//table[@class='ui-datepicker-calendar'])//tbody"));
						noOfDays = datePickerD.findElements(By.xpath("(//table[@class='ui-datepicker-calendar']/tbody/tr/td)//a"));
						Thread.sleep(1500);
						for (WebElement cell : noOfDays) {
							if (cell.getText().equalsIgnoreCase(expDate)) {
								cell.click();
								break firstloop;
							}
						}
					}
				}
			}
			// else{
			//
			// throw new Exception()
			// }
		}
		dateNotFound = false;
	}else if (expYear > Integer.parseInt(maxYear)) {
		WebElement Next =(driver).findElement(By.xpath("//div/a[2]/span[@class='ui-icon ui-icon-circle-triangle-e']"));
		if(Next.getAttribute("style").equalsIgnoreCase("visibility: visible;"))
		{// Click on next button of date picker.
			Next.click();
		}else {
			throw new Exception("This is exception")
		}
	}
	// If current selected month and year are greater than expected
	// month and year then go Inside this condition.
	else if (expYear < Integer.parseInt(minYear)) {
		WebElement Previous =(driver).findElement(By.xpath("//div/a/span[@class='ui-icon ui-icon-circle-triangle-w']"));
		if(Previous.getAttribute("style").equalsIgnoreCase("visibility: visible;"))
		{
			// Click on previous button of date picker.
			Previous.click();
		}else{
			throw new Exception("This is exception")
		}
	}

}

```

}

Please find the below screen shot for datepicker

 ![image](https://europe1.discourse-cdn.com/katalon/original/2X/e/e2c23729b588c998ab88f03ddc51a91f310b10d3.png)

---

<div class="post-metadata">

**Author:** ![Timo\_Kuisma](https://avatars.discourse-cdn.com/v4/letter/t/8baadc/32.png) [@Timo\_Kuisma](https://forum.katalon.com/u/Timo_Kuisma)\
**Post date:** [October 11, 2019, 1:31pm UTC](https://forum.katalon.com/t/datepicker-functionality/34516/2 "2019-10-11T13:31:39Z")

</div>

hello,

are you able to add wanted dates direct to fields?

---

<div class="post-metadata">

**Author:** ![swetha.ravula](https://avatars.discourse-cdn.com/v4/letter/s/a587f6/32.png) [@swetha.ravula](https://forum.katalon.com/u/swetha.ravula)\
**Post date:** [October 15, 2019, 9:41am UTC](https://forum.katalon.com/t/datepicker-functionality/34516/3 "2019-10-15T09:41:38Z")

</div>

Yes through front end we are able to select the dates but when we use the custom keyword the specified date is not displayed (and also not selected).

---

<div class="post-metadata">

**Author:** ![Timo\_Kuisma](https://avatars.discourse-cdn.com/v4/letter/t/8baadc/32.png) [@Timo\_Kuisma](https://forum.katalon.com/u/Timo_Kuisma)\
**Post date:** [October 15, 2019, 2:32pm UTC](https://forum.katalon.com/t/datepicker-functionality/34516/4 "2019-10-15T14:32:56Z")

</div>

hi,

not got the point, what is the issue to get dates using custom keyword?

---

<div class="post-metadata">

**Author:** ![swetha.ravula](https://avatars.discourse-cdn.com/v4/letter/s/a587f6/32.png) [@swetha.ravula](https://forum.katalon.com/u/swetha.ravula)\
**Post date:** [October 16, 2019, 6:29am UTC](https://forum.katalon.com/t/datepicker-functionality/34516/5 "2019-10-16T06:29:45Z")

</div>

> [@swetha.ravula](#):
>
> ustom keyword the specified date

Screen Shot 1

![image](https://europe1.discourse-cdn.com/katalon/original/2X/6/6dc8a592b595ba3d914a89fd8aae6c1a2e3b3d76.png)

Code  
Screen Shot 2

package customename  
import static com.kms.katalon.core.checkpoint.CheckpointFactory.findCheckpoint  
import static com.kms.katalon.core.testcase.TestCaseFactory.findTestCase  
import static com.kms.katalon.core.testdata.TestDataFactory.findTestData  
import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject

import com.kms.katalon.core.annotation.Keyword  
import com.kms.katalon.core.checkpoint.Checkpoint  
import com.kms.katalon.core.checkpoint.CheckpointFactory  
import com.kms.katalon.core.logging.KeywordLogger  
import com.kms.katalon.core.mobile.keyword.MobileBuiltInKeywords  
import com.kms.katalon.core.model.FailureHandling  
import com.kms.katalon.core.testcase.TestCase  
import com.kms.katalon.core.testcase.TestCaseFactory  
import com.kms.katalon.core.testdata.TestData  
import com.kms.katalon.core.testdata.TestDataFactory  
import com.kms.katalon.core.testobject.ObjectRepository  
import com.kms.katalon.core.testobject.TestObject  
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords  
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords

import internal.GlobalVariable

import MobileBuiltInKeywords as Mobile  
import WSBuiltInKeywords as WS  
import WebUiBuiltInKeywords as WebUI

import org.openqa.selenium.WebElement  
import org.openqa.selenium.support.ui.\*  
import org.openqa.selenium.WebDriver  
import org.openqa.selenium.By

import com.kms.katalon.core.mobile.keyword.internal.MobileDriverFactory  
import com.kms.katalon.core.webui.driver.DriverFactory

import com.kms.katalon.core.testobject.RequestObject  
import com.kms.katalon.core.testobject.ResponseObject  
import com.kms.katalon.core.testobject.ConditionType  
import com.kms.katalon.core.testobject.TestObjectProperty

import com.kms.katalon.core.mobile.helper.MobileElementCommonHelper  
import com.kms.katalon.core.util.KeywordUtil

import com.kms.katalon.core.webui.exception.WebElementNotFoundException  
import com.kms.katalon.core.webui.common.WebUiCommonHelper

class sample {  
@Keyword  
public static void Datepicker(TestObject calender, String exp\_Date, String exp\_Month,String exp\_Year)throws Exception {  
String expDate = null, calYear = null,datepickerText=null,minYear=null,maxYear=null;  
//KeywordLogger log = new KeywordLogger()  
[//log.logInfo](https://log.logInfo)(exp\_Date+“yes”+exp\_Month+“yes”+exp\_Year);  
int expMonth = 0, expYear = 0;

```
	WebElement datePicker1;
	WebElement datePickerY;
	WebElement datePickerM;
	WebElement datePickerD;

	List<WebElement> noofDayss=null,noOfDays=null,noOfMonths=null,noOfYears=null;
	boolean dateNotFound = true;
	List<String> monthList = Arrays.asList("None","Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");

	def driver = DriverFactory.getWebDriver()
	//DriverFactory.changeWebDriver(driver)
	//driver.findElement(By.xpath("(//div[2]/div[2]/div/div/div/input)[1]")).click();
	WebElement SelectCalender = WebUiCommonHelper.findWebElement(calender, 20);
	SelectCalender.click()

	expDate = (exp_Date);
	println(expDate);
	expMonth = Integer.parseInt(exp_Month);
	println(expMonth);
	expYear = Integer.parseInt(exp_Year);
	println(expYear);

	Thread.sleep(9000);

	driver.findElement(By.xpath("//select[2]")).click();
	WebElement datePicker_Heading3 =(driver).findElement(By.xpath("(//select[@class='ui-datepicker-year'])/option"));
	println(datePicker_Heading3.getText());

	//wait.until(ExpectedConditions.visibilityOf(datePicker_Heading3));
	datepickerText =datePicker_Heading3.getText();

	String[] datepickerYear = datepickerText.split("-");

	minYear =datepickerYear[0];

	maxYear = datepickerYear[180];

	if((expYear >= Integer.parseInt(minYear)) && (expYear<=Integer.parseInt(maxYear))) {
		datePickerY = (driver).findElement(By.xpath("//select[@class='ui-datepicker-year']"));
		noOfYears = datePickerY.findElements(By.xpath("(//select[@class='ui-datepicker-year'])/option"));
		firstloop:
		for (WebElement year : noOfYears) {
			// Select the date from date picker when condition
			// match.
			if (year.getText().equalsIgnoreCase((String)exp_Year)) {
				year.click();
				Thread.sleep(1500);
				datePickerM = (driver).findElement(By.xpath("//select[@class='ui-datepicker-month']"));
				noOfMonths = datePickerM.findElements(By.xpath("//select[@class='ui-datepicker-month']/option"));
				Thread.sleep(1000);
				for (WebElement month : noOfMonths) {
					System.out.println(" the expected month in int is : "+expMonth);
					System.out.println(" the expected month is : "+monthList.get(expMonth));
					System.out.println(" the Actual month is : "+month.getText());
					if ((monthList.get(expMonth)).equalsIgnoreCase(month.getText())) {
						month.click();
						datePickerD = (driver).findElement(By.xpath("(//table[@class='ui-datepicker-calendar'])//tbody"));
						noOfDays = datePickerD.findElements(By.xpath("(//table[@class='ui-datepicker-calendar']/tbody/tr/td)//a"));
						Thread.sleep(1500);
						for (WebElement cell : noOfDays) {
							if (cell.getText().equalsIgnoreCase(expDate)) {
								cell.click();
								break firstloop;
							}
						}
					}
				}
			}
			// else{
			//
			// throw new Exception()
			// }
		}
		dateNotFound = false;
	}else if (expYear > Integer.parseInt(maxYear)) {
		WebElement Next =(driver).findElement(By.xpath("//div/a[2]/span[@class='ui-icon ui-icon-circle-triangle-e']"));
		if(Next.getAttribute("style").equalsIgnoreCase("visibility: visible;"))
		{// Click on next button of date picker.
			Next.click();
		}else {
			throw new Exception("This is exception")
		}
	}
	// If current selected month and year are greater than expected
	// month and year then go Inside this condition.
	else if (expYear < Integer.parseInt(minYear)) {
		WebElement Previous =(driver).findElement(By.xpath("//div/a/span[@class='ui-icon ui-icon-circle-triangle-w']"));
		if(Previous.getAttribute("style").equalsIgnoreCase("visibility: visible;"))
		{
			// Click on previous button of date picker.
			Previous.click();
		}else{
			throw new Exception("This is exception")
		}
	}

}

```

}

Screen Shot 3: Date Picker

 ![image](https://europe1.discourse-cdn.com/katalon/original/2X/8/84022b55bc296f1a066dc11bd1e06549db79fbb0.png)

There are three screen shots attached  
The first one is the manual script in which we specified the date as 20th Nov 2019 (the date on which the leave has to be applied or the date that has to be selected in the date picker(calendar)).

The second screen shot is the custom keyword. When we execute this then the date is displayed as 11-Sep-2019 in the date picker field but the date has to be selected as 20th Nov 2019 (as we have given the date)

The third screen shot is the date picker (calendar) that we used in our application.

Is there any problem with the above mentioned custom keyword?  
The date picker we used is of j query.

Note: Is there any other solution?

Thank You!
