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 :
- Test case creates a Test Object instance on the fly without storing it into the Object Repository
- Test case reuses the Test Object instance multiple times to get access to multiple elements in the target Web page
- 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
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:
[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'