RobotFramework测试框架(4)-控制结构

本文介绍了RobotFramework中的For循环、嵌套循环结构以及如何使用RunKeywordIf进行条件判断,包括基本语法、多条件和执行多个动作的示例。

Robot Framework User Guide

简单For循环

*** Variables ***
@{LIST}     Apple Banana Orange
*** Test Cases ***
Constants
    FOR    ${element}    IN    @{LIST}
        Log    ${element}
    END

 

嵌套For循环

*** Test Cases ***
Constants
    FOR    ${element}    IN    @{LIST}
        FOR    ${element}    IN    @{LIST}
            Log    ${element}
        END
        Log    ${element}
    END
*** Test Cases ***
Multiple nesting levels
    FOR    ${root}    IN    r1    r2
        FOR    ${child}    IN    c1   c2    c3
            FOR    ${grandchild}    IN    g1    g2
                Log Many    ${root}    ${child}    ${grandchild}
            END
        END
        FOR    ${sibling}    IN    s1    s2    s3
            IF    '${sibling}' != 's2'
                Log Many    ${root}    ${sibling}
            END
        END
    END

分支

分支其实是主要通过关键字完成的,即Run Keyword If。

Run Keyword If    condition    name
    Run Keyword If    $var_in_py_expr1 == $var_in_py_expr2
    ...    Keyword    @args
    ...  ELSE IF    condition_in_py_expr
    ...    Keyword    @args
    ...  ELSE
    ...    Keyword    @args

`Run Keyword If` 是 Robot Framework 中的一个关键字,它允许你在满足特定条件时执行一个或多个关键字。以下是关于 `Run Keyword If` 的使用

方法和示例:

### 1. 基本语法

Run Keyword If    条件    关键字    参数



或者,如果你想要包含多个条件或多个执行动作,可以使用以下语法:

```robotframework
Run Keyword If    条件1    关键字1    参数1
...    ELSE IF    条件2    关键字2    参数2
...    ELSE        关键字3    参数3
```


示例1:单条件判断

假设我们有一个变量 `${value}`,我们想要检查它是否等于 10,如果是,则打印一条消息。

```robotframework
*** Variables ***
${value}    10

*** Test Cases ***
Example Test
    Run Keyword If    ${value} == 10    Log    Value is 10
```


#### 示例2:多条件判断

在这个示例中,我们有两个条件,并且根据条件的满足情况执行不同的关键字。

*** Variables ***
${value1}    5
${value2}    10

*** Test Cases ***
Example Test
    Run Keyword If    ${value1} < 10    Log    Value1 is less than 10
    ...    ELSE IF    ${value2} == 10    Log    Value2 is 10
    ...    ELSE        Log    None of the conditions are met


#### 示例3:多个执行动作

如果你想要在满足条件时执行多个动作,你可以使用 `Run Keywords` 关键字,并结合 `AND` 来分隔它们。

*** Variables ***
${value}    True

*** Test Cases ***
Example Test
    Run Keyword If    ${value} == True
    ...    Run Keywords    Log    Value is True    AND    Log    Executing another keyword


### 3. 注意事项

* 在条件中使用字符串时,需要将其放在引号中(单引号或双引号都可以)。
* `ELSE IF` 和 `ELSE` 必须是大写。
* 布尔值在 Robot Framework 中被识别为 `True` 和 `False`(不区分大小写)。
* 你可以使用 `in`、`and`、`or` 等运算符来构建更复杂的条件表达式,但请注意这些运算符在 Robot Framework 中都是小写。
* `Run Keyword If` 关键字可以在测试用例的任何地方使用,包括在关键字定义中。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值