how to use “Run Keyword If” in robot framework

后端 未结 2 965
生来不讨喜
生来不讨喜 2021-02-10 04:43

I just started working on Robot Framework and I am trying to use Try Keyword If keyword, but all the examples I see online show the solution in a single line wherea

2条回答
  •  暖寄归人
    2021-02-10 05:24

    If you are using Run Keyword If, the second column must be a python expression rather than another keyword. This is explained in the keyword documentation. For example (using pipe-separated format for clarity):

    | | Run keyword if | ${answer} == 42 | Go to | http://www.example.com
    

    If you want to run a keyword only if the page has an element with the id of "Current Status", you need to first determine if the page has the element or not, and then use that in the expression. There are many ways to do this. The documentation shows how to use "Run keyword and ignore error", which would look something like this:

    | | ${status} | ${value}= | Run keyword and ignore error | Page should contain | //*[@id='Current Status']
    | | Run Keyword if | '${status}' == 'PASS' | Go to | http://www.example.com
    

    There are other ways to accomplish the same thing. For example, you could get a count of how many items on the page contain the ID, and only run the keyword if the count is greater than zero:

    | | # determine if something on the page has an id of 'Current Status'
    | | ${count}= | Get matching xpath count | //*[@id='Current Status']
    
    | | # if there is at least one item on the page with that id, go to xyz.com
    | | Run keyword if | ${count} > 0 | Go to | http://www.example.com
    

    If you want to perform multiple steps, such as go to the page and do some validation, the most straight-forward thing to do is create a separate keyword, and call that.

    ...
    | | Run keyword if | ${count} > 0 | Do extra validation
    
    *** Keywords ***
    | Do extra validation
    | | Go to | http://www.example.com
    | | Page should contain | Hello, world
    

提交回复
热议问题