Count text (by index order) and store in variable

Hey community,

I have a challenge… Or well, at least for me it is a challenge. Perhaps this is something easy for you people.

My case;
I have a page with multiple blocks which are “requests of a course”.
(In a previous flow I have created a specific course called Course1).
My goal is to cancel Course1. However on the same page, I have multiple blocks that also have the cancel buttons (These are courses from another subject and I can not remove them as they are used by others…).
This means I have multiple cancel buttons. The element of the buttons are exactly the same apart from the ID which is ordered by Index Number. ID = CancelCourse_0, CancelCourse_1, etc…)
Also, when I cancel a course…The canceled course stays there… Meaning my index only get’s higher as the new course that shows up, doesn’t pop on the top, instead of pops up on the bottom of the subject’s course.

Solution?:
So I was thinking, if I count the subject of this specific course on page (which is unique to the course).
And then concatenate the first part of the element’s ID + the number of counts of the subject course.
I put this into a variable which I put into an Object with the ${function}
What is important for this, is that the count number starts with 0.

I just have no clue how I could do this, or if this is even possible.

Anyone here that could help me out with this? :slight_smile:
Or perhaps have an easier solution?

Got it to work with the method I was looking for.

Solution:

import com.kms.katalon.core.webui.common.WebUiCommonHelper

countOfElements = WebUiCommonHelper.findWebElements(findTestObject(‘OBJECT’), 5).size()
IndexNumber = countOfElements - 1
Cancel = ‘cancelButton_’ + IndexNumber
WebUI.click(findTestObject(‘OBJECT’, [‘Cancel’ : “${Cancel}”]))

And of course ${Cancel} is in the object itself.

1 Like