how to use “Run Keyword If” in robot framework

大憨熊 提交于 2019-12-04 23:51:07

问题


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 whereas I have columns and rows in RIDE.

If I have a button with the ID of "Current Status" on the current page then I want to go to URL www.xyz.com and perform some action. The confusion is when I write Run Keyword If in the first cell of a test case in RIDE, what should I write in second column? Should this be the Page Should Contain? or Page Should Not Contain?

Please let me know what information I am missing for above.


回答1:


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



回答2:


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


来源:https://stackoverflow.com/questions/26763244/how-to-use-run-keyword-if-in-robot-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!