python - How to write If statement and For loop programatically with Robot Framework API
问题描述
I have been exploring Robot framework and came across this example which I am trying to use. This example works great except I wanted to try adding a for loop and if statement. I haven't even began the if statement yet as I am stuck with the for loop. Please may I have help to suggest how to construct a for loop and if statement.
This is a basic attempt of a for loop to add at the end of the script to test:
test.keywords.create('For', args=['1','IN','10'], type='for')
error - TypeError: __init__() got an unexpected keyword argument 'flavor'
The code below just shows i am trying to add basic for loop but compiles with above error
from robot.api import TestSuite
suite = TestSuite('Activate Skynet')
suite.imports.library('OperatingSystem')
test = suite.tests.create('Should Activate Skynet', tags=['smoke'])
test.keywords.create('Set Environment Variable', args=['SKYNET', 'activated'], type='setup')
test.keywords.create('Environment V`enter code here`ariable Should Be Set', args=['SKYNET'])
test.keywords.create('For', args=['1','IN','10'], type='for')
origin - https://robot-framework.readthedocs.io/en/2.8.1/autodoc/robot.running.html
test.keywords.create('Create List', args=['a', 'b', 'c'], assign=['@{list}'])
for_kw = ForLoop(['${l}'], ['@{list}'], flavor='IN')
for_kw.keywords.create('log', args=['${l}'])
test.keywords.create()
test.keywords.append(for_kw)
解决方案
更新:使用机器人框架,这已经改变并且变得更容易做到。
发行说明:运行和结果模型已更改。
TestSuite
,TestCase
和Keyword
对象曾经有关键字属性包含在他们使用的关键字。现在,当它们还具有 FOR 和 IF 对象时,此名称会产生误导。WithTestCase
andKeyword
属性被重命名为body
and withTestSuite
it 被完全删除。关键字属性仍然存在,但它是只读的并且已弃用。- 新的
body
没有create()
创建关键字的方法,就像旧的关键字一样,但是它有单独create_keyword()
的 ,create_for()
和create_if()
方法。这意味着test.keywords.create()
需要将旧用法更改为test.body.create_keyword()
.
例如,查看其他答案:如何使用 Robot Framework 4.0 以编程方式编写 FOR 循环和 IF 语句?.
在机器人框架 4.0 之前:
IF
陈述
该if
语句应该是Run Keyword If
带有您需要的参数的关键字。它与任何其他关键字一样是关键字,因此您应该在其args
列表中列出所有其他内容。
- 条件。
True
分支的关键字名称。- 如果有的话,将任何分支
args
的关键字分开。True
单独列出。
- 关键字(
ELSE IF
如果需要)。 ELSE IF
条件。ELSE IF
分支的关键字名称。- 如果有的话,将任何分支
args
的关键字分开。ELSE IF
单独列出。
ELSE
关键字。ELSE
分支的关键字名称。- 如果有的话,将任何分支
args
的关键字分开。ELSE
单独列出。
from robot.api import TestSuite
suite = TestSuite('Activate Skynet')
test = suite.tests.create('Should Activate Skynet', tags=['smoke'])
test.keywords.create('Run Keyword If', args=[True, 'Log To Console', 'Condition was TRUE', 'ELSE', 'Log To Console', 'Condition was FALSE'])
test.keywords.create('Run Keyword If', args=[False, 'Log To Console', 'Condition was TRUE', 'ELSE', 'Log To Console', 'Condition was FALSE'])
suite.run()
这是它在日志中的样子:
FOR
环形
至于for
循环。它是基于robot.running.model.ForLoop
类的类实现的特殊关键字robot.running.model.Keyword
。这是构造函数:
它有一个flavor
参数,就是要说的循环类型。所以它是IN
, IN RANGE
, IN ZIP
, 等等。
现在您实例化 arobot.running.model.Keyword
尽管您可以将其类型设置为for
,但它不会具有flavor
属性。因此,当您执行代码时,它会抛出您看到的错误。这是因为ForRunner
将尝试访问该flavor
属性。
File "/usr/local/lib/python3.7/site-packages/robot/running/steprunner.py", line 52, in run_step
runner = ForRunner(context, self._templated, step.flavor)
AttributeError: 'Keyword' object has no attribute 'flavor'
所以你必须使用这个ForLoop
类。此外,我使用的是 Robot Framework 3.1.2,因此在我的情况下错误可能会有所不同,但方法应该是相同的。
这是它的样子:
from robot.running.model import ForLoop
for_kw = ForLoop(['${i}'], ['10'], flavor='IN RANGE')
test.keywords.append(for_kw)
不,这仍然会失败并出现错误:
FOR 循环不包含关键字。
所以你必须像这样填充它:
for_kw.keywords.create('No Operation')
完整的例子:
from robot.api import TestSuite
from robot.running.model import ForLoop
suite = TestSuite('Activate Skynet')
test = suite.tests.create('Should Activate Skynet', tags=['smoke'])
test.keywords.create('Log Many', args=['SKYNET', 'activated'], type='setup')
test.keywords.create('Log', args=['SKYNET'])
for_kw = ForLoop(['${i}'], ['10'], flavor='IN RANGE')
for_kw.keywords.create('No Operation')
test.keywords.append(for_kw)
suite.run()
如果你运行它,它只会产生一个输出 XML,你必须手动运行 rebot 来获得一个日志和报告 HTML 文件。
推荐阅读
- firebase - Firebase 如何在内部处理 Flutter 依赖项
- graphql - 为什么中继现代分页似乎要返回所有项目?
- reactjs - React、ReactJS 和 React Native 之间有什么区别?
- javascript - 按下按钮时无法设置未定义错误的属性
- python - 上采样的目的
- javascript - 是否可以本地延迟加载未指定尺寸的图像(因为它们需要响应)?
- c - 需要 C 中子进程的帮助
- python - 在 Python 中验证文本文件中的用户名和密码
- c++ - 在 CMake 中仅自动链接包含标头的源(简单的项目结构)
- css - 如何设置 Woocommerce 结帐选择选项的样式?