Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assign a variable within the keyword "Run Keywords" possible?

following code is producing some problem:

API Setup
[Arguments]                           ${url}                           ${username}    ${password}    ${run}=True    ${fail}=False
Run Keyword If                        ${run}
...  Run Keywords     
     ...  ${passed}=                  Run Keyword And Return Status    Setup          ${url}         ${username}    ${password}
     ...  AND  Log To Console         ${passed}
     ...  AND  Should Not Be Equal    ${fail}                          ${passed}

When I try to execute that, my RF says: Variable '${passed}' not found.

The RED IDE also says

Multiple markers at this line:
  • Variable 'passed' is used, but not defined
  • Keyword name '${passed}=' contains variables. RED is unable to validate 
    arguments given to this keyword

Does the keywords "Run Keywords" not allow any assignments of values to variables and if so, is there any "best practice" way to do what I want to do?

Thanks!

like image 598
Faram Avatar asked Dec 14 '18 13:12

Faram


2 Answers

The Run Keywords does not allow assignments, that's true. This comes from the parser - the purpose of the keyword is to run other ones; it comes to the line with the assignment (${passed}= Run...), tries to substitute the variable "passed" with its value so it can execute it, but at this point the var is still undefined - and it fails.

It looks like what you want to do is to run a particular keyword (Setup) only if a condition is true, and only if the condition is true then log its outcome, and assert it (the outcome) is the desired one.

This can be achieved by breaking up the block in two. The keyword Run Keyword If returns the value of the embedded keyword, so this will work:

${passed}=    Run Keyword If    ${run}    Run Keyword And Return Status    Setup          ${url}         ${username}    ${password}
                             ...    ELSE    Set Variable    ${False}

If ${run} == True the Setup keyword will be ran, and {passed} will hold a True/False value did the execution pass. If ${run} != True we're setting ${passed} to False, just so it doesn't have a value of None (its value is not really important in this case, but doing that gives consistency in the datatypes it has).

And now the other two keywords can be in an if-block by themselves - do their work only when ${run} == True:

Run Keyword If    ${run}   Run Keywords     Log To Console         ${passed}
 ...  AND  Should Not Be Equal    ${fail}    ${passed}
like image 155
Todor Minakov Avatar answered Oct 20 '22 13:10

Todor Minakov


It is true that Run Keywords does not allow variable assignment within

Make a keyword that contains everything inside Run Keywords and just call it instead.

For example

*** Test Cases ***
API Setup
[Arguments]                           ${url}                           ${username}    ${password}    ${run}=True    ${fail}=False
Run Keyword If                        ${run}                           Keyword A

*** Keywords ***
Keyword A
    # put everything you need here
like image 24
thanh le Avatar answered Oct 20 '22 13:10

thanh le