For loop: access looping variable as string

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
file.append(html);
}

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.

Koleman

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

Script1551459748107.zip (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 www.siacargo.com:
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.

is there any one who can help me out
that how For loop statement works in katalon ?

@Noor_Ahmed_Khan

Please create a new Topic for your issue. You can create it by clicking “+ New Topic” button at http://forum.katalon.com/

And have a look at the guidance

and provide enough information about your issue, please.

Kindly check out attachment, how to use variable in loop or from excel

In the code you have a line of WebUI.openBrowser('') inside a for loop. This looks odd. If your for loop repeats calling WebUI.openBrowser('') 10 times, then on your desktop you will have 10 browser windows opened. Do you really want to do it? This must be a mistake. Isn’t it?

I don’t see your question.

Which “variable” in your test case script are you talking about?

  • def i
  • userid

Let me assume you are talking about the i variable, then see the following code:

import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI

for (def i = 0; i <= 10; i++) {
	WebUI.comment("i is " + i)
}

This works just fine. This is a valid way how to use i.

So, what is your question?

Thank you for your reply

Example
Open bowers
Navigate URL
Enter Login ID

I wan to run this case 5 times, and every time login email will be changed (Don’t want to run test suit) want to use for loop statement.

We need to understand your question more precisely. Before talking about how to loop, we need to understand a simpler case without any loop.

Could you complete your test case script which does

  • Open Browser
  • Navigate to a URL
  • Enter Login ID

for 1 pair of URL + LoginID (no loop), and share your code.

Then, based on that working script, we would be able to advise you how to modify your script to process multiple pairs.

Hello
Step 1
for (def i = 1; i <= findTestData(‘file_name’).getRowNumbers(); i++) {

is there any way to print value of i after running of step 1

int numOfRows =findTestData(‘file_name’).getRowNumbers()
println "numOfRows=${numOfRows}" 
for (def i = 1; i <= numOfRows; i++) {

Thank you for your response , actually I am getting total rows count

example :
Excel sheet:
1 A
2 B
3 C

Now I want to know that , when loop is running , what value it stored in def i
for (def i = 1; i <= numOfRows; i++) {

I don’t understand your post. Sorry, I would quit.

Actually I want to print value what ever store in loop
Example :

for (def i = 1; i <= findTestData(‘file_name’).getRowNumbers(); i++) {
in excel Row 1= Noor Khan
in excel Row 2= Kazurayam

now when loop runs so what value is stored in i , i want to print that value 1st, then remaining case run

So you do not want to print the value of i, but you want to print some data from your data file? i is just a counter. it will change from 1 to the number of rows in your data file.