首页 > 解决方案 > 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)

标签: pythonloopsrobotframework

解决方案


更新:使用机器人框架,这已经改变并且变得更容易做到。

发行说明:运行和结果模型已更改

  • TestSuite,TestCaseKeyword对象曾经有关键字属性包含在他们使用的关键字。现在,当它们还具有 FOR 和 IF 对象时,此名称会产生误导。With TestCaseand Keyword属性被重命名为bodyand with TestSuiteit 被完全删除。关键字属性仍然存在,但它是只读的并且已弃用。
  • 新的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 文件。

在此处输入图像描述


推荐阅读