如何使用Robot Framework 4.0以编程方式编写FOR循环和IF语句? [英] How to write FOR loop and IF statement programmatically with Robot Framework 4.0?

查看:98
本文介绍了如何使用Robot Framework 4.0以编程方式编写FOR循环和IF语句?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

    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会行得通.

an example which yielded undesired results. I was hoping that it would have run log 3 times but it just logs latest value of x which is c. I am trying to make some complex examples with these like a nested if, and a for loop that reads x variables in a list until all of x is captured and validate each x with if statement against a condition. A while loop would have been good but I guess a for if would work.

该示例的if语句如何与create if一起使用?

how to do if statement like this example with create 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)

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

There is an extension to this to when we do not know the number of items in the list. how to code this with keywords -

运行循环,从文件中选取项目并添加到列表中,直到没有物品可以拿起

run loop, pick up items from a file and add to a list, until no items to pick up

推荐答案

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

You have to create the Log keyword in the body of the FOR keyword, currently you are creating it in the body of the test case. With the IF you have to call the create_if() without parameters, then on its returned object you could call create_branch(type='IF', condition='"${x}" == "b"') with type and condition. It will return an object of which body should be used to add keywords to be executed inside this IF branch.

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 等.

For a nested IF and FOR statements, just repeat the same. Get the body of the keyword and then call create_if, create_for, etc.

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

这篇关于如何使用Robot Framework 4.0以编程方式编写FOR循环和IF语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆