how to use “Run Keyword If” in robot framework

↘锁芯ラ 提交于 2019-12-03 15:35:25
Bryan Oakley

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

usually it's separated to 3 sections ,

Run Keyword If || 'condition' || Keyword to run

Example :

Run Keyword If  '${count}'<'5'  Pass Execution.

some keywords must be followed with some message like :

Run Keyword If  '${count}'<'5'  Log To Console  counter is less than 5
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!