How to loop through arrays?

Hi,
I want this function to loop through this list of assertions, checking the first parameter against the second (e.g. ‘Type of call’ against ‘Type of call’):

So far I have been able to pass through the first assertion (assert tableHeaders.get(0).equals(Arrays.asList('Type of call', 'Type of call'))

But as shown here, it stops at the second assertion [1], because the expected parameters are ‘Outline’ and ‘Level of Complexity’, whereas it returns ‘Type of call’, ‘Type of call’:

I think it is because I’ve hard-coded the xpaths in the function, to check one specific xpath e.g. (//tbody/tr[2]/th).

I don’t know how to iterate through so that it checks checks one by one, e.g. checks ‘Type of call’ against ‘Type of call’, then moves on and checks ‘Outline’ against ‘Level of Complexity’…etc.

Here is the function :

	@Keyword
def List<List<String>> getScoreOverTimeHeaders(TestObject to) {
	List<List<String>> tableHeaders = new ArrayList<>()
	WebElement table = WebUI.findWebElement(to, 10)
	List<WebElement> minorHeader = table.findElements(By.xpath("//tbody/tr/th")) //number of arrays
	for(WebElement header : minorHeader) {
		List<String> subHeaderStrings = new ArrayList<>()
		WebElement subHeader = header.findElement(By.xpath("//tbody/tr[2]/th")) //gets text of element[0] in the array
		subHeaderStrings.add(subHeader.getText())

		List<WebElement> subSubHeaders = header.findElements(By.xpath("//tbody/tr[3]/th")) //gets text of element[1] in the array
		for(WebElement subSubHeader : subSubHeaders) {
			subHeaderStrings.add(subSubHeader.getText())

		}
		tableHeaders.add(subHeaderStrings)
	}
	return tableHeaders
}

I have tried changing the xpaths to //tbody/tr/th rather than //tbody/tr[3]/th etc, but it just returns all the text in the table, which I don’t want.

Does anyone know what I could do?

Well, I looked at other sites and perhaps it’s how you set your for loop up:

The above site suggests:
for (header in minorHeader) {

1 Like

Thanks for the help. I’m going to see how I can refactor it in a different way, since the one who wrote the code has left now :slight_smile: