Running a block of code in until loop Robot Framework - robotframework

I have a problem in writing a loop in Robot Framework for a block of code.
This code firstly check some values (Minimum and Current), then compare them, and then increase another value (Quantity) by input text. I would like this block of code to be executed UNTIL the condition that Current is greater than Minimum is fulfilled.
How should I write such kind of condition?
Thanks in advance.
${Minimum}= Get Table Cell xpath=... 5 3
${Current}= Get Table Cell xpath=... 5 4
${status} ${value}= Run Keyword And Ignore Error
... Should be true ${Current} > ${Minimum}
${quantity}= Get Value xpath=
... Run Keyword If '${status}' == 'FAIL'
... Input Text xpath=${quantity+10}

Ok, I manage to do this with simple FOR loop and EXIT FOR LOOP in ELSE condition.
: FOR ${i} IN RANGE 1 999
${BoxesMinimum}= Get Table Cell xpath=//someid 5 3
${BoxesCurrent}= Get Table Cell xpath=//someid 5 4
${status} ${value}= Run Keyword and Ignore Error
... Should be true ${BoxesCurrent} > ${BoxesMinimum}
${quantity}= Get Value xpath=//someid
Run Keyword If '${status}' == 'FAIL'
... Input Text xpath=//someid ${quantity+10}
... ELSE Exit for loop

Related

Assigning to variables inside Run Keyword If

\ Run Keyword If ${i} == 7 log to console Testing Variant 1
\ ${is visible}= Run Keyword And Return Status Element Should Be Visible (//li[#class='_8HqL0'])[${i}]
\ Run Keyword If ${is visible} Run keywords
\ ... Scroll Element Into View (//li[#class='_8HqL0'])[${i}]
\ ... AND Click Element (//li[#class='_8HqL0'])[${i}]
\ ... AND sleep 2s
\ ... set variable ${Ad_Path} Get Text //*[#class='rui-3blDo _1Uh38 _27AdP']
\ ... AND log to console ${Ad_Path}
Hi i want to use the GET TEXT activity inside a IF block in FOR but its giving an error stating keyword name cannot be empty help
You can't have a construct ${variable}= Returned Value From Keyword inside a Run Keyword/Run Keyword If because the latter expects everything passed to it to be a keyword - and it considers ${variable} also to be one.
There is one "workaround" - Run Keyword If propagates back up the last returned value in its keyword, and that can be set to a variable. E.g. you can do this:
${variable}= Run Keyword If ${condition} Returned Value From Keyword ELSE Set Variable other value
Mind the ELSE in this construct - without it, if the condition is false the variable will be undefined - will be left with a value None (the data type).
Naturally, if the Run Keyword If has more than one steps (like your console logs) you'll have to break it up - a block that "does things", and another (or others) that "assigns values".
I've typed "workaround" in quotes because it isn't really such - it's the way the keyword is designed to be used.

How to loop on a table column?

Badly need your help. In Vehicle ID column, I would like to find a vehicle and once it finds it, it will click on it... How can I do that in Robot Framework? What approach should i be using?
For looping in table you can use xpath easily:
*** Test Cases ***
Stackoverflow Test
[Tags] #InDevelop
Go To https://www.w3schools.com/html/html_tables.asp
Wait Until Element Is Visible id=customers ${global_timeout}
:FOR ${index} IN RANGE 2 8
\ Wait Until Element Is Visible xpath=//*[#id="customers"]/tbody/tr[${index}]/td[1] ${global_timeout}
\ ${var} = Get Text xpath=//*[#id="customers"]/tbody/tr[${index}]/td[1]
\ Log ${var}
In order to click the correct element, you probably need to compare the variable content with your expected value. That can be done this way:
${areYouMyLine} = Run Keyword and Return Status Should Be Equal As Strings ${var} Island Trading
Run Keyword If ${areYouMyLine} Click Elementxpath=//*[#id="customers"]/tbody/tr[${index}]/td[1]
And also don't forget to Exit your For Loop since you found your element.
Exit For Loop
However this is not the best practice. You probably should go to your product team asking for some data attributes which will help you to find your line. Alternatively, if you know your table content, put it into a list and use For In Loop instead. Good stuff about loops can be found here: https://blog.codecentric.de/en/2013/05/robot-framework-tutorial-loops-conditional-execution-and-more/

How to use run a keyword if testcasename is equal to abc in robotframework

I know how can we use run keyword if specific condition is met but i want to run a keyword if testcasename matches.
run keyword if testcase=abc.robot
does anyone know how can we achieve that.
Robot framework provides several automatic variables -- variables that are set automatically by the framework. One such variable is ${TEST_NAME}.
In the following example, only the log statement in the second test will run:
*** Test Cases ***
Example 1
run keyword if "${TEST_NAME}" == "Example 2"
... log this is example 2?
Example 2
run keyword if '${TEST_NAME}' == "Example 2"
... log this is example 2!
Run Keyword If '${TEST_NAME}'=='<your_test_name>' <Keyword> <args>

How to write a loop while in Robot Framework

I make my first simple test case, and I have one problem.
Is it possible write a loop in Robot Framework?
I want to retrieve the value from the address and the address of the modified variable "i". I want to perform until such an address exists, because it is a row in the table.
${f1} A
${f_temp} B
While ${f1} != ${f_temp}
or
While element xpath=//${i} is visible
\ ${F_temp} Get Text xpath=//${i}
\ ${i} ${i}+1
\ Run Keyword And Continue On Failure Should be equal ${f_temp} ${f1}
Any ideas?
I'm updating my answer because modern Robot Framework does have a while loop.
The old answer, do not use this:
Robot Framework does not have a while loop. You must use the FOR-loop and "exit for loop if" keywords to exit. It will run in a finite time, but if you select a large enough number in range, it is close enough for practical purposes.
*** Test Cases ***
For Test
FOR ${i} IN RANGE 999999
Exit For Loop If ${i} == 9
Log ${i}
END
Log Exited
You might be looking for the Wait Until Keyword Succeeds keyword, which enables you to do a similar construction to a while loop. It is much more readable than FOR cycles with conditional exiting.
You then use your custom keyword, which fails when you need to end the "loop".
This are other kinds of FOR Loops in Robot Framework, I have this on my own notes and its very helpfull.
FOR Loop with Upper Bounds Range
[Documentation] This gives us a 0 based range
FOR ${Index} IN RANGE 5
Do Something ${Index}
${RANDOM_STRING} = Generate Random String ${Index}
Log ${RANDOM_STRING}
END
FOR Loop with Start and Finish Range
[Documentation] No longer a 0 based range because I provided start
FOR ${Index} IN RANGE 1 4
Do Something ${Index}
${RANDOM_STRING} = Generate Random String ${Index}
Log ${RANDOM_STRING}
END
FOR Loop with Start, Finish, and Step Range
[Documentation] The counter will jump by 2 each time ("step" value = 2)
FOR ${Index} IN RANGE 1 10 2
Do Something ${Index}
${RANDOM_STRING} = Generate Random String ${Index}
Log ${RANDOM_STRING}
END
#index for elements in for
${index} = Set Variable 0
FOR ${col} IN #{cols}
${colum} Format String css:div[class='v-widget v-has-caption v-caption-on-top'] table[aria-rowcount='{0}'] tbody tr:nth-of-type({1}) td:nth-of-type(10) ${r_count} ${col}
Click element ${colum}
press Keys none ${net_config.broadcast}
Press Keys none TAB
Press Keys none ${net_config.${index}}
${index}= Evaluate ${index} + 1
END
#-------
FOR Loop with List
#{ITEMS} = Create List Item 1 Item 2 Item 3
FOR ${MyItem} IN #{ITEMS}
Log ${MyItem}
END
Exit a FOR Loop
#{ITEMS} = Create List Item 1 Item 2 Item 3 Item 4
FOR ${MyItem} IN #{ITEMS}
Log ${MyItem}
Run Keyword If "${MyItem}" == "Item 3" Exit For Loop
Log Didn't exit yet
END
Log Now we're out of the loop
As the above answer said, Robot doesn't support native WHILE loop.
But this one may help if you insist.
https://github.com/robotframework/robotframework/issues/3235

Run a test case Multiple times and display the pass and fail count under test statistics

How to run a particular test case multiple times and display the pass and fail count under Test Statistics?
Below is the current code I have to run a test case multiple times. (The test case is implemented in a keyword and called)
*** Test Cases ***
Testcase
repeat keyword 5 Run Keyword And Continue On Failure Execute
*** Keywords ***
Execute
log Hello world!
The code is run from cmd using "pybot testcase.robot"
This code runs the test multiple times but I'm not getting the final pass/fail count in the logs.
I need to manually count the pass and fail test case repetitions.
So what modifications should I do to get the data automatically and should be seen in Test Statistics of the log also.
Instead of using "Repeat Keyword", use For loop.
Use "Run Keyword And Return Status" instead of "Run Keyword And Continue On Failure ".
*** Test Cases ***
Test Me
${fail}= Set Variable 0
:FOR ${index} IN RANGE 5
\ ${passed}= Run Keyword and Return Status Execute
\ Continue For Loop If ${passed}
\ ${fail}= ${fail} + 1
${success}= Set Variable 5 - ${fail}
Log Many Success: ${success}
Log Many fail: ${fail}

Resources