Get loop value when condition is true in robotframework - for-loop

I am new to Robot Framework, I want to get loop value when the condition is true. Below I mention sample code.
FOR ${INDEX} IN RANGE 1 10
Run Keyword If ${INDEX}==5
END

You are missing the call to a keyword, and I assume you don't want to continue the loop after it, so for example:
FOR ${INDEX} IN RANGE 1 10
Run Keyword If ${INDEX}==5 Run Keywords No Operation Exit For Loop
END
Log For loop variable is ${INDEX}

Related

Robot Framework, how to get the value of odd row table from the Range

I have a new task, I need to get the generated table first then from that table, I need to validate the odd rows of the table
So far, this is my script
${Count} get element count ${Accounting_Table_Row}
FOR ${i} IN RANGE 999999
Exit For Loop If ${i} == ${Count}
${M} get text
xpath=//div/[#id'GeneratedTable']/table/tr[${count}]
Run Keyword if "${M}" != "0" fail transaction is not validated ELSE pass execution
transaction is validated
END
I get all the generated table but I only need the odd row table. How can I get it?
We use the Python module operator (remaining of division). Here is an example:
*** Test Cases ***
cicle
${Count}= Set Variable ${55}
FOR ${i} IN RANGE ${Count}
${Oddness}= Set Variable If ${i%2} != 0 "Odd" "Even"
Log To Console var=\${i}=${i} is ${Oddness}
END

How to iterate For loop until certain condition meets?

I need to iterate for loop till certain condition meets in Robot Framework.
${counter}= Set Variable 1
:FOR ${item} IN RANGE ${counter}
\ Check condition
\ ${counter} = ${counter} + 1
Is it possible to increase the ${counter} variable value here?
Yes.
${counter}= Set Variable 1
FOR ${item} IN RANGE 1 20
${counter}= Evaluate ${counter} + 1
Log To Console ${counter}
Exit For Loop If ${counter} == 10
END
And FOR loops can be exited using Exit For Loop or Exit For Loop If keywords. Keywords documentation.
EDIT after comments.
You're asking about a while loop. Robot doesn't have a while loop. There's simply no support for it, and robot probably won't support it until at least 2021. The only looping construct is a for loop.
You seem to have an aversion to setting a limit of 20, but there must be a practical limit to the number of iterations, whether it's 1,000, 10,000, or 1 million or more. Just use a FOR loop that has a huge upper limit, and for all intents and purposes you've created a while loop.
FOR ${item} IN RANGE 1000000
Exit FOR loop if <some condition>
${counter}= evaluate $counter + 1
END
While it doesn't look quite as pretty as While <some condition>, the end result will be the same, assuming your condition becomes true at some point before one million iterations.

"for var = 1, 2" in Lua

What does this kind of loop do in Lua?
for count = 1, 2 do
-- do stuff
end
The variable count isn't used in the body of the loop.
It executes the body of the loop twice.
There's no need to refer to count inside the loop unless you need to know its current value.
for count = 1,5 do
print("Hello")
end
prints
Hello
Hello
Hello
Hello
Hello
In this case count is "dummy variable" - "dummy" in that a variable is used fulfill a certain construct even though the variable is not used. (Another common name for such a usage is _, although count arguably adds a little more semantic intent.)
Such a dummy variable is used because LUA loops require a variable / assignment in the grammar construct. However, there is no requirement that the variable is used - hence a "dummy".
.. A numeric for [loop] has the following syntax:
for var=exp1,exp2,exp3 do
something
end
That loop will execute something for each value of var from exp1 to exp2, using exp3 as the step to increment var. This third expression [exp3] is optional; when absent, Lua assumes one [1] as the step value.

How to escape from a recursive method in ruby

I created a recursive function that tries to parse the information from the parsed list. It's kind of hard to explain, but it's something like
In a parse function that parses either a wikipedia Movie page or an Actor page, starts by parsing a filmography list from a wikipedia actor page -> call the same function on the parsed list -> repeat
I set a global variable that counts the number of iterations, but when I try to break out from the function and move on to the next step by doing,
if $counter > 10
return nil
end
but it does not immediately ends since there are still functions to be called left (since it's recursive). I tried to use "abort" but this one just terminated the program instead of moving on to the next one.
Is there a way to immedately stop the recursive run and move on to the next step without aborting the program?
A bit hard to answer without more code. But i guess you looking for next or break to jump out of recursiveness.
next
Jumps to the next iteration of the most internal loop. Terminates execution of a block if called within a block (with yield or call returning nil).
for i in 0..5
if i < 2 then
next
end
puts "Value of local variable is #{i}"
end
Result:
Value of local variable is 2
Value of local variable is 3
Value of local variable is 4
Value of local variable is 5
break
Terminates the most internal loop. Terminates a method with an associated block if called within the block (with the method returning nil).
for i in 0..5
if i > 2 then
break
end
puts "Value of local variable is #{i}"
end
Result:
Value of local variable is 0
Value of local variable is 1
Value of local variable is 2

Confused by this unless statement in rubykoans

Lines 4 & 5 are causing me grief:
1 def test_break_statement
2 i = 1
3 result = 1
4 while true
5 break unless i <= 10
6 result = result * i
7 i += 1
8 end
9 assert_equal 3628800, result
10 end
I'm not sure what needs to remain true in the while true statement, however I believe it is the code that follows it. This leads to further confusion because I am reading the line:
break unless i <= 10 as break if i is not smaller or equal to 10. What procedure is this code going through ie how does the while and break statements interplay. I think I am nearly there but can't put the process in my head. Thanks.
The code will break out of the endless while loop when i is greater than 10.
But I'm not sure why the condition isn't checked in the while statement.
Edit: Had I read the method name I would have understood why the condition isn't checked directly with the while statement. The method's purpose is to test the break statement.
while statements test whatever comes after the word while. If the expression that follows them is true they execute the code within the loop. If the expression is false, they do not.
Thus, as other posters have pointed out, while true will always execute the code within the loop. Luckily for your code there is a break statement within the loop. If there wasn't, the loop would run forever and you'd have to kill the process running your program.
In your code sample the break keyword is followed by unless which means that it will break the loop unless the expression following it is true. Your code will break out of the loop when i is greater than 10.
while true is an infinite loop. break, when executed, will exit it immediately, to continue with the first line after it (assert_equal...).
In this specific case (nothing intervening between while and break unless), it is equivalent to this:
while i <= 10
result = result * i
i += 1
end
while true it is endless loop.
break unless i <= 10 is same as break if i > 10 it will break that loop if i is smaller or equal to 10

Resources