Seth
Seth

Reputation: 716

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.

Upvotes: 2

Views: 52

Answers (2)

Todor Minakov
Todor Minakov

Reputation: 20067

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).

Upvotes: 2

Bryan Oakley
Bryan Oakley

Reputation: 386010

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.

Upvotes: 3

Related Questions