Andrew,
I thought I can abstract your requirement to a simpler case. You just want to iterate over a elements and elements of a : 2 dimensional iteration.
Then there is a much simpler approach. You do not need to study org.openqa.selenium.WebElement. You just want to use Parameterized Test Object in Katalon Studio.
The Web Page as testbed is like this:
<html>
<body>
<table class="table">
<thead>
<tr><th>found</th><th>A</th><th>B</th><th>C</th></tr>
</thead>
<tbody>
<tr><th>1</th><td>A1</td><td><span>B1</span></td><td><a href="#">C1</a></td></tr>
<tr><th>2</th><td>A2</td><td><span>B2</span></td><td><a href="#">C2</a></td></tr>
<tr><th>3</th><td>A3</td><td><span>B3</span></td><td><a href="#">c3</a></td></tr>
</tbody>
</table>
</div>
</body>
</html>
I made a test case is like this:
import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject
import com.kms.katalon.core.model.FailureHandling as FailureHandling
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
WebUI.openBrowser('')
WebUI.setViewPortSize(703, 347)
WebUI.navigateToUrl('http://demoaut-mimic.kazurayam.com/7520_simpler_testbed.html')
WebUI.verifyElementPresent(findTestObject('Page_Discussion 7520/table'),
10, FailureHandling.STOP_ON_FAILURE)
List<List<String>> expectedContents = [
["A1", "B1", "C1"],
["A2", "B2", "C2"],
["A3", "B3", "C3"],
]
for (int i = 0; i < expectedContents.size(); i++) {
for (int j = 0; j < expectedContents[i].size(); j++) {
String cellValue = WebUI.getText(findTestObject(
'Page_Discussion 7520/cell', ['trIndex':i + 1, 'tdIndex':j + 1]))
WebUI.verifyEqual(expectedContents[i][j], cellValue)
}
}
WebUI.closeBrowser()
I wanted to check if the Web page displays A1, B1, C1, A2, B2, C2, A3, B3, C3 in table as I wrote in the testcase.
I made a Test Object ‘Page_Discussion 7520/cell’ like this:
Please note this Test Object has such XPath as selector:
//table/tbody/tr[${trIndex}]/td[${tdIndex}]
${trIndex} is the place holder for the index of the
elements in the
.
${tdIndex} is the place holder for the index of the .
These place holders will be interpolated by the Test Case runtime.
When executed, I got following log:
[INFO] - Found 1 web elements with id: 'Object Repository/Page_Discussion 7520/cell' located by 'By.xpath: //table/tbody/tr[1]/td[1]' in '30' second(s)
...
[PASSED] - Text of object 'Object Repository/Page_Discussion 7520/cell' is: 'A1'
...
[INFO] - Found 1 web elements with id: 'Object Repository/Page_Discussion 7520/cell' located by 'By.xpath: //table/tbody/tr[1]/td[2]' in '30' second(s)
...
[INFO] - Found 1 web elements with id: 'Object Repository/Page_Discussion 7520/cell' located by 'By.xpath: //table/tbody/tr[1]/td[3]' in '30' second(s)
...
[PASSED] - Text of object 'Object Repository/Page_Discussion 7520/cell' is: 'C1'
...
[INFO] - Found 1 web elements with id: 'Object Repository/Page_Discussion 7520/cell' located by 'By.xpath: //table/tbody/tr[3]/td[3]' in '30' second(s)
...
[FAILED] - Unable to verify equal between actual object 'C3' and expected object 'c3' (Root cause: Actual object 'C3' and expected object 'c3' are not equal)
I expected ‘C3’ but the page displayed ‘c3’, therefore the test case failed.
You can clone out this demo form GitHub repository: GitHub - kazurayam/KatalonDiscussion7520 and see Test Case ‘TC2’
# Disucssion
Paremetarizing feature of Test Object is flexible enough; You can make more deeply nested iteration. You can write as many dimensional parameterized XPath expression as you want.
For example, if you have 2 or more in the target page, you need 3 dimensional parameterized XPath like this:
//table[$tblIndex]/tbody/tr[${trIndex}]/td[${tdIndex}]
or if you have a table which contains inner tables, you need 4 dimensional parameterized XPath like this:
//table[@id="T1"]/tbody/tr[${outerTrIndex}]/td[${outerTdIndex}]//table/tbody/tr[${innerTrIndex}]/td[${innerTdIndex}]
# Performance issue
The above approach has a performance issue. It make frequent call to WebUI.getText(TestObject). Every single WebUI.getText() makes a request/reply session between Katalon Studio and Browser. It can be very slow.
If you find this approach too slow, then you should look at using
List WebUI.findWebElements(TestObject)
https://api-docs.katalon.com/com/kms/katalon/core/webui/keyword/WebUiBuiltInKeywords.html#findWebElements(TestObject,%20int)
testobject_cell.png
7520.PNG