using variables with the Run Keyword If evaluation

8,853 views
Skip to first unread message

Lori Thayer

unread,
Dec 28, 2012, 1:45:27 PM12/28/12
to robotframe...@googlegroups.com
I am trying to use Run Keyword If evaluation checking to see if a specific suite variable is set. I realized in looking through other topics that I was missing the quotes around my keyword so I've added though but then I have the following. I imagine there is a simple tweak I am missing.

I'm trying to use it in a user keyword. Here are a couple attempts I've made and their results

Edit Portfolio If One Exists Else Create Portfolio
    Run Keyword If  ${Portfolio_ste}=='PASS'  Go To Edit Portfolio 

This example would find the variable value but gave me this error: Evaluating condition 'AdvisorPortfolio_334=='PASS'' failed: NameError: name 'AdvisorPortfolio_334' is not defined
----------

Edit Portfolio If One Exists
    Run Keyword If  '${Portfolio_ste}'=='PASS'  Go To Edit Portfolio

This example evaluates as false when I know the variable is set

Kevin O.

unread,
Dec 28, 2012, 3:09:14 PM12/28/12
to robotframe...@googlegroups.com
When RF processes the line:
  Run Keyword If  ${Portfolio_ste}=='PASS'  Go To Edit Portfolio  
this is what happens:
${Portfolio_ste} is replaced by the value of variable Portfolio_ste, becoming:
  Run Keyword If  AdvisorPortfolio_334=='PASS'  Go To Edit Portfolio  
RF attempts to evaluate your condition. Since your argument AdvisorPortfolio_334=='PASS' is a string, the Run Keyword If evaluates it using the Python function eval() - this behavior is described in the documentation for keyword Should Be True. This is as if you typed AdvisorPortfolio_334=='PASS' into the Python interpreter.

What you want to do have an empty string (${EMPTY}) assigned to ${Portfolio_ste} by default.
Then you can do:
   Run Keyword Unless '${Portfolio_ste}'==''  Go To Edit Portfolio

Note that ${Portfolio_ste} is wrapped in quotes. This approach will fail if an unescaped quotation mark appears inside the variable value that is the same mark as used to enclose it. The following will fail:
${value}=    Set Variable    John's Store
Run Keyword If    '${value}'=='Test Store'    Some Keyword

If you are checking for a non-empty value, you can also do this:
${length}=    Get Length    ${Portfolio_ste}
Run Keyword If    ${length}    Go To Edit Portfolio

Since Get Length returns an integer, Run Keyword If will evaluate the keyword passed to it as long as ${length} is non-zero (since it is an integer).  In other words if ${Portfolio_ste} is not an empty string. This is a more robust check that does not have problems with embedded quotes. Note that Get Length works on other things too.

Kevin

Lori Thayer

unread,
Dec 28, 2012, 3:17:52 PM12/28/12
to robotframe...@googlegroups.com
Thank you Kevin - basically I want to verify the value is set before taking the IF action and if not I'll add an ELSE. 

I tried to follow the documentation in the Should Be True keyword referenced for Run Keyword If and just do
Run Keyword If  ${Portfolio_ste}  Go To Edit Portfolio 

but that didn't work. You've given me several options here. Fabulous

Lori

Pekka Klärck

unread,
Jan 7, 2013, 6:53:06 PM1/7/13
to korm...@gmail.com, robotframe...@googlegroups.com
2012/12/28 Kevin O. <korm...@gmail.com>:
> When RF processes the line:
> Run Keyword If ${Portfolio_ste}=='PASS' Go To Edit Portfolio
> this is what happens:
> ${Portfolio_ste} is replaced by the value of variable Portfolio_ste,
> becoming:
> Run Keyword If AdvisorPortfolio_334=='PASS' Go To Edit Portfolio
> RF attempts to evaluate your condition. Since your argument
> AdvisorPortfolio_334=='PASS' is a string, the Run Keyword If evaluates it
> using the Python function eval() - this behavior is described in the
> documentation for keyword Should Be True. This is as if you typed
> AdvisorPortfolio_334=='PASS' into the Python interpreter.

This is all true.

> What you want to do have an empty string (${EMPTY}) assigned to
> ${Portfolio_ste} by default.
> Then you can do:
> Run Keyword Unless '${Portfolio_ste}'=='' Go To Edit Portfolio

You can simplify this a little. In Python empty string is considered
false and others true, so you can just use the following instead:

Run Keyword If '${Portfolio_str}' Go To Edit Portfolio

> Note that ${Portfolio_ste} is wrapped in quotes. This approach will fail if
> an unescaped quotation mark appears inside the variable value that is the
> same mark as used to enclose it. The following will fail:
> ${value}= Set Variable John's Store
> Run Keyword If '${value}'=='Test Store' Some Keyword

In addition to internal quotes, also newlines cause problems in this
evaluation. A good way to avoid these problem is using Python's
triple-quote syntax:

Run Keyword If '''${value}'''=='Test Store' Some Keyword

In the above example ${value} could contain both internal quotes and
newlines. Only way to break it is having three quotes in a row
internally.

Cheers,
.peke
--
Agile Tester/Developer/Consultant :: http://eliga.fi
Lead Developer of Robot Framework :: http://robotframework.org

N.T.

unread,
Jun 18, 2015, 1:26:26 AM6/18/15
to robotframe...@googlegroups.com
Good day,

I read this thread a bit and a bit confused, so I'll explain my situation a bit before and confirm just to see if I understood the situation better.

Sometimes I have to switch between running my scripts in Browserstack and running my scripts on the Webdriver Browser. 

So in one of my keywords I have this script:

Run Keyword if    ${ENV} == local    Open Browser    ${url}    ${browser}
   
Run Keyword if    ${ENV} == browserstack    Open Browser    ${url}    ${browserName}    remote_url=${REMOTE_URL_BROWSERSTACK}    desired_capabilities=${DESIRED_CAPABILITIES_BROWSERSTACK}

The ${ENV} variable is set in a variable file.

From what I understand from this conversation, I need to set the ${ENV} file before evaluating the keyword?

${local}   local
$
{browserstack}    browserstack

$
{ENV} =Set Variable${local}

Run Keyword if  ${ENV}=${local}  Open Browser <local variables here>
Run keyword if   ${ENV}=${browserstack}  Open Browser<browserstack variables here>



If you also have another suggestions on how to switch from running from the webdriver than from Browserstack, that's cool too. 

Thanks!
Reply all
Reply to author
Forward
0 new messages