Run Keyword If / Unless overwrite return variable even if not run?

1,784 views
Skip to first unread message

David Smith

unread,
Jan 17, 2014, 2:04:43 PM1/17/14
to robotframe...@googlegroups.com
Is this really the expected behavior?

| | @{port_status}= | Run Keyword Unless | '${testname}'=='wan' | SSH Port Status Network Interface | ${connection_name} | ${port} |
| | @{port_status}= | Run Keyword If | '${testname}'=='wan' | SSH Port Status Wan Interface | ${connection_name} | ${port} |

if the first line runs, port_status gets a value.
eventhough the second line case shouldn't run, @{port_status} gets overwritten to empty set.

My hack at the moment is to assign two different variables then;
| | ${status1_len}= | Get Length | ${port_status1}
| | @{port_status}= | Set Variable If |  ${status1_len} > 1 | ${port_status1} | ${port_status2} 

This seems clumsy, is there a better way?

Kevin O.

unread,
Jan 17, 2014, 2:42:08 PM1/17/14
to robotframe...@googlegroups.com
Starting with 2.7.4, you can use else clauses.
Try:
| | @{port_status}= | Run Keyword If | '${testname}'=='wan' | SSH Port Status Wan Interface | ${connection_name} | ${port} | ELSE | SSH Port Status Network Interface | ${connection_name} | ${port} |

Since the arguments are the same, you can also use Run Keyword with a dynamic keyword name. I tend to avoid doing this because things like automatic renaming in RIDE will miss it.
|       | ${interface type}= | Set Variable If | '${testname}'=='wan' | Wan | Network |
| | @{port_status}= | Run Keyword | SSH Port Status ${interface type} Interface | ${connection_name} | ${port} |
This at least keeps you from running the wrong keyword without reason.

Other options:
Create a SSH Port Status keyword that takes the type as a third parameter

David Smith

unread,
Jan 17, 2014, 5:45:49 PM1/17/14
to robotframe...@googlegroups.com
Thanks Kevin,

Those are great alternatives, and they'll work for me :)

However it still seems better suited if the variable went untouched if the keyword doesn't actually run, there's probably some underlying design reason why it works the way it does.

Thank again.

Pekka Klärck

unread,
Jan 17, 2014, 6:33:23 PM1/17/14
to dsm...@mypchelp.com, robotframework-users
2014/1/17 David Smith <dsm...@mypchelp.com>:
> Is this really the expected behavior?
>
> | | @{port_status}= | Run Keyword Unless | '${testname}'=='wan' | SSH Port
> Status Network Interface | ${connection_name} | ${port} |
> | | @{port_status}= | Run Keyword If | '${testname}'=='wan' | SSH Port
> Status Wan Interface | ${connection_name} | ${port} |
>
> if the first line runs, port_status gets a value.
> eventhough the second line case shouldn't run, @{port_status} gets
> overwritten to empty set.

The reason this happens is that all keywords, including Run Keyword If
and Run Keyword Unless, always return value. If there is no other
return value, the return value will be Python None. When assigning to
a list variable, None is later automatically turned into an empty
list.

In cases like the above keywords always returning something is
annoying and we wanted to fix it. Instead of making Run Keyword If and
friends somehow special regarding to how variables are assigned, we
decided to add ELSE IF/ELSE support for Run Keyword If. As Kevin
already demonstrated, this syntax avoids the problem when assigning
variables, and it is clearer than Run Keyword If/Unless combination in
some other use cases too.

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

David Smith

unread,
Jan 17, 2014, 6:55:15 PM1/17/14
to robotframe...@googlegroups.com, dsm...@mypchelp.com
Thanks Pekka!

unknown unknown

unread,
Mar 10, 2015, 1:01:39 AM3/10/15
to robotframe...@googlegroups.com
Hello

I am using Run Keyword If in my test case and it is giving the error
"Documentation:

Runs the given keyword with the given arguments, if `condition` is true.

Start / End / Elapsed:20150310 10:22:18.241 / 20150310 10:22:18.255 / 00:00:00.014
10:22:18.254FAILEvaluating expression ' == 0' failed: SyntaxError: invalid syntax" (<string>, line 1)


Below is the way i am using the keyword in my project

GET TEXT    ${Prev_AID}                   
${element}=    GET TEXT    ${Prev_AID}               
${run}=    run keyword if    ${element} == 0    "No field value"    else    "Field value not zero"               
                       

Reply all
Reply to author
Forward
0 new messages