Robot Framework - performing multiple keywords after running keyword if
Asked Answered
F

3

9

I am trying to execute multiple keywords if a condition evaluates as true.

I tried to do something like this

 *** Test Cases ***
| Example

 *** Keywords ***
| Example
|  | ${title}=  Get Title
|  | Run Keyword If      | '${title}' == 'Some Title' 
|  | ... Click Element   |  xpath=some element 
|  | ... Element Text Should Be  |   xpath=some element   |  some text
|  | ... Else
|  | ... Click Element   | xpath=other element  

The error I get when running this is that the Click Element expects 1 argument but gets 4.

I know that I can set the if statement in the Test cases section and if evaluated true it will run a keyword with all the stuff I want but I wonder if there is a way to do it from the Keywords section.

Thanks.

Furnishing answered 4/5, 2015 at 13:48 Comment(0)
M
20

You can do a couple of things. The first is to create a new keyword that calls all the other keywords, and then call that from Run keyword if. This might be the most readable solution, but at the expense of having to write and document another keyword.

The other choice is to use a combination of Run keyword if and Run keywords, like so:

| | Run Keyword if | '${title}' == 'Some Title'
| | ... | Run Keywords
| | ... | Click Element | xpath=some element
| | ... | AND | Element Text Should Be  |  xpath=some element | some text
| | ... | ELSE
| | ... | Click Element | xpath=other element
Monocotyledon answered 4/5, 2015 at 14:17 Comment(1)
When using Run Keywords make sure to use capital AND (needed when using keywords with arguments as per documentation)Exert
A
0

Run keywords does not take keywords with arguments so you cannot use this.Option is to create keyword for all statements below Run keywords and call it. https://robotframework.googlecode.com/svn/trunk/doc/libraries/BuiltIn.html#Run --User keywords must nevertheless be used if the executed keywords need to take arguments.

Amelia answered 16/10, 2015 at 3:14 Comment(1)
"Run keywords does not take keywords with arguments" - that is not a true statement.Monocotyledon
D
0

Use IF END construct instead of "Run keyword If" to run multiple keywords without much hustle.

Example:

IF    '${skip}' == 'True'
    Log    This prints this line
    ${count} =    Evaluate    ${count} + 1
    IF    '${count}' == ${10}
        Log     Value of count: ${count}. Resetting ${skip}
        ${skip}    Set Variable    ${False}    
    END
END 
Dowzall answered 17/7, 2023 at 19:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.