For loop: access looping variable as string

Very basic question here from a newbie. I am trying to loop over some clicks. I had hoped to automate this via a for loop, but I need to access the looping variable to call the correct page. In the code snippet below I am trying to access,
Object Repository/Page_Real Estate - Search By Name/a_2
Object Repository/Page_Real Estate - Search By Name/a_3
Object Repository/Page_Real Estate - Search By Name/a_4

What I cannot figure out is how to call the looping value in my findTestObject statement (I also need to use it for a definition as well: I am appending all of the results into one file download). I tried r$ (where r is the looping variable) which is clearly wrong. I also attach the full script. Any help on this would be wonderful!

code snip:
for (int r = 2; r <= 9; r++){‘Object Repository/Page_Real Estate - Search By Name/a_r$’))
def htmlr$ = driver.getPageSource()
new File(‘myresults.html’).append(htmlr$)
} (966 Bytes)

Scripts are written in the Groovy/Java language, so you must follow that syntax:

for (int r = 2; r <= 9; r++){'Object Repository/Page_Real Estate - Search By Name/a_' + r));

Also, can you elaborate on the:

… step? What I’m guessing is that you are trying to get all of the HTML for the page that appears after clicking each Test Object, and write that data to a file. If that’s the case, the rest of the code in your loop will not do what you’re trying to do. Let me know, and I can give you the correct way to do this.

Thanks so much Brandon for letting me know how to access the loop variable ®! I am away from my code right now but will test it on Monday.

  1. is there a way to include that in a variable name: in the fourth line of my code I have,
    def htmlr$ = driver.getPageSource()
    where I want to append the loop variable at the end not r$.
    I am doing this since the same variable (my temporary variable, html, holding the page content) cannot be defined twice.

  2. the remainder of the code in the loop works when I write out each step of the loop, but if there is another way (no doubt better) please let me know! You are correct I a trying to get all of the links (in my case about 6) and continually append them to one big file.


No. Variable names are always explicitly defined.

Why would you want it to be defined twice? If you want to store the the HTML for each iteration, you should use an ArrayList.

You will need to use a class like PrintWriter or similar to actually write data to the file. Your approach isn’t right for a couple reasons:

1.) You’re creating a new File object inside the for loop, which means it would create a new file for each URL.
2.) Unless you are using some kind of non-java File object, there’s no append() method.

OK thanks Brandon I will give it a whirl on Monday and report back.

Just one small follow-up (perhaps for another time):
If I want to create a variable with the loop variable ( r) in it,
def mynewvar_r = …
how would that be done? In other words how to get the value of r at the end?

And thanks so much for your patience. I apologize for these simple questions, I have literally been using Katalon for two weeks.

Hi @cigar

The short answer is you can’t do that.

The longer answer is that you can’t do that, and you should really think about why do you need to do it.

A variable must have a predefined name, or in other words be statically named, is because code should be separated from data. Data are dynamic and can be whatever they need to be, we also want them to be this way.

However, this dynamicity of data is so overwhelming that we human need a more stable and concrete representation of it - which is code, to represent as many behaviors (that result in data) as possible with as few lines of code as possible.

If you break this then lots of complications will arise and it’s not something you want. This rule prevents real consequences that you may not be aware of, but undoubtedly real.

I haven’t understood your rationale for doing this, can you elaborate ? Why do you say that the same variable cannot be defined twice ? If your goal is:

Then a variable representing the content of the HTML file you retrieve within each iteration should be enough to achieve it. I suspect that you think it’s a rule that you can’t define two variables with the same name in different lines of code within the same script. This is indeed true. But like I said the code (for loop in this case) is separated from the data (individual iteration in this case), therefore you have not defined two variables in different places when you’re using:

in the for loop, you simply define a place holder called htmlr$ (terrible name btw) which will hold data that comes in at every iteration.

Now I notice within the for loop you wrote this:

And you want to append html contents from all iterations to one big file. The above code will not achieve that, instead in each iteration it will create a new file and append the content from that iteration, and in the next iteration this file is created again (possibly by overriding the file from the previous iteration).

The best case scenario by using the above code is that you will have a file called myresults.html that will have the content of the page of the last iteration, not the accumulated content of the page after clicking all the test objects like you want.

You need to create a file outside of the for loop to avoid re-defining the file within each iteration, and then in the for loop you simply call append() on this file. An example may be:

File file = new File("myresults.html");

for(int i i = 2; i <= 9; i ++){
// ....... etc
// The following line will get the html content and store it into variable html
def html = driver.getPageSource()
// The following line append the html content to the defined file

Hope that helps.

Regards !

Thanks Thanh!!!
I had thought that continually defining html in the for loop as in your code have would cause problems. Hopefully not!

Anyway I will give this a try tomorrow! I appreciate the very detailed response.


Apologies for the slow reply: I got this running thanks to everyone for all of your assistance! I attach my script for anyone encountering similar issues in the future (there are two for loops at the end, and the first could have been made a special case of the second one).

As an aside I was told that my approach to appending files would not work, but I did not have any issues. The code loops through all of the search results and appends all of them into one output file call “myresults.html”

At any rate thanks all for the assistance, This is my first time using Katalon, hopefully I will build up some mastery as I go!

Koleman (1.1 KB)

1 Like

Hello, can you help me with this. Im still new to this katalon, and I dont have any idea for to use for loop on this scenario.

I want to check that the following function is available in
Track Shipment - clicking on this can open a new page, with the following fields available:
‘Air Waybill 1’ – Text box with default value “618” followed by empty text box.
‘Air Waybill 2’ – Text box with default value “618” followed by empty text box.
‘Air Waybill 3’ – Text box with default value “618” followed by empty text box.
‘Air Waybill 4’ – Text box with default value “618” followed by empty text box.
‘Air Waybill 5’ – Text box with default value “618” followed by empty text box.

@soclabador Although you could use a loop to review the text fields, you may not need to use a for loop. Each textbox on the “Track Shipment” page has an unique id that could be used to identify each box.

However, if you want to use the for loop, then you will need to use it on a List< WebElement> object. An example is below:

Notice how a String aString variable is declared inside the parentheses of the for loop. For each iteration of the for each loop this variable will be bound to one of the String objects from the strings array. The for each loop will repeat once for each element in the strings array.