Robot Framework - Run Keyword If - robotframework

I have a clarification on "Run Keyword If" command.
The command works if it is assigned to some variable as shown:
${txt}= Run Keyword If ${lenght} > 5 Some Keyword
... ELSE IF ${lenght} < 5 Some Keyword
... ELSE Some Keyword
Log ${txt}
If I use the command as shown, it is not working, it is taking the "ELSE IF" statements as arguments to the "Some Keyword"
Run Keyword If ${lenght} > 5 Some Keyword
... ELSE IF ${lenght} < 5 Some Keyword
... ELSE Some Keyword
Also I would like to know the "\" in the "FOR" loop represents continue statement or anything else?

Related

Run Keyword If has invalid syntax

Why is the following Robot statement complaining about 'Convert To Integer' keyword is invalid syntax? Thanks
Run Keyword If Convert To Integer ${packets_2} <= Convert To Integer ${packets_1}
... FAIL ${\n}[FAILED] Packets 2 not greater than packets 1.
... ${\n}packets_time1: ${packets_1} ${\n}packets_time2: ${packets_2}
You cannot call a keywords as the condition for Run Keyword If. The first argument is expected to be a python expression. Since you're trying to do a comparison of an integer, you can do that directly in the expression like so:
Run keyword if int('${packets_2}') <= int('${packets_1})
... FAIL \n[FAILED] Packets 2 not greater than packets 1
The other problem is that you are supplying two other arguments: ${\n}packets_time1: ${packets_1} and ${\n}packets_time2: ${packets_2}. It's not clear what you think those are for. I'm guessing you want them as part of the error message. If that's the case, it will have to be all on one line or else robot will think they are extra arguments to the FAIL keyword.
Run keyword if int('${packets_2}') <= int('${packets_1})
... FAIL \n[FAILED] Packets 2 not greater than packets 1\n$packets_time1: ${packets_1}\npackets_time2: ${packets_2}

Arguments in if statements are ignored 1/2 the time

*** variables ***
${x} 0
*** Test Cases ***
Test1
run keyword if ${x} == 1 run keywords
... print hi
... ELSE
... print hi
Test2
run keyword if ${x} == 0 run keywords
... print hi
... ELSE
... print hi
*** keywords ***
print
[arguments] ${x}
log to console ${x}
Output:
Test1 hi
Test1 | PASS |
------------------------------------------------------------------------------
Test2 | FAIL |
Keyword 'print' expected 1 argument, got 0.
------------------------------------------------------------------------------
What is going on here? Arguments at the second print work but are ignored at the first.
The difference is that in one case you're calling run keywords (with arguments) and in the other case you're running print (with arguments).
We can reformat your code to show how robot is looking at it:
run keyword if ${x} == 1
... run keywords print hi
... ELSE
... print hi
When the expression is false, you fall through and run print hi, and everything works.
When the case is true, robot runs run keywords print hi. run keywords treats each of its arguments as a separate keyword to run so it tries to run print, and then it tries to run hi. Since you aren't giving an argument to print, it throws the error.
The issue comes from you expecting the hi to be passed as an argument to print in the run keywords construct, but robot doesn't treat it that way, the hi is just another keyword to be ran.
In Run Keywords documentation there's a paragraph how to use keywords with arguments in it - you have to chain the keywords with an AND:
... keywords can also be run with arguments using upper case AND as a separator between keywords. The keywords are executed so that the first argument is the first keyword and proceeding arguments until the first AND are arguments to it. First argument after the first AND is the second keyword and proceeding arguments until the next AND are its arguments. And so on.
In your case:
run keyword if ${x} == 1 run keywords
... print hi AND No Operation
... ELSE
... print hi
, will now change the call to "run the keyword print with an argument 'hi', and then run the keyword No Operation" (which does literally nothing, comes in handy for situations like this).

How to null check List in Robot FrameWork

${rowcount}= Keyword1 Book1.xlsx 0
${length}= Set Variable ${rowcount}
${i} Set Variable 1
:FOR ${rowvalue} IN RANGE ${rowcount}
\ #{columnlist}= Keyword2 ${rowvalue}
Keyword2 is returning List of data. I want to check whether it is returning Empty List. Please help me with this?
The BuiltIn library has keywords Should be empty and Should not be empty which can be used to validate the length of a list.
Should Be Empty ${columnlist}
Should Not Be Empty ${columnlist}
Just in case someone else comes here looking for an answer which also addresses:
If ${columnlist} is not empty then I have to execute keyword below it. Is it possible using If statement? – Orsu Suni Jun 12 '17 at 4:44
Either one of these options should help (NOTE: only used within RIDE, I imagine they will work for others as well):
${len} Get Length ${columnlist} ---- will return zero if list is empty, you can then use ${len} in your conditional.
'#{columnlist}' == '#{EMPTY}' ---- should return true if list is empty, although so far I have only used it with RUN KEYWORD IF.
Alternatively you could run the following keyword:
${isEmpty} Run Keyword And Return Status Should Be Empty ${columnlist}
Then you get a boolean value in ${isEmpty} with whether the list is empty or not.
Statements/conditions in Robot are a bit confusing in my opinion. Previous suggestion 2. doesn't work for me. I'm using: Robot Framework 3.1.2 (Python 3.4.1 on win32)
I obtain expected solution:
*** Test Cases ***
TC1
${marker_files} Create List dummy3 dummy4 dummy5
Run keyword unless ${marker_files} == #{EMPTY} Operation on list
marker_files=${marker_files}
tc2
${marker_files} Create List #{EMPTY}
Run keyword unless ${marker_files} == #{EMPTY} Operation on list
marker_files=${marker_files}
*** Keywords ***
Operation on list
[Arguments] ${marker_files}=def
log to console \ndo something on list

How can I check if a variable is inside a list during a for loop in robot framework?

first time posting here. I've a feeling that this is a really dumb question, but for some reason my code keeps failing and I just can't put my finger on what's wrong.
Here's is what I have:
*** Settings ***
Library Selenium2Library
Library OperatingSystem
Library String
Library Collections
*** Test Cases ***
Test Robot Framework Logging
#{ALLOWED}= Create List /page1 /page2 /page3
${ControllersList}= Get File ${EXEC_DIR}/Resources/controllers.txt
#{PAGES}= Split to lines ${ControllersList}
:FOR ${PAGE} IN #{PAGES}
\ Run Keyword If '${PAGE} IN #{ALLOWED}' Log Testing WARN
[Teardown] Close Browser
This is the output:
Evaluating expression ''/page1 IN [u'/page1', u'/page2', u'/page3']'' failed: SyntaxError: invalid syntax (<string>, line 1)
If I change the condition to something like this it works:
'${PAGE} == /page1'
I checked the documentation and it seems that the IN condition could be used. I'm totally lost here. Any hints? Thanks!
This is the proper way to do the expression:
Run Keyword If $PAGE in $ALLOWED Log Testing WARN
By removing the quotes and the curly braces, robot is able to treat PAGE and ALLOWED as python variables when evaluating the expression.
From the section Evaluating Expressions in the documentation for the BuiltIn library:
Starting from Robot Framework 2.9, variables themselves are automatically available in the evaluation namespace. They can be accessed using special variable syntax without the curly braces like $variable. These variables should never be quoted, and in fact they are not even replaced inside strings.
Also, when using keywords like Run Keyword If, the expression must be a valid python expression after variable substitution. Therefore, you must use the operator in rather than IN. The latter is used only by the robot :FOR statement.
Example
*** Variables ***
#{ALLOWED} /page1 /page2 /page3
*** Test Cases ***
Example that passes
${PAGE}= set variable /page1
Run Keyword If $PAGE in $ALLOWED
... pass execution ${PAGE} is allowed
fail ${PAGE} is not allowed
Example that fails
${PAGE}= set variable /page99
Run Keyword If $PAGE in $ALLOWED
... pass execution ${PAGE} is allowed
fail ${PAGE} is not allowed
The check was almost right, but you've effectively turned the Boolean expression into a String by surrounding it with the quotes. Here's the syntax that'll do it:
\ Run Keyword If $PAGE in $ALLOWED Log Testing WARN
Mind there are no curly brackets {} around the variable names - thus the check is (almost) the straight python's variable in another_variable
The documentation Evaluating Expressions does indeed specify that in construction used in the evaluation itself. An alternative approach is to use the Collections library keyword Get Match Count. This will return 0 when no results are found, and not generate a test failure.
*** Settings ***
Library Collections
*** Test Cases ***
Test Robot Framework Logging
#{ALLOWED}= Create List /page1 /page2 /page3
#{PAGES}= Create List /page1 /page3 /page5
:FOR ${PAGE} IN #{PAGES}
\ ${InList}= Get Match Count ${ALLOWED} ${PAGE}
\ Run Keyword If ('${InList}' == '0') Log To Console Page Not Allowed
\ ... ELSE IF ('${InList}' == '1') Log To Console 1 Page Found
\ ... ELSE Log To Console More pages found.

Set Variable to Empty Dict if None in Robot Framework

I'm looking to recreate this in a test suite:
if value is None:
value = {}
I plan to be adding to the dictionary so the following does not work:
${default}= Create_Dictionary
${value2}= Set_Variable_If ${value} is ${None} ${default}
... ${value}
When I add a key to ${value2} it gets added to ${default} (most likely due to how Python passes around references).
If you are using robot framework 2.9 or greater you can use variables directly in python expressions by omitting the curly braces. If you have a robot variable named ${value}, you can reference it as $value where robot is expecting a python expression (such as with the evaluate keyword).
For example:
${value2}= evaluate {} if $value is None else $value
You can use Run Keyword If to run a keyword to set a variable. This can call Create Dictionary and create a new & unreferenced dictionary to set your variable.
${value}= Run Keyword If ${value} is ${None} Create Dictionary
... ELSE Set Variable ${value}

Resources