# Parameterized Test Object on the fly

**URL:** <https://forum.katalon.com/t/parameterized-test-object-on-the-fly/11542>\
**Category:** Web Testing\
**Tags:** tips-tricks, katalon-studio\
**Created:** [June 15, 2018, 4:37am UTC](https://forum.katalon.com/t/parameterized-test-object-on-the-fly/11542 "2018-06-15T04:37:12Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![kazurayam](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/kazurayam/32/333_2.png) [@kazurayam](https://forum.katalon.com/u/kazurayam)\
**Post date:** [June 15, 2018, 4:37am UTC](https://forum.katalon.com/t/parameterized-test-object-on-the-fly/11542/1 "2018-06-15T04:37:12Z")

</div>

I have made a demo here:

> **[GitHub - kazurayam/UsingParameterizedTestObjectOnTheFly](https://github.com/kazurayam/UsingParameterizedTestObjectOnTheFly)**
>
> Contribute to kazurayam/UsingParameterizedTestObjectOnTheFly development by creating an account on GitHub.

## 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:

- [Katalon Forum/Using waitForElementPresent without an object](http://forum.katalon.com/discussion/5852/using-waitforelementpresent-without-an-object)

**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:

```auto
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
WebUI.openBrowser('')
WebUI.navigateToUrl('http://automationpractice.com/index.php?')
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)
	WebUI.comment("text=${text}")
	// check if displayed as expected
	WebUI.verifyEqual(text, expected[i], FailureHandling.CONTINUE_ON_FAILURE)
}
WebUI.closeBrowser()

```

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

```auto
[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'

```

[WOMEN\_DRESS\_T-SHIRTS.png](https://s3.amazonaws.com/katalon-forum/editor/qg/0zblzxp9qxbs.png)

---

<div class="post-metadata">

**Author:** ![Mate\_Mrse](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/mate_mrse/32/345_2.png) [@Mate\_Mrse](https://forum.katalon.com/u/Mate_Mrse)\
**Post date:** [June 15, 2018, 6:28am UTC](https://forum.katalon.com/t/parameterized-test-object-on-the-fly/11542/2 "2018-06-15T06:28:22Z")

</div>

Great work!

---

<div class="post-metadata">

**Author:** ![Russ\_Thomas](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/russ_thomas/32/11884_2.png) [@Russ\_Thomas](https://forum.katalon.com/u/Russ_Thomas)\
**Post date:** [June 15, 2018, 1:46pm UTC](https://forum.katalon.com/t/parameterized-test-object-on-the-fly/11542/3 "2018-06-15T13:46:57Z")

</div>

Upvoted. Sweet!

---

<div class="post-metadata">

**Author:** ![Don\_Welsh](https://avatars.discourse-cdn.com/v4/letter/d/b5e925/32.png) [@Don\_Welsh](https://forum.katalon.com/u/Don_Welsh)\
**Post date:** [June 15, 2018, 2:28pm UTC](https://forum.katalon.com/t/parameterized-test-object-on-the-fly/11542/4 "2018-06-15T14:28:15Z")

</div>

Upvoted. Nice job!

---

<div class="post-metadata">

**Author:** ![kazurayam](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/kazurayam/32/333_2.png) [@kazurayam](https://forum.katalon.com/u/kazurayam)\
**Post date:** [June 15, 2018, 9:44pm UTC](https://forum.katalon.com/t/parameterized-test-object-on-the-fly/11542/5 "2018-06-15T21:44:47Z")

</div>

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

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

```

addProperty ? It worked but looks strange.

---

<div class="post-metadata">

**Author:** ![melissaQA](https://avatars.discourse-cdn.com/v4/letter/m/87869e/32.png) [@melissaQA](https://forum.katalon.com/u/melissaQA)\
**Post date:** [August 16, 2019, 4:46pm UTC](https://forum.katalon.com/t/parameterized-test-object-on-the-fly/11542/6 "2019-08-16T16:46:13Z")

</div>

Hi,

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 - ❌ Test Cases/Reto\_ComprobanteContable/TC\_EditarComprobante FAILED.  
Reason:  
groovy.lang.MissingPropertyException: No such property: ConditionType for class: Script1565725786672  
at TC\_EditarComprobante.run(TC\_EditarComprobante:46)

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.

---

<div class="post-metadata">

**Author:** ![kazurayam](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/kazurayam/32/333_2.png) [@kazurayam](https://forum.katalon.com/u/kazurayam)\
**Post date:** [August 16, 2019, 5:11pm UTC](https://forum.katalon.com/t/parameterized-test-object-on-the-fly/11542/7 "2019-08-16T17:11:41Z")

</div>

@melissaQA

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

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

```

---

<div class="post-metadata">

**Author:** ![melissaQA](https://avatars.discourse-cdn.com/v4/letter/m/87869e/32.png) [@melissaQA](https://forum.katalon.com/u/melissaQA)\
**Post date:** [August 20, 2019, 4:33pm UTC](https://forum.katalon.com/t/parameterized-test-object-on-the-fly/11542/8 "2019-08-20T16:33:33Z")

</div>

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! 🙂

---

<div class="post-metadata">

**Author:** ![kazurayam](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/kazurayam/32/333_2.png) [@kazurayam](https://forum.katalon.com/u/kazurayam)\
**Post date:** [August 20, 2019, 7:49pm UTC](https://forum.katalon.com/t/parameterized-test-object-on-the-fly/11542/9 "2019-08-20T19:49:39Z")

</div>

> (I didn’t understand why).

You should.

[The Apache Groovy programming language - Program structure](http://www.groovy-lang.org/structure.html) tells:

> **Imports**  
> 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.

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

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

assert xml != null

```

---

<div class="post-metadata">

**Author:** ![albert.vu](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/albert.vu/32/20595_2.png) [@albert.vu](https://forum.katalon.com/u/albert.vu)\
**Post date:** [August 8, 2022, 7:40am UTC](https://forum.katalon.com/t/parameterized-test-object-on-the-fly/11542/10 "2022-08-08T07:40:10Z")

</div>

Archived on August 8, 2022, 7:40am UTC


