如何用Robot框架API编程编写If语句和For循环

2024-05-14 23:48:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在探索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)

Tags: intestforiftypecreateargsrobot
1条回答
网友
1楼 · 发布于 2024-05-14 23:48:26

更新:有了Robot框架,这一点已经改变,变得更容易做到

发行说明: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().

例如,请查看另一个答案:How to write FOR loop and IF statement programmatically with Robot Framework 4.0?


在Robot Framework 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()

这是日志中的情况:

enter image description here


FOR循环

至于for循环。它是由基于robot.running.model.Keyword类的^{}类实现的特殊关键字。以下是构造函数:

enter image description here

它有一个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'

因此,您必须使用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 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,那么您必须手动运行rebot以获得一个日志和报告HTML文件

enter image description here

相关问题 更多 >

    热门问题