【问题标题】:How to write FOR loop and IF statement programmatically with Robot Framework 4.0?如何使用 Robot Framework 4.0 以编程方式编写 FOR 循环和 IF 语句?
【发布时间】:2021-06-16 07:53:47
【问题描述】:
    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)

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

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

【问题讨论】:

    标签: python for-loop if-statement robotframework


    【解决方案1】:

    您必须在FOR 关键字的body 中创建Log 关键字,目前您是在测试用例的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()
    

    对于嵌套的 IFFOR 语句,只需重复相同的操作即可。获取关键字的正文,然后调用create_ifcreate_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()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-17
      • 2018-06-06
      • 2021-09-20
      • 1970-01-01
      • 1970-01-01
      • 2016-07-19
      • 2018-03-16
      • 2014-07-14
      相关资源
      最近更新 更多