比起Python控制变量,RF会很方便,所以我们必须熟悉它,适应它
如果对比Python来学的话,会事半功倍
变量名包含变量种类标识符($, @, &, %),大括号({,})和变量名。大括号是强制使用的。
Robot Framework 中的变量和关键字相似,是大小写敏感,但对空格和下划线是忽略的。
标量是指${},这个有点像shell中的变量引用
赋值: ${a} Set variable 5
引用就简单的${a}就可以引用
如: Log ${a}
作为返回值:
*** Test Cases ***
One Return Value
${ret} = Return One Value argument
Some Keyword ${ret}
Multiple Values
${a} ${b} ${c} = Return Three Values
@{list} = Return Three Values
${scalar} @{rest} = Return Three Values
*** Keywords ***
Return One Value
[Arguments] ${arg}
Do Something ${arg}
${value} = Get Some Value
[Return] ${value}
Return Three Values
[Return] foo bar zap
用@{}表示,在python中列表赋值方式[a, b]
赋值:
@{List1} Create List user name password
@{L2} 1 2 3 4
@{L3} Set variable a b
取值及应用:
在循环中使用:
: FOR ${x} IN @{L2}
\ log ${x}
单个元素的取用:
*** Test Cases ***
List Variable Item
Login @{USER}[0] @{USER}[1]
Title Should Be Welcome @{USER}[0]!
Negative Index
Log @{LIST}[-1]
Index As Variable
Log @{LIST}[${INDEX}]
设置里使用List:
*** Settings ***
Library ExampleLibrary @{LIB ARGS} # This works
Library ${LIBRARY} @{LIB ARGS} # This works
Library @{NAME AND ARGS} # This does not work
Suite Setup Some Keyword @{KW ARGS} # This works
Suite Setup ${KEYWORD} @{KW ARGS} # This works
Suite Setup @{KEYWORD} # This does not work
Default Tags @{TAGS} # This works
和Python里的定义一致, key=value格式
赋值:
*** Variables ***
&{D1} user=username password=password
&{D2} Create Dictionary student=Tom age=22
如下是等价的
*** Test Cases ***
Constants
Login name=robot password=secret
Dict Variable
Login &{USER} #假设&{USER}为 {'name': 'robot', 'password': 'secret'}
单个值的访问:
*** Test Cases ***
Dict Variable Item
Login &{USER}[name] &{USER}[password]
Title Should Be Welcome &{USER}[name]!
Key As Variable
Log Many &{DICT}[${KEY}] &{DICT}[${42}]
Attribute Access
Login ${USER.name} ${USER.password}
Title Should Be Welcome ${USER.name}!
设置里也可以使用:
*** Settings ***
Library ExampleLibrary &{LIB ARGS}
Suite Setup Some Keyword &{KW ARGS} named=arg
环境变量是全局的,测试运行期间都有效,无文件限制
通过 OperatingSystem 测试库的 Set Environment Variable 和 Delete Environment Variable 关键字可以动态设置环境变量,一个测试用例设置的环境变量会保留到下一个用例中,但执行结束后就失效了。
*** Settings ***
Library OperatingSystem
*** Test Cases ***
Env Variables
Log Current user: %{USER}
Run %{JAVA_HOME}${/}javac
取值:Get Environment Variable
变量表
*** Variables ***
${a} hello world
@{List1} one two three four
&{D1} user=username password=password
变量文件
可以用python文件导入到工程中
*** Settings ***
Variables config.py
config.py内容大致如下:
# -*- coding: utf-8 -*-
HOST="10.11.12.100"
USER_NAME="[email protected]"
USSER_PWD="123456"
USER={ user = “hanmeimei”, age = 12}
--variable VAR_NAME1:value1 --variable VAR_NAME2:value2
返回值赋值
*** Keywords ***
Return One Value
[Arguments] ${arg}
Do Something ${arg}
${value} = Get Some Value
[Return] ${value}
调用:
${x} Return One Value 你好吗?
使用关键字赋值,Set Variable, Set Test Variable, Set Suite Variable, Set Global Variable可以在运行时赋值,如同大多数编程语言一样的使用。
数字变量的赋值
${x} Set Variable ${5} #${x}实际是整型
${x} Set Variable 5 #${x}实际是个字符串
还可以创建二进制,八进制和十六进制数,前缀分别为 0b, 0o, 0x。
${x} Set Variable ${0o15}
${x} Set Variable ${0b1010}
${x} Set Variable ${0x1fff}
布尔型赋值/ Null 及 None
*** Variables ***
${a} ${true}
${x} Set Variable ${false}
${x} Set Variable ${None}
${x} Set Variable ${null}
空值及空格的赋值
空格用${SPACE}专用变量,空值用${EMPTY}专用变量
Should Be Equal ${SPACE * 5} \ \ \ \ \ \
Should Be Equal ${EMPTY} \
内置变量:
Variable |
Explanation |
${CURDIR} |
An absolute path to the directory where the test data file is located. This variable is case-sensitive. |
${TEMPDIR} |
An absolute path to the system temporary directory. In UNIX-like systems this is typically /tmp, and in Windows c:\Documents and Settings\ |
${EXECDIR} |
An absolute path to the directory where test execution was started from. |
${/} |
The system directory path separator. / in UNIX-like systems and \ in Windows. |
${:} |
The system path element separator. : in UNIX-like systems and ; in Windows. |
${\n} |
The system line separator. \n in UNIX-like systems and \r\n in Windows. New in version 2.7.5. |
自动化执行内置变量
Available automatic variables
变量 |
解释 |
作用域 |
${TEST NAME} |
当前测试的名称 |
Test case |
@{TEST TAGS} |
当前测试包含的TAG,可以动态的用 Set Tags and Remove Tags 关键字修改 |
Test case |
${TEST DOCUMENTATION} |
Keyword注释 |
Test case |
${TEST STATUS} |
当前运行的测试的状态, PASS or FAIL. |
Test teardown |
${TEST MESSAGE} |
当前的测试的消息 |
Test teardown |
${PREV TEST NAME} |
前一条测试的名称,如果第一个没有执行过,前一条为空 |
Everywhere |
${PREV TEST STATUS} |
前一条测试执行状态: PASS, FAIL, or 一条empty的字符串 |
Everywhere |
${PREV TEST MESSAGE} |
前一条测试的消息 |
Everywhere |
${SUITE NAME} |
当前suite的名称 |
Everywhere |
${SUITE SOURCE} |
suite绝对路径 |
Everywhere |
${SUITE DOCUMENTATION} |
suite声明的注释 |
Everywhere |
&{SUITE METADATA} |
The free metadata of the current test suite. Can be set using Set Suite Metadata keyword. New in Robot Framework 2.7.4. |
Everywhere |
${SUITE STATUS} |
当前test suite的状态, PASS or FAIL. |
Suite teardown |
${SUITE MESSAGE} |
当前test suite的状态消息及统计 |
Suite teardown |
${KEYWORD STATUS} |
当前关键字的状态, PASS or FAIL. New in Robot Framework 2.7 |
User keyword teardown |
${KEYWORD MESSAGE} |
当前keyword的错误消息. New in Robot Framework 2.7. |
User keyword teardown |
${LOG LEVEL} |
当前 log level. New in Robot Framework 2.8. |
Everywhere |
${OUTPUT FILE} |
输出绝对路径 output file. |
Everywhere |
${LOG FILE} |
log file绝对路径 ,如果为 NONE 不创建log.html |
Everywhere |
${REPORT FILE} |
report file 绝对路径,如果为 NONE 就不创建report.html |
Everywhere |
${DEBUG FILE} |
debug file绝对路径,如果为NONE就不创建debug.html |
Everywhere |
${OUTPUT DIR} |
output directory路径 |