Enter dates into a date picker for Chrome and Firefox
I’m not a fan of, nor am I versed in handling the latest trend of using date picker calendars so I was trying to cheat my way around it by entering a date in the input field. But, this lead to another problem, at least for the date picker we were using. It also lead to two interesting answers on how to solve it.
When entering the date, Chrome and Firefox need the date information passed in two completely different ways. Not just the format of the date, but the commands themselves.
For Firefox, the setText command worked as expected. For Chrome, sendKeys was needed. Additionally, Firefox needed the year to come first. This means the code isn’t interchangeable and generates an error when used with the wrong browser.
Since there two different commands are needed, how do you handle the right case for the right browser?
Here are the two commands for entering the date into the text field of the date picker:
WebUI.setText(findTestObject('dateField'), '2017-10-31') WebUI.sendKeys(findTestObject('dateField'), Keys.chord('10-31-2017', Keys.ENTER, Keys.TAB))
We have an answer, but how to pick the correct one at runtime?
Katalon has a way to call out and get the browser type. We can use the DriverFactory library along with the .getName command. This will result in a text string of FIREFOX_DRIVER or CHROME_DRIVER
import com.kms.katalon.core.webui.driver.DriverFactory as DriverFactory browserName=DriverFactory.getExecutedBrowser().getName()
Putting it all together, it’s possible to use this little snippet to check the browser type, then use the correct command to set the date.
//Use setText for Firefox and sendKeys for Chrome
import com.kms.katalon.core.webui.driver.DriverFactory as DriverFactory
browserName=DriverFactory.getExecutedBrowser().getName()
if (browserName=="FIREFOX_DRIVER"){
WebUI.setText(findTestObject('dateField'), '2017-10-31')
} else {
WebUI.sendKeys(findTestObject('dateField'), Keys.chord('10-31-2017', Keys.ENTER, Keys.TAB))
}