Navigating Robot Framework maze…
If you have been using Robot Framework(RF) for longtime or a beginner you will definitely stumble upon following gotchas... Without further Ado here is the list…
- When to use “…” or “\”
You are trying to write
RunKeywordAndReturnIf <condition> <action>
and want to make the code look nice so you move action to new line
RunKeywordAndReturnIf <condition> <action>
& run the code. You get error
Keyword ‘BuiltIn.Run Keyword And Return If’ expected at least 2 arguments, got 1.
This is because RF reads “\n” as new line and not continuation. You need to add “…” to make it a continuation of the line, so the code now looks like this,
RunKeywordAndReturnIf <condition>… <action>
This brings us to the question, can I use “\” instead of “…” for continuation, as done in python. Answer is yes and no.
No because you cannot use it as line continuation character but
Yes you can use it as a block continuation character. Here is how you use it in RF
: FOR ${INDEX} IN RANGE 1 11
\ DoSomething
\ DoSomethingMore
In the above example, “\” is used as block continuation character.
2. Does it matter if I use <space> or <tab> in calling a keyword.
Suppose you are implementing a conditional sequence like this…
***Testcase***MenuGoto<tab>Cat1***Keywords***
MenuGoto
<tab>[Arguments]<tab>${category}${category} = Convert To Lowercase ${category}RunKeywordAndReturnIf '${category}' == 'cat1'
... MenuGoto<tab>Cat1RunKeywordAndReturnIf '${category}' == 'cat2'
... MenuGoto<tab>Cat2MenuGoto<space>Cat1
Log Cat1
MenuGoto<space>Cat2
Log Cat2
And try to run the code, this will result in an error
Maximum limit of started keywords exceeded.
Why is that so… !!
It is because MenuGoto<tab>Cat1
recursively calls MenuGoto<tab>Cat1
Although we have declared MenuGoto<space>Cat1; RF is not able to find MenuGoto<tab>Cat1. So right way to declare this is
***Testcase***MenuGoto<tab>Cat1***Keywords***
MenuGoto
<tab>[Arguments]<tab>${category}${category} = Convert To Lowercase ${category}RunKeywordAndReturnIf '${category}' == 'cat1'
... MenuGoto<space>Cat1RunKeywordAndReturnIf '${category}' == 'cat2'
... MenuGoto<space>Cat2MenuGoto<space>Cat1
Log Cat1
MenuGoto<space>Cat2
Log Cat2
3. Does it matter if I use If Else
or IF ELSE
Generally the answer to this question is that it does not matter. By RF is very specific about its cases…I burnt good few hours and some slack help from RF friends to finally get over this hurdle. Here is what I was trying to do.
`Login[Documentation] GetMyId . retry 3 times else failFOR ${i} IN RANGE 3${status}= Run Keyword And Ignore Error GetMyIdRun Keyword If '${status}[0]' == 'PASS' Exit For Loop... Else If ${i}==2 FailLog After Failure still runningEND
To naive user this seems simple enough. So just before exiting we mark Login
Keyword to fail. But thats not what you get. Here is the log
As you can see it ran through the loop but never exited as a result of “Pass” or mark test “Fail” as a result of end of loop. Ideally at i==2
this should have failed. After struggling a lot, good friends from RF community suggested using ELSE IF
instead of Else If
. After doing that this got fixed here are the new logs
and new code looks as follows
Login[Documentation] GetMyId . retry 3 times else failFOR ${i} IN RANGE 3${status}= Run Keyword And Ignore Error GetMyIdRun Keyword If '${status}[0]' == 'PASS' Exit For Loop... ELSE IF ${i}==2 FailLog After Failure still runningEND
Are there any more Gotchas you have come across. Please let me know…