Parameterized Test Object on the fly

I have made a demo here:

Problem to solve

Let’s imagine you have a target Web page which contains a menu with 20 <ul> elements each of which contains 10 times of fragment: <li><a href="..">label</a></li>. The way how <ul> and <li> elements are marked up is uniform. You want to verify the label texts against some fixture data. You will use Katalon Studio to do the test.

Katalon’s documents would suggest to you to create a set of Test Objects in the Object Repository using the Spy Object. However, nobody would like to create 20 * 10 = 200 entries in the Object Repository one by one using the Spy Object tool.

My solution

The demo shows you :

  1. Test case creates a Test Object instance on the fly without storing it into the Object Repository
  2. Test case reuses the Test Object instance multiple times to get access to multiple elements in the target Web page
  3. Test case makes the selector of the Test Object parameterized — it interpolates the place holder ${index} in the selector expression with concrete values: 1,2,3…,10

I would call this method Parameterized Test Object on the fly. This method works well. Provided that a tester has sound knowledge of XPath or CSS selector, this method is easy to implement.

I was inspired by the discussion by Katalon enthusiasts in the Katalon forum:

What the demo does

The demo verifies the texts displayed as the label of TABs in the target Web pages.

My Test Case is as follows:

import com.kms.katalon.core.model.FailureHandling as FailureHandling
import com.kms.katalon.core.testobject.ConditionType as ConditionType
import com.kms.katalon.core.testobject.TestObject as TestObject
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
def headerLogo = new TestObject('header_logo')
headerLogo.addProperty('xpath', ConditionType.EQUALS, '//div[@id="header_logo"]')
WebUI.verifyElementPresent(headerLogo, 10, FailureHandling.STOP_ON_FAILURE)
WebUI.verifyElementVisible(headerLogo, FailureHandling.STOP_ON_FAILURE)
def expected = ['WOMEN', 'DRESSES', 'T-SHIRT']
def engine = new groovy.text.SimpleTemplateEngine()
String template = '//div[@id="block_top_menu"]/ul/li[${index}]/a'
TestObject blockTopMenu = new TestObject('block_top_menu')
for (int i = 0; i < expected.size(); i++) {
	String expr = engine.createTemplate(template).make(['index':i+1]).toString()	WebUI.comment("expr=${expr}")
	blockTopMenu.addProperty('xpath', ConditionType.EQUALS, expr)
	String text = WebUI.getText(blockTopMenu, FailureHandling.STOP_ON_FAILURE)
	// check if displayed as expected
	WebUI.verifyEqual(text, expected[i], FailureHandling.CONTINUE_ON_FAILURE)

When I run the test case I got the following output in the log:

[PASSED] - Actual object 'WOMEN' and expected object 'WOMEN' are equal
 ...[PASSED] - Actual object 'DRESSES' and expected object 'DRESSES' are equal
 ...[FAILED] - Unable to verify equal between actual object 'T-SHIRTS' and expected object 'T-SHIRT'



Great work!

Upvoted. Sweet!

Upvoted. Nice job!

I could not find com.kms.katalon.core.testobject.TestObject#setProperty(). So I wrote as this:

        blockTopMenu.addProperty('xpath', ConditionType.EQUALS, expr)

addProperty ? It worked but looks strange.



I tested your code sample and it works great. I tried to adapt it to my script but I always get this message error:

2019-08-16 10:20:11.995 ERROR c.k.katalon.core.main.TestCaseExecutor - :x: Test Cases/Reto_ComprobanteContable/TC_EditarComprobante FAILED.
groovy.lang.MissingPropertyException: No such property: ConditionType for class: Script1565725786672

I debbuged my script and it always fail at this line:

              blockTopMenu.addProperty('xpath', ConditionType.EQUALS, expr)

I don’t know what am I doing wrong and I’ve tried everything to work with dynamic objects in Katalon. If you could help me, I’d appreciate that so much.


Please check if your test case script has the following line.

import com.kms.katalon.core.testobject.ConditionType 

Hi @kazurayam, I had that line in my code but I replaced it for this one (as in your sample code):
import com.kms.katalon.core.testobject.ConditionType as ConditionType
After that, the error message just disappeared (I didn’t understand why).

Thanks for your help! :slight_smile:

(I didn’t understand why).

You should. tells:

In order to refer to any class you need a qualified reference to its package. Groovy follows Java’s notion of allowing import statement to resolve class references.

Simple import
A simple import is an import statement where you fully define the class name along with the package. For example the import statement import groovy.xml.MarkupBuilder in the code below is a simple import which directly refers to a class inside a package.

// importing the class MarkupBuilder
import groovy.xml.MarkupBuilder

// using the imported class to create an object
def xml = new MarkupBuilder()

assert xml != null
1 Like