Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
Test Objects are an essential component of any successful automation project. Some Test Objects are static, meaning they never change. However, some of them are dynamic, which means they are dependent on parameters that change dynamically. In this article, we will read about various ways to deal with different types of test objects in Katalon Studio.
Prerequisites
The user should know how to write their tests in Script View. Along with that, the user should also have basic ideas about Java and Groovy.
Static Objects
Static objects are fairly simple to handle. We just have to simply navigate to the Object Repository First. There we select New Test Object and select our preferred selector. Because here, we will see about XPath, all of my examples will be for XPaths. However, we should know that the approach is the same for other selectors as well. We will save our element and retrieve our Test Object with Katalon's static built-in method findTestElement(String pathInRepository).
Dynamic Objects
Dynamic objects can be a little more complex to handle and care for than static objects. However, they are not as complicated as they appear. We have ways to handle parameterised objects in Katalon's own way. However, here we will see the other ways to do that here.
Parameterise Web Test Objects and Their Properties
We can dynamically update test object locators by utilising either local or global variables when parameterising test objects. This feature is useful in the following use cases or scenarios:
We want to check numerous checkboxes in bulk on a group of comparable components where this has to be done without constructing multiple Test Objects.
An object's locator can only be identified during runtime. This is because there is a set of similar objects. Also, the one that has been chosen cannot be given beforehand in test scripts.
To handle dynamic objects, Katalon Studio offers us parameterising test object properties. Dynamic objects are ones that change their attributes in response to certain business rules.
Defining Test Object Directly
There is a very easy way to define a test object. Let us take a look at a relatable code and its explanation below.
Code
import com.kms.katalon.core.testobject.ConditionType
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
import com.kms.katalon.core.testobject.TestObject
String NinjaDynamicId = 'CodingNinja123'
String path = '//div[@id="' + NinjaDynamicId + '"]'
TestObject x = new TestObject("objectName")
to.addProperty("path", ConditionType.EQUALS, path)
WebUI.click(x)
Explanation
First, we have the import statements. We can tell that the first couple of lines of the code is pretty simple to understand. First, we create a String Variable "NinjaDynamicId" and then put that into a different String named as "path".
The real stuff now happens in the following lines. We make a new instance of TestObject. We will call it as "x". After this, we will assign our selector to the new test object that we made. We will do this by using the "addProperty(String selectorType, ConditionType type, String selectorValue)" method. Finally, we have used the test object directly in Katalon's default keywords.
Isn't it simple? Yes, however, there are certain drawbacks to this solution. It's difficult to manage, especially when the Path changes frequently and we use the same test object in many test cases.
Creating a Separate Keyword for Dynamic Selectors
Extraction of dynamic selectors into individual keywords is a better method. This is because it helps us manage them well. We can have numerous keywords in your test project, each one for a single page or so. It's entirely up to us.
Below we can see a simple keyword with dynamic selectors.
package CodingNinjaPackage
public class MySelectors {
public static final String dynamicIdPath = '//div[@id="%s"]'
}
As we can see, the path is similar to the last example, but there is one minor change. For the String.format() method, we have to use the %s wildcard. We have done it instead of a variable. Now, let us make the necessary changes to our initial test case.
import com.kms.katalon.core.testobject.ConditionType
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
import com.kms.katalon.core.testobject.TestObject
import CodingNinjaPackage.MySelectors
String NinjaDynamicId = 'CodingNinja123'
String xpath = String.format(MySelectors.dynamicIdPath, NinjaDynamicId)
TestObject to = new TestObject("objectName")
to.addProperty("xpath", ConditionType.EQUALS, xpath)
WebUI.click(to)
As we can see here, the only difference is on the line that defines xpath.
We can also use our own wildcard in the selector value. Then, instead of String.format(), we will use String.replace().
We can see it below.
public static final String dynamicIdPath = '//div[@id="<<NinjaDynamicId>>"]'
// a line in a test case:
String xpath = MySelectors.dynamicIdPath.replace("<<dynamicId>>", NinjaDynamicId)
The benefit of storing dynamic selectors in different separate keywords is that all selectors are kept in one place. When a selection is changed, only one test is affected rather than all tests where it is used.
Create a Method that Returns a Dynamic Test Object
In this method, we now extend the MySelectors keyword with a new method that returns TestObject. This will remove a few lines of code from our test case, making it easier to maintain.
We have
package CodingNinjaPackage
import com.kms.katalon.core.testobject.TestObject
import com.kms.katalon.core.testobject.ConditionType
public class MySelectors {
public static final String dynamicIdPath = '//div[@id="%s"]'
public static TestObject getMyTestObject(String selectorType, String selectorValue) {
TestObject to = new TestObject()
to.addProperty(selectorType, ConditionType.EQUALS, selectorValue)
return to
}
}
Now, we will have the code appearing like the one below.
We may use the default Katalon keywords to call our new method because it returns TestObject. We do not need to bother about generating a new instance of TestObject in the test case. We can modify it in any way you like, and we can add more parameters to this procedure. The method is "ConditionType," or it can be simplified by passing only a TestObjectProperty object.
Frequently Asked Questions
What exactly is a dynamic test suite in Katalon?
A dynamic test suite is one that has various test cases that were added via a search query. While running the test suite, users can dynamically add new test cases.
What is the distinction between static and dynamic testing?
Static testing occurs during the verification step, whereas dynamic testing occurs during the validation stage. In static testing, code is examined but not run, while in dynamic testing, code gets executed and tested but not necessarily examined.
What framework is used in Katalon?
The Katalon Testing Platform is built on the hybrid test automation framework. It gives testing professionals a comprehensive and total range of testing solutions for software and apps.
Conclusion
The article discusses the different approaches to Handling static and dynamic Test Objects with Katalon studio. We saw the codes and read the explanations. Refer to our courses and explore Coding Ninjas Studio to find more exciting stuff. You can also look into the interview experiences and solve different problems. Look into our Guided paths, test series, libraries and resources to know more.