I have made a demo at
How to run the demo
- download the zip file of this project from the Releases page, unzip it.
- start Katalon Studio, open the project
- open
Test Cases/TC1_iterate_over_Katalon_Discussions
, and run it with any browser
Input to the demo
The TestCase opens http://forum.katalon.com/discussions. There you will find a list of 37 discussions.
Output from the demo
The TestCase iterates over the list of discussions and print the titles like this:
10-07-2018 01:33:23 PM - [INFO] - >>> 1: Visual Testing In Katalon Studio...10-07-2018 01:33:23 PM - [INFO] - >>> 37: Query on: Running remotely and paralel thread, Merging teams code in Git,Ex/Import of KatalonProject
How the demo works
TestObjects made
- [
Page_Recent Discussions/Content.ul_class-Disscussions
] with xpath
//div[@id='Content']/div/ul
- [
Page_Recent Discussions/Content.(li_Item)
] with xpath
//li
- [
Page_Recent Discussions/Content.(a_Title)
] with xpath
//div[contains(@class, 'Title')]/a
TestCase made
I made 1 TestCase TC1_iterate_over_Katalon_Discussions
. I would copy its portion and past it here:
WebUI.navigateToUrl('http://forum.katalon.com/discussions')
WebDriver driver = DriverFactory.getWebDriver()// read XPath fragments
String ulSelector =
findTestObject('Object Repository/Page_Recent Discussions/Content.ul_class-Discussions').findPropertyValue('xpath')
String liSelector =
ulSelector + findTestObject('Object Repository/Page_Recent Discussions/Content.(li_Item)').findPropertyValue('xpath')
// obtain the number of Discussion rows
int rowCount = driver.findElements(By.xpath(liSelector)).size()
WebUI.comment(">>> rowCount=${rowCount}")// iterate over Discussions in the Forum
for (int pos = 1; pos <= rowCount; pos++) {
// selector to the Title of each Discussion
String titleSelector = liSelector + "[position()=${pos}]" +
findTestObject('Object Repository/Page_Recent Discussions/Content.(a_Title)').findPropertyValue('xpath')
TestObject to = new TestObject().addProperty('xpath', ConditionType.EQUALS, titleSelector)
WebUI.verifyElementPresent(to, 1)
String title = WebUI.getText(to)
WebUI.comment(">>> ${pos}: ${title}")
}
...
The following line is the core part of this script:
String liSelector =
ulSelector +
findTestObject('Object Repository/Page_Recent Discussions/Content.(li_Item)').findPropertyValue('xpath')
...
for (int pos = 1; pos < rowCount; pos++) {
String titleSelector =
liSelector +
"[position()=${pos}]" +
findTestObject('Object Repository/Page_Recent Discussions/Content.(a_Title)').findPropertyValue('xpath')
...
}
...
Generated tilesSelectors will be as follows:
//div[@id='Content']/div/ul//li[position()=1]//div[contains(@class, 'Title')]/a
//div[@id='Content']/div/ul//li[position()=2]//div[contains(@class, 'Title')]/a
//div[@id='Content']/div/ul//li[position()=3]//div[contains(@class, 'Title')]/a
//div[@id='Content']/div/ul//li[position()=4]//div[contains(@class, 'Title')]/a
âŚ//div[@id='Content']/div/ul//li[position()=37]//div[contains(@class, 'Title')]/a
Conclusion
- In the demo I used TestObject just as an Envelope of XPath selector literal.
- TestObject is designed primarilly for the Katalon Studio features: âRecord Webâ and âSpy Webâ. But you can modify/refactor the generated TestObject manually, and it is highly recommended.
- I had no XPath literals hard-coded and scattered in the sample TestCase scripts. All the XPath literals are externalized into the Object Repository. This would make code maintenance easier. It is good to have magical values centralized in the Object Repository. It would be nightmare if you scatter XPath literals all over the TestCase scripts.
- This sample shows you that you can introduce âsearching for child elementsâ and âsomething dynamicâ by a bit tricky coding. You construct XPath literals dynamically for yourself using the XPath fragments stored in the TestObjects as Envelope.
Beginners might not be able to use this approach, but well-trained programmers like you would easily see this idea.
Thanks to Marek Melocik for the original idea. It was posted at