0
    bot.body.create_keyword('Create List', args=['a', 'b', 'c'], assign=['@{list}'])
    bot.body.create_for(flavor='IN', variables=['${x}'], values=['@{list}'])
    bot.body.create_keyword('log', args=['${x}'])

一个产生不良结果的例子。我希望它会运行 log 3 次,但它只记录 x 的最新值,即c. 我试图用这些做一些复杂的例子,比如嵌套的 if 和一个 for 循环,它读取列表中的 x 变量,直到捕获所有 x 并使用 if 语句针对条件验证每个 x。一个while循环会很好,但我猜一个for if会起作用。

如何使用 create if 执行此示例的 if 语句?

bot.body.create_keyword('Create List', args=['a', 'b', 'c'], assign=['@{list}'])
for_kw = bot.body.create_for(flavor='IN', variables=['${x}'], values=['@{list}'])
for_kw.body.create_keyword(if list[x] == 'b':
            log list[x] (or do any other keyword)
                     elif list[x] == 'c':
            log list[x]
                     else:
            end for loop)

当我们不知道列表中的项目数时,还有一个扩展。如何使用关键字对此进行编码 -

运行循环,从文件中提取项目并添加到列表中,直到没有项目可以提取

4

1 回答 1

1

您必须在Log关键字中body创建FOR关键字,目前您正在body测试用例中创建它。使用IF你必须调用create_if()不带参数的,然后在其返回的对象上你可以create_branch(type='IF', condition='"${x}" == "b"')使用类型和条件调用。它将返回一个对象,该对象body应该用于添加要在此IF分支内执行的关键字。

from robot.api import TestSuite

suite = TestSuite('Activate Skynet')
test = suite.tests.create('Should Activate Skynet', tags=['smoke'])

test.body.create_keyword('Create List', args=['a', 'b', 'c'], assign=['@{list}'])
for_kw = test.body.create_for(flavor='IN', variables=['${x}'], values=['@{list}'])
for_kw.body.create_keyword('log', args=['${x}'])

if_kw = for_kw.body.create_if()

if_branch = if_kw.body.create_branch(type='IF', condition='"${x}" == "b"')
if_branch.body.create_keyword('log', args=['BBBB'])

elif_branch = if_kw.body.create_branch(type='ELSE IF', condition='"${x}" == "a"')
elif_branch.body.create_keyword('log', args=['AAAA'])

else_branch = if_kw.body.create_branch(type='ELSE', condition='"${x}" == "c"')
else_branch.body.create_keyword('log', args=['CCCC'])

suite.run()

对于嵌套的IFandFOR语句,只需重复相同的操作即可。获取关键字的正文,然后调用create_if,create_for等。

from robot.api import TestSuite

suite = TestSuite('Activate Skynet')
test = suite.tests.create('Should Activate Skynet', tags=['smoke'])

test.body.create_keyword('Create List', args=['a', 'b', 'c'], assign=['@{list}'])
for_kw = test.body.create_for(flavor='IN', variables=['${x}'], values=['@{list}'])
for_kw.body.create_keyword('log', args=['${x}'])

for_kw2 = for_kw.body.create_for(flavor='IN', variables=['${x}'], values=['@{list}'])
for_kw2.body.create_keyword('log', args=['${x}'])

suite.run()
于 2021-03-19T09:29:48.710 回答