如果您正在使用运行关键字 If http://robotframework.org/robotframework/latest/libraries/BuiltIn.html#Run%20Keyword%20If,第二列必须是 python 表达式而不是另一个关键字。关键字文档对此进行了解释。例如(为了清楚起见,使用管道分隔格式):
| | Run keyword if | ${answer} == 42 | Go to | http://www.example.com
如果您只想在页面包含 id 为“Current Status”的元素时运行关键字,则需要首先确定页面是否包含该元素,然后在表达式中使用该元素。有很多方法可以做到这一点。该文档显示了如何使用“运行关键字并忽略错误”,它看起来像这样:
| | ${status} | ${value}= | Run keyword and ignore error | Page should contain | //*[@id='Current Status']
| | Run Keyword if | '${status}' == 'PASS' | Go to | http://www.example.com
还有其他方法可以完成同样的事情。例如,您可以获取页面上有多少项包含 ID 的计数,并且仅在计数大于零时才运行关键字:
| | # 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
如果您想执行多个步骤,例如转到页面并进行一些验证,最直接的方法是创建一个单独的关键字并调用它。
...
| | Run keyword if | ${count} > 0 | Do extra validation
*** Keywords ***
| Do extra validation
| | Go to | http://www.example.com
| | Page should contain | Hello, world