Handling Static and Dynamic Test Objects

Introduction

Test Objects are one of the most crucial parts of building a successful automation project. Some of them are static (do not change at all) and some of them are dynamic (they are dependent on some dynamically changing parameters). This tutorial shows you different approaches for handling different types of test objects.

Requirements

You should be able to write your tests in the Script view. You should also know the basics of Java/Groovy.

Static objects

Static objects are very easy to handle. Just go to Object Repository, choose New Test Object and use your favorite selector (I prefer XPath, so all my examples will be for XPaths, but the approach is the same also for other selectors). Save your element and use Katalon static built-in method findTestElement(String pathInRepository) to get your Test Object.

Dynamic objects

Dynamic objects are a bit trickier to deal with than static objects, but it is not so difficult as it may look like. There is a Katalon way to handle parameterized objects, but I want to introduce other possible approaches.

Define your test object directly in a test case

The easiest way to define a test object. See a sample code below:

Without counting the import statements, the first two lines are simple – create a String variable dynamicId and put it into another String – xpath. The magic is happening on the next two lines. You create a new instance of TestObject (let’s call it to). Then, you assign your selector to this new test object using method addProperty(String selectorType, ConditionType type, String selectorValue). Finally, you use the test object directly in the default Katalon keywords. For details on ConditionType, please refer to API documentation.

Easy, isn’t it? Well, yes, but there are also cons of this solution. It is hardly maintainable, especially when your XPath changes often and when you create the same test object in multiple test cases.

Create a separate keyword for dynamic selectors

A better way to handle dynamic selectors is to extract them into separate keywords. You can have several keywords in your test project, one for a single page or so. It is up to you.

I will show a simple keyword holding dynamic selectors.

As you can see, there is the same path as in previous example, but there is a small difference. Instead of using a variable, I use %s wildcard for the String.format() method. Let’s update our original test case accordingly.

As you can see, the only change is on the line defining xpath. If you are not familiar with String.format(), please refer to Java documentation.

Alternatively, you can use your own wildcard in the selector value. Then, use String.replace() rather than String.format().

The advantage of storing dynamic selectors in separate keywords is that you keep all selectors in one place. When a selector changes, you change only one place instead of all tests where you use it.

Create a method which returns a dynamic test object

Now we extend the MySelectors keyword by adding a new method to return TestObject. This will eliminate a few lines of code in your test case, making it more maintainable.

And our test case will look like this:

As our new method returns TestObject, we can call it directly using the default Katalon keywords. In the test case, you do not have to worry about creating a new instance of TestObject. You can customize it however you want, you can add more parameters into this method (i.e. ConditionType), or simplify it to pass only a TestObjectProperty instance.

Conclusion

The main point of this tutorial is that there are several different approaches to deal with dynamic selectors. This tutorial presents a few examples (more or less difficult) which can help you with this topic. I am 100% sure that there are even more effective ways how to do it, I will be glad if you share your own approach.

About the author
Marek Melocik is currently working as Test Automation Engineer in Brno, Czech Republic. He has been working in QA industry since 2014.
Meet Marek at www.linkedin.com/in/marek-melocik

83% people found this helpful
Related testing tools
Katalon Studio
Best automation solution for software testing team. Completely free!
Katalon Recorder
Ultimate Selenium IDE to record, play, and debug app. Fast and extensible!
Katalon Analytics
Bring you to the next level of test execution reports. Smart and seamless solution!
Scroll up