PurpleBeni
PurpleBeni

Reputation: 479

Robot Framework nested if statement

I need to have a nested if statement in my test case.

I need to check if variable a equals X and if it does I need to check if variable b equals Y.

I tried doing something like this:

Click on button
Run Keyword If                      '${var_a}' == 'X'
...         Run Keyword If                      '${var_b}' == 'Y'
...                 Click Element               Locator_a
...                 ELSE
...                 Click Element               Locator_b

...         ELSE
...         Click Element                       Locator_c

The error that I receive is that click element expected 1 argument and got 4. Meaning once it returned False for the first if statement (var_a == X) it tried to call the first ELSE statement with all the later keywords as arguments (Click Element, Arg1 = locator_b, Arg2 = Else, Arg3 = Click Element, Arg5 = Locator_c).

Is there a Robot way to do this without writing a custom keywords by myself?

Upvotes: 2

Views: 11486

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 385970

Strictly speaking, no, there's no way to do what you want. However, you can combine your if statements into one large statement with three conditions joined by "ELSE IF" and "ELSE":

       Run keyword if  '${var_a}' == 'X' and '${var_b}' == 'Y'
...    Click Element    Locator_a

...    ELSE IF         '${var_a}' == 'X' and '${var_b}' != 'Y'
...    Click Element    Locator_b

...    ELSE
...    Click Element    Locator_c

Upvotes: 6

Related Questions