Passing Object Names as String Variables in Katalon Studio

While working on a Custom Keyword, I noted the code could be even more modular if I was able to pass in the object name. It doesn’t need to be an object, just the path to the object in the Object Repository. As a simple example, take the code to read the value from a Select or Input field. The code is the same except for the referenced object.
The code would look like:

int resultsPerPage=WebUI.getAttribute(findTestObject('Sales Data/Sales Table/select-Results Per Page'),'value').toInteger()

With that in mind, we could create a variable with the path, then call the Custom Keyword with that variable. The code would then look like:

objectPath="Sales Data/Sales Table/select-Results Per Page"
CustomKeywords.'commonCode.DataCollection.getResults'(objectPath)

We could then change the path and call the Keyword again, still using the same code.
As a slightly more involved example, we have a page with three sets of filters. Each row has a different definition which meant three objects in the repository. When setting up code to manipulate those filters, everything is the same except for the object name. In order to verify all three rows of filters, 3 code blocks would be needed.
However, setting a variable to the path of the object means the code can be re-used.

//Validate the second row of filters
for (loop = 1; loop <=3; loop++) {
WebUI.click(findTestObject(filterName)
CustomKeywords.'commonCode.verifyFilter.verifyTableData'(objectPath, loop, filterType[loop], filterDescriptionText[loop])
WebUI.delay(2)
}

It would also be possible to make a List of path names and refer to that as well.
This may not be the best or most effective way to work with objects, but it’s very straightforward and easy to understand. This has come up a couple of times already where I’ve been able to substitute the object path and parameterize larger portions of my code blocks.

Other articles of interest:

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:

Currency Formatting with getCurrencyInstance()

One of my normal practices is to strip all the formatting from a site value, simulate the site calculations, then compare my calculated value to the site value and display the results in the log file. But without formatting, the results can be hard to read as they go scrolling by. To add the formatting back is a relatively simply process. We can use the NumberFormat package and the format option to put the $ and , where they belong. Thus, 16384 becomes $16,384.

To use the formatting, import the following libraries, which sets the formatting based on locale.

import java.text.NumberFormat
import java.util.Locale

Next, create reference to the NumberFormat library:

NumberFormat defaultFormat = NumberFormat.getCurrencyInstance()

Finally, format the number back to a dollar figure:

log.logWarning(‘Total amount in dollars is:’ + defaultFormat.format(totalAmount))

One thing to note, the value to format must be an integer, float, double or big decimal value. Basically, anything other than a string.

As a basic example:

import com.kms.katalon.core.logging.KeywordLogger as KeywordLogger

import java.text.NumberFormat

import java.util.Locale

KeywordLogger log = new KeywordLogger()

NumberFormat defaultFormat = NumberFormat.getCurrencyInstance()

double dollarFigure = 16384.526;

log.logWarning(“The dollar figure is: ” + defaultFormat.format(dollarFigure))

Output:

The dollar figure is: $16,384.53

Other articles of interest:

Boostnote for Code Snippets

After working with Katalon Studio for several months, I have a wealth of code snippets and examples. And with my collection growing each day, where to store them all for easy access? The most common examples are in TypeIt4Me, but the rest need a good storage place that is easily searchable.

There are quite a few code snippet tools available such as Quiver and Snippetslab, but I recently got hooked up with Boostnote and find it’s just what I’m looking for.

Boostnote is a Markdown editor with syntax highlighting for dozens of languages with Groovy, Java, HTML and CSS right there in the mix.

Code can be organized into different folders and each snippet can be tagged for further grouping and easier search. Each snippet can have multiple tabs, so examples, links and other pieces of information can be found under a single header. For example you can have getText and setText examples for Katalon and Selenium tied together.

Boostnote works with Dropbox, Google Drive and other sync tools. It’s available for Windows, macOS and even tablets. Like the Atom editor, it has dozens of UI and Editor theme choices.

Since it has Markdown, I’ve also built a dashboard note with links to Katalon articles, the support forum, documents on my local machine, tutorial videos and blog sites. Both the code and additional resource material is available in one location.

For my needs, Boostnote is a very capable and highly configurable tool that let’s me organize code snippets into categories and allows plenty of flexibility for writing and recalling the next block of code.

boostnote

Other articles of interest:

Using the Katalon Automation Recorder to find Objects

While Katalon Studio has the built in SpyWeb tool, and it works quite well to capture objects and build a repository, I don’t find myself using it that much. Instead, I rely on the Katalon Automation Recorder, the replacement for the Selenium IDE.

I find it quicker to start, and runs alongside the web page. I also find it easier to pick from multiple styles of object paths without all of them being assigned to the object itself.

For those who worked with the Selenium IDE, the interface and usage are almost identical and comes as a Firefox or Chrome plugin. Once running, it’s just a matter of clicking the Inspect button then selecting the object in question. When hovering over an object, it will turn yellow, and when selected, the XPATH will appear. If you want more choices of that object is defined, click the dropdown arrow and additional choices, if available, will be listed. You can also enter a target path, or modify the existing one, then click the magnifying glass to highlight the object in question. This is how I confirm which part of the path can be parameterized.

SpyWeb is good, but I find this a bit faster and more flexible since I’m already in the browser and the object is visible.

I know some prefer to use the Inspect Element function and copy the path from there, but this is another choice.

katalon-automation-recorder

Other articles of interest:

Recent Comments

  • How To Disable the Quicken Registration Prompt (18)
    • Susan Long: I have Quicken personal 2012 which I only use to enter and retrieve data. It suits my needs perfect. Now they have blocked my access and want me to re register. I don’t want to upgrade as it I am comfortable with this...
    • Stewart: I have been using Quicken 200 basic since it was introduced but I now have a new PC using windows 10 and can’t get rid of the registration prompt. I have tried the above control/shift trick but to no avail. Any ideas
    • Dave: Works for Quicken 2007 too. Thanks, Gina!
    • Charlie Fellenbaum: Yay, thanks, and that worked for Quicken 2006 as well.
    • Ken: This didn’t work for me. I have 2006. I installed on a new laptop. I must have registered before, don’t remember. I tried to register this time, but got an error message. I’d rather not registered, though. I use...
    • Neil Murphy: I discovered that early on. I’m using 2001.
  • Conditional Statements – IF .. ELSE IF in Katalon Studio (1)
    • Allyson: I used this : elementVisible=WebUI.verifyEle mentPresent… but it failed because “FAILED because (of) Unable to verify object ‘Object Repository/Homepage/b_vaolopho c’ is present (Root cause:...
  • Quasi Performance/Load Testing with Katalon Studio (1)
    • Anthony: I have done this with 100 plus machines at the same time and it worked well and provided some useful feedback.
  • A Try Catch example in Katalon Studio (3)
    • Venkatesh: Hi Don Pedro Can you share me sample project ….which consist of basic to advanced stuff…If your github or gitlab url aso present please share… As I am beginer to this I need to learn lot
    • Don Pedro: Hi Lucas. Thank you for the kind comments. I would be happy to help. Let me know how I could be of assistance.
    • Lucas: Thanks for sharing a lot of Katalon tips and tricks. The Katalon team highly appreciate your support and feedback. To make Katalon Studio better and be the best free automation solution for testing community, we wish to coordinate...
  • Securely storing passwords and login details with Set Encrypted Text in Katalon Studio (1)
    • Agus: Hi Don, Is there a way to set the encrypted text to be used in the ‘WebUI.authenticate̵ 7; method? Basically I am trying to encrypt the password that is passed to the ‘WebUI.authenticate̵ 7; method. Thank you.
  • Katalon Studio – Manual View – The start of a test script (2)
    • ben: Hello Don Great Website. Keep up the work. I learnt a lot. cheers Ben.
  • Another success with Katalon Studio (1)
    • Mahesh Joshi: Hi Don, I just started following your blog in regards to katalon. I want to introduce katalon studio at my work we do manual testing at work at the moment. What approach I should use for proof of concept with katalon for...
  • What went wrong with Crime City? (34)
    • G D: One thing I have noticed is the awkwardness of items and the attack points and how you can earn them. I was pretty pathetic with attack points and then I played that limited time boss and got weapons that had attack points in the...