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.
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);
}
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!
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.
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?
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.