Loop to printing out text of every instance of an element

I have a practice test where I can validate the number of elements by Xpath identifier, but I want to also print out the text of the elements the test is ‘seeing’. Right now my code is something like this:

for (int i = 0; i < textCount ; i++){

** def** validateText = WebUI.getText(dynamic_Text[i])

print(validateText)

}

Basically, I want the for loop to through every instance of the specified element and then print out each instance of it [i]. At the moment, all I am seeing printed out is the 1st instance of the element, instead of cycling through all of them. Suggestions?

This page has a nice example regard to looping through elements, please try it first:
https://docs.katalon.com/pages/viewpage.action?pageId=12419075

(2..5).each{  WebUI.check(findTestObject('chk_Title', [('index') : it])) }

I tried to apply that example to a getText method:

(0…2).each

{

def validateText = WebUI.getText(findTestObject(‘dynamicText’, [(‘index’) : it]))

println(validateText)

}

The test passes, but looking at the console, the validateText value that is prints is the same for all 3 instances. However, I am expecting it to print out the 3 different blocks of text that I am validating. Any other suggestions?

Sam,

Could you show us how your test object ‘dymanicText’ is defined?

I figured it out using Selenium instead of WebUI:

def textObject = driver.findElements(By.xpath([path-to-element]))

for (int i = 0; i < textObject.size() ; i++){

print textObject[i].text

}