Python3.6+RobotFramework中的变量、赋值

RF的自动化测试之行2

  • 基础
    • 变量的优先级
    • 变量的作用域
    • scalar变量
    • list变量
    • 字典变量
  • 内置变量built-in variables
    • 与操作系统相关的变量
    • 数字变量
    • 布尔变量 、NULL、NONE
    • 空格和空
    • 自动变量automatic variable

基础

变量是大小写敏感的
全局变量要大写,如${PATH}
$@&%加{}来定义变量,可以用字母、下划线、数字、空格

变量的优先级

1、命令行中定义的变量优先级最高。会覆盖test case里Variables中的、test data中导入的resource and variable files
单独使用–variable定义的,覆盖–variablefile定义的
单独使用–variable定义的,后边的覆盖前边的
多个variablefile里都有定义的,先引用的文件里的优先级最高
2、测试文件的Variable table定义的变量,覆盖引入的resource and variable files里的变量,在当前文件的所有test case里可用。
3、resource and variable files里的变量优先级一样,最低。。。最先引入的生效。resource file里的变量会覆盖该文件引入resource and variable files里的变量。Variable里不能使用resource and variable files里引入的变量,因为Variable table的处理在Setting table之前,resource and variable files又是在Setting table里引入的。
4、使用return values from 和 Set Test/Suite/Global Variable 关键字定义的变量会覆盖所有变量,但是也仅限于该语句所在的范围。
5、类似${TEMPDIR} 、 ${TEST_NAME}的系统变量优先级最高,不能在Variable table 或者 command line中修改。${CURDIR}会随着测试执行而自动变化。

变量的作用域

作用域的四个等级:global, test suite, test case or local scope
1、Global scope:在命令行中通过 --variable and --variablefile参数指定,或者在任意位置通过Set Global Variable关键字设置。内置变量也是全局范围可用的。建议全部用大写字母表示
2、Test suite scope:测试套件范围内可用。可以通过resource and variable files,或者使用Set Suite Variable关键字指定。高级套件里的变量不会递归到低级别的套件里,但是可以通过resource and variable files来共享变量的使用。建议全部用大写字母表示
3、Test case scope:在test case里以及调用的用户自定义关键字里可用,可以在test case里通过Set Test Variable关键字设置。建议全部用大写字母表示
4、Local scope:test case和用户自定义关键字里有其他test case和用户自定义关键字不可见的变量。本地变量可以通过return values关键字从关键字的执行中获取,用户自定义关键字也可以拿来做参数建议全部用小写字母表示

Prior to Robot Framework 2.9 variables in the local scope leaked to lower level user keywords. This was never an intended feature, and variables should be set or passed explicitly also with earlier versions.

scalar变量

最常用的变量表达式
变量的赋值
1、在Variables段落里,直接赋值

${name}      zhangsan
${age}       20

2、使用set variable关键字

${name} =  set variable   lisi

3、使用evaluate关键字

${age}    evaluate     random.randint(20, 100)        random

evaluate的用法

evaluate + 表达式 + 引用模块

变量的使用

 log to console      ${name} is ${age} old
 log   ${name}

list变量

@{list}
变量的赋值
1、在Variables段落里,直接赋值

@{user}      [zhangsan

2、@{list} = Create List 1 2 3

变量的使用
使用list变量时,相当于将list中的值依次输出使用,以下两句代码作用一样:
1、做参数时

KeyWord   ${user}
KeyWord   ${username}    ${password}

2、日志输出

 log many       &{dic}

逐行打印
a=a1
b=b2
c=c3

   log many       ${dic}

一行打印字典:[‘a=a1’, ‘b=b2’, ‘c=c3’]

字典变量

&{dic}
变量的赋值
1、在Variables段落里,直接赋值

&{dic}      a=a1   b=b2   c=c3

变量的使用

log many       &{dic}

按照字典的形式逐行打印
a=a1
b=b2
c=c3

   log many       ${dic}

一行打印字典:{‘a’:‘a1’, ‘b’: ‘b2’, ‘c’: ‘c3’}

内置变量built-in variables

与操作系统相关的变量

变量 说明
${CURDIR} test data file的绝对路径. 只有这个必须大写、大小写敏感
${TEMPDIR} 系统临时目录的绝对路径 UNIX-like systems 里一般是 /tmp, Windows里一般是 c:\Documents and Settings\用户\Local Settings\Temp.
${EXECDIR} 绝对路径test execution was started from.
${/} 路径中的分隔符 / in UNIX-like systems and \ in Windows.
${:} 多个路径之间的间隔符 : in UNIX-like systems and ; in Windows.
${\n} 换行符 \n in UNIX-like systems and \r\n in Windows.

数字变量

Connect    example.com    80       # 参数是两个字符
Connect    example.com    ${80}    #参数是一个字符、一个整数	
Do X    ${3.14}    ${-1e-4}        # 两个浮点数3.14 and -0.0001	

也可以用二进制、八进制、十六进制(大小写不敏感)

Should Be Equal    ${0b1011}    ${11}
Should Be Equal    ${0o10}      ${8}
Should Be Equal    ${0xff}      ${255}
Should Be Equal    ${0B1010}    ${0XA}

布尔变量 、NULL、NONE

大小写不敏感

${true} 和 ${True}一样
${false}
${None} 和 ${Null}一样

空格和空

Should Be Equal    ${SPACE}          \ \
Should Be Equal    ${SPACE * 4}      \ \ \ \ \
Should Be Equal    ${SPACE * 10}     \ \ \ \ \ \ \ \ \ \ \
Should Be Equal    "${SPACE}"        " "
Should Be Equal    "${SPACE * 2}"    " \ "
Should Be Equal    ${EMPTY}          \

空列表:@{EMPTY}
空字典:&{EMPTY}

自动变量automatic variable

变量 说明 范围
${TEST NAME} The name of the current test case. Test case
@{TEST TAGS} Contains the tags of the current test case in alphabetical order. Can be modified dynamically using Set Tags and Remove Tags keywords. Test case
${TEST DOCUMENTATION} The documentation of the current test case. Can be set dynamically using using Set Test Documentation keyword. Test case
${TEST STATUS} The status of the current test case, either PASS or FAIL. Test teardown
${TEST MESSAGE} The message of the current test case. Test teardown
${PREV TEST NAME} The name of the previous test case, or an empty string if no tests have been executed yet. Everywhere
${PREV TEST STATUS} The status of the previous test case: either PASS, FAIL, or an empty string when no tests have been executed. Everywhere
${PREV TEST MESSAGE} The possible error message of the previous test case. Everywhere
${SUITE NAME} The full name of the current test suite. Everywhere
${SUITE SOURCE} An absolute path to the suite file or directory. Everywhere
${SUITE DOCUMENTATION} The documentation of the current test suite. Can be set dynamically using using Set Suite Documentation keyword. Everywhere
&{SUITE METADATA} The free metadata of the current test suite. Can be set using Set Suite Metadata keyword. Everywhere
${SUITE STATUS} The status of the current test suite, either PASS or FAIL. Suite teardown
${SUITE MESSAGE} The full message of the current test suite, including statistics. Suite teardown
${KEYWORD STATUS} The status of the current keyword, either PASS or FAIL. User keyword teardown
${KEYWORD MESSAGE} The possible error message of the current keyword. User keyword teardown
${LOG LEVEL} Current log level. Everywhere
${OUTPUT FILE} An absolute path to the output file. Everywhere
${LOG FILE} An absolute path to the log file or string NONE when no log file is created. Everywhere
${REPORT FILE} An absolute path to the report file or string NONE when no report is created. Everywhere
${DEBUG FILE} An absolute path to the debug file or string NONE when no debug file is created. Everywhere
${OUTPUT DIR} An absolute path to the output directory. Everywhere

你可能感兴趣的:(UI自动化测试)