如何使用 Robot Framework 以编程方式编写 If 语句和 For 循环 API

How to write If statement and For loop programatically with Robot Framework API

我一直在探索 Robot 框架并遇到了我正在尝试使用的这个示例。除了我想尝试添加一个 for 循环和 if 语句之外,这个示例效果很好。我什至还没有开始 if 语句,因为我坚持使用 for 循环。请帮助我建议如何构建 for 循环和 if 语句。

这是一个for循环的基本尝试,在脚本末尾添加测试:

test.keywords.create('For', args=['1','IN','10'], type='for')
error - TypeError: __init__() got an unexpected keyword argument 'flavor'

下面的代码仅显示我正在尝试添加基本的 for 循环,但编译时出现上述错误

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')

来源 - 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)

更新:有了 Robot Framework,这已经改变并且变得更容易做到。

发行说明:Running and result models have been changed

  • TestSuite, TestCase and Keyword objects used to have keywords attribute containing keywords used in them. This name is misleading now when they also have FOR and IF objects. With TestCase and Keyword the attribute was renamed to body and with TestSuite it was removed altogether. The keywords attribute still exists but it is read-only and deprecated.
  • The new body does not have create() method for creating keywords, like the old keywords had, but instead it has separate create_keyword(), create_for() and create_if() methods. This means that old usages like test.keywords.create() need to be changed to test.body.create_keyword().

查看其他答案的示例:


机器人框架 4.0 之前:

IF 声明

if 语句应该是带有您需要的参数的 Run Keyword If 关键字。它是一个与其他任何关键字一样的关键字,因此您应该在其 args 列表中列出所有其他内容。

  • 条件。
  • True 分支的关键字名称。
  • 如果 True 分支的关键字有任何单独的 args。单独列出。

  • ELSE IF 关键字(如果需要)。
  • ELSE IF条件。
  • ELSE IF 分支的关键字名称。
  • 如果 ELSE IF 分支的关键字有任何单独的 args。单独列出。

  • ELSE 关键字。
  • ELSE 分支的关键字名称。
  • 如果 ELSE 分支的关键字有任何单独的 args。单独列出。
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.ForLoopclass在robot.running.model.Keywordclass的基础上实现的特殊关键字。这是构造函数:

它有一个flavor参数,就是要说的循环类型。所以是ININ RANGEIN ZIP

现在您实例化了一个 robot.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'

所以你要用ForLoopclass。此外,我使用的是 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 loop contains no keywords.

所以你必须像这样填充它:

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,你必须 运行 手动重启以获得日志和报告 HTML 文件。