如何使用“运行关键字如果"在机器人框架中 [英] how to use "Run Keyword If" in robot framework

查看:33
本文介绍了如何使用“运行关键字如果"在机器人框架中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始研究 Robot Framework,我正在尝试使用 Try Keyword If 关键字,但是我在网上看到的所有示例都在一行中显示了解决方案,而我在 RIDE 中有列和行.

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.

如果我在当前页面上有一个 ID 为当前状态"的按钮,那么我想转到 URL www.xyz.com 并执行一些操作.令人困惑的是,当我在 RIDE 的测试用例的第一个单元格中编写 Run Keyword If 时,我应该在第二列中写什么?这应该是 Page Should Contain 吗?或 页面不应包含?

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.

推荐答案

如果您使用的是 Run Keyword If,第二列必须是python表达式而不是另一个关键字.这在关键字文档中进行了解释.例如(为了清晰起见,使用竖线分隔格式):

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

如果你想只在页面有一个id为Current Status"的元素时才运行关键字,你需要先确定页面是否有该元素,然后在表达式中使用它.有很多方法可以做到这一点.文档显示了如何使用运行关键字并忽略错误",它看起来像这样:

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

还有其他方法可以完成同样的事情.例如,您可以计算页面上有多少项包含 ID,并且仅在计数大于零时才运行关键字:

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

这篇关于如何使用“运行关键字如果"在机器人框架中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆