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))
}

Other articles of interest:

Leave a Reply

Your email address will not be published. Required fields are marked *

Recent Comments
  • Parsing Strings in Katalon – Split, Substring and Readlines (4)
    • Anjana: Hey thanks for the response It is quite strange but initializing variable for delimiter and it worked String splitFormat = ‘-‘ WebUI.println(var_CampaignDura tion.toString().split(splitFor mat)[0])...
    • Don Pedro: I’m not really sure what the difference is, but I don’t think the “-“ in the split is the same as the one in the date. String var_CampaignDuration = “05/13/2019-06/07/2019&# 8221; String[] parsedDateRange =...
    • Anjana: Hi, I tried the same thing to split the string, but does not work for me, Could you plz point out whats wrong Variable var_CampaignDuration has value “05/13/2019-06/07/2019&# 8221; String[] parsedDateRange =...
    • Ellen: Thanks for sharing!! I like your contributions to Katalon topics.
  • How to Block games by Title and Tag on Steam (2)
    • Marcus861: I made an acc just to say: Same bro
    • JACK: Thanks, same just wanted to block anime games in my discover
  • Create a new and random UUID for an API call (1)
    • Anonymous: Hi ! I found this blog few weeks ago and it’s really helping me out with my tests! Thanks a lot for the Katalon tips and tricks ! Keep up the good work 🙂
  • How To Disable the Quicken Registration Prompt (25)
    • Greg: For me, holding the *LEFT* CTL + Shift then clicking Online, One Step Update worked. I originally tried holding the right CTL + Shift, and it didn’t work. I’m using Quicken 2006, so I don’t know if it will work...
    • Joe SR>: My monthly income is deposited into my Credit Union account. I use debit whenever possible. I write checks manually and mail them. I use Quicken 2012 off-line only. I have entered all my money and investment accounts. I...
    • Prtet: Never say never….every time I swear I will never use Quicken again, I discover that there are still no viable alternatives. Amazing that there is no decent personal finance software.
    • Joe D.: Holding CTRL + Shift keys and selecting On Line | One Step Update from the main menu worked for my Quicken 2004. I’m grateful that you wrote a synopsis (“Simply put, …”) just beneath the link to the blog...
    • Susan Long: I bought my quicken disc in the beginning and it came with a registration number. I rang the helpline and they gave me the code to put in and talked me through it. It you downloaded your version then you don’t own it...
    • Peter: You might consider running your old version of Quicken on an ancient computer. This is what I have done for years. The newer versions are fraught with problems- criminal, in my opinion. One version made mathmatical errors when...
  • Working with Dates and Date Formatting in Katalon Studio (6)
    • Ajoo: Thank you for the details. How do i remove leading “0” from dates. i.e. while formatting i receive 04/21/2019, but i need 4/21/2019. (same applies for date)
  • What is Katalon Studio? A Distro of Selenium, Groovy and Eclipse (1)
    • Mahesh: Looking for more posts on katalon studio.your katalon stuffs are always exiting