Skip to main content

Software

Katalon – Custom Keyword For JavaScript Executioner

5c91662ee4f8183ad5f624dabb6e3ce1

In Katalon, users can set additional keywords to increase the functionality of Katalon Studio in addition to the built-in keywords. Like other built-in keywords, custom keywords can be used in test cases once developed. Users can expand keywords and reuse them across projects thanks to this capability.

Assume that you want to execute the JavaScript from your Automation Script.

Katalon Studio is built on top of webDriver API. To use the webDriver API first, we need to get the implementation of webDriver API from the Katalon studio. Now we can use the webDriver instance to access the interface and methods present inside the webDriver API. We have to use the class driverFactory and the method getWebDriver. After getting the instance of webDriver, I can use webDriver API.

Create a Package

Custom keywords can be categorized into a package to make them easier to search for and utilize.

  1. Select File – New – Package from the main menu. Enter the name of your package and click OK.
  2. Accordingly, a new package is created in Katalon Studio.

 

Creating A Custom Keyword

 

  1. Create a custom keyword.

Select File – New – Keyword from the main menu to open the New Keyword dialog. Fill in with appropriate details and click on OK.

keyword

  1. Accordingly, a new keyword is added to the designated package.
  2. To define a Unique Keyword in Java/Groovy, add the following block of code to your class.

Code

We are creating an object “driver” that will get the instance of webDriver.

Thereby Typecasting the driver object into the javaScriptExecutor will give us the instance of javaScriptExecutor. Now using the executor object, we can call the method executeScript or executeAsyncScript.

We created a method executeScript with one parameter and a “@Keyword” annotation. It can be uniquely identified as a keyword.

Overriding the first executeScript, we have created a Second Method executeScript(String javascript, Testobject) with a “@Keyword” annotation.

The testObject keyword will execute the javaScript about a test object.

In the second executeScript, we must Convert the testObject into a webElement to use it along with the webDriver API. To achieve this, we use the class webUicommonHelper.findWebElement(). Helps us get a response as a webElement.

Now, the custom keyword is ready to Execute the javaScript. The driver keyword will return the instance of webDriver.

Congratulations

Conclusion: Congratulations, you have successfully created a custom keyword to execute JavaScript.

Happy Coding

Happy Coding!

Thoughts on “Katalon – Custom Keyword For JavaScript Executioner”

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Sanket Dudhe

Sanket Dudhe is an Associate Technical Consultant at Perficient. He has an experience of 4+ years as SDET. He loves technology and hence is curious to learn about new emerging technologies #lovefortechnology.

More from this Author

Follow Us
TwitterLinkedinFacebookYoutubeInstagram