In the previous section, you have already felt the basics of Robot Framework. In this section, you will see more powerful uses of Robot Framework.

if statement


An if branch statement can be written with the "run keyword if" keyword.

*** Test Cases ***

test case8
    ${a}    Set variable    59
    run keyword if ${a}>=90 log 优秀 ... ELSE IF ${a}<=70 log 良好 ... ELSE IF ${a}<=60 log 及格 ... ELSE log 不及格 

First define the variable a equal to 59.

If it is judged that a is greater than or equal to 90, and the condition log is satisfied, the output is "excellent";

If the above conditions are not met, then else if judges that a is greater than or equal to 70, and the condition log is satisfied, and the output is "good";

If the above conditions are not met, then else if judges that a is greater than or equal to 60, and the condition log is satisfied, and the output "passes";

If none of the above conditions are met, else log outputs "failed".

Note the three dots ( ) in front of sele if and else

for loop


Write loops in Robot Framework via ":FOR".

1. Cycle 0~9.

*** Test Cases ***

test case9
    :FOR    ${i}    IN RANGE    10
    \    log ${i} 

A for loop is defined by ":FOR"; IN RANGE is used to specify the range of the loop.

2. Traverse the list.

*** Test Cases ***

test case10
    @{abc}    create list    a    b    c
    : FOR    ${i}    IN    @{abc}
    \    log ${i} 

The "create list" keyword is used to define a list (a,b,c), and the "@{}" is used to store the list. Iterate over the characters in the @{abc} list by looping through ":FOR".

Powerful Evaluate


Why is the "Evauate" keyword powerful? Because through it you can use the methods provided in the Python language.

1. Generate random numbers

In Python we can reference and use methods like this:

C:\Users\fnngj> python

Python 3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 18:41:36) [MSC v.1900 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import random >>> random.randint(1000, 9999) 9777 

The randint() method of the random module is used to get the current time.

Using "Evauate" in Robot Framework can also call the randint() method under the random module provided by Python.


*** Test Cases ***

test case11
    ${d}    Evaluate    random.randint(1000, 9999)    random
    log ${d} 

2. Call the Python program written by yourself

First create the D:/rf_test/count.py file.


def add(a,b): return a + b if __name__ == "__main__": a = add(4,5) print(a) 

通过add函数实现两个数相加,这是再简单不过的小程序了。

下面就通过 Robot Framework 调用 count.py 文件中的 add()函数。


*** Test Cases ***

test case12
    Import Library    D:/rf_test/count.py
    ${a}    Evaluate    int(4)
    ${b} Evaluate int(5) ${add} add ${a} ${b} log ${add} 

在 Robot Framework 中所有的内容都不是字符串类型,所以,需要通过 “Evaluate” 将 4 和 5 转化为 int 类型后,再调用 add 计算两个数的和。

注释


Robot Framework 中添加注释也非常简单。“Comment” 关键字用于设置脚本中的注释。除此之外,你也可以像 Python 一样使用“#”号进行注释。


*** Test Cases ***

test case13
    Comment    这是注释
    #这也是注释

如果你熟悉 Python 编程语言,那么 Robot Framework 几乎没有实现不了的功能。