如何使用 Robot Framework 4.0 以编程方式编写 FOR 循环和 IF 语句?
How to write FOR loop and IF statement programmatically with Robot Framework 4.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}'])
一个产生了不希望的结果的例子。我希望它将 运行 记录 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)
当我们不知道列表中的项目数时,可以对此进行扩展。如何用关键字编码 -
运行循环,
从文件中选取项目并添加到列表中,
直到没有物品可取
您必须在 FOR
关键字的 body
中创建 Log
关键字,目前您正在测试用例的 body
中创建它。对于 IF
,您必须调用不带参数的 create_if()
,然后在其 returned 对象上,您可以使用类型和条件调用 create_branch(type='IF', condition='"${x}" == "b"')
。它将 return 一个对象,其中 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()
对于嵌套的 IF
和 FOR
语句,只需重复相同的语句即可。获取关键字的主体,然后调用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()
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}'])
一个产生了不希望的结果的例子。我希望它将 运行 记录 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)
当我们不知道列表中的项目数时,可以对此进行扩展。如何用关键字编码 -
运行循环, 从文件中选取项目并添加到列表中, 直到没有物品可取
您必须在 FOR
关键字的 body
中创建 Log
关键字,目前您正在测试用例的 body
中创建它。对于 IF
,您必须调用不带参数的 create_if()
,然后在其 returned 对象上,您可以使用类型和条件调用 create_branch(type='IF', condition='"${x}" == "b"')
。它将 return 一个对象,其中 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()
对于嵌套的 IF
和 FOR
语句,只需重复相同的语句即可。获取关键字的主体,然后调用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()