前面已经提到变量有三类,一类是scalar variable ${variable},一类是list variable,还有一类是内置(Built-in)的变量.
1. 给scalar variable类型的变量赋值:${scalar variable}
(1)${scalar variable}= simple strings
(2)${scalar variable}= objects
无论scalar类型的变量被赋予什么值,这个值先被转换成Unicode,然后再按字符串显示出来。转换的过程相当于调用这个对象的__unicode__,__str__
2. 给list variable赋值: @{variable}
(1)列表变量中的值,可以是一个,也可以是多数(无数),还可以是空
(2)列表变量的值通常是字符类型的, 其它类型的也可以
(3)可以通过 @{NAME}[i]形式来访问列表中的某一个值
(4)可将list variables当作scalar类型的变量使用,使用方法是将list变量的符号从@替换成$就可以了;这种表达式让我们使用built-in类库及Collection类库中list相关的keywords成为可能。
(5)list变量可以应用在setting table中,但仅限于一部分的settings命令可用。
3. 给Envrionment Variables赋值: %{ENV_VAR_NAME}
(1)只能赋值string类型的值
(2)可于 OperatingSystem library类库中的keyword一块应用
4. 可以将keyword中返回的值作为变量存储
${keyword_return_value}= Get Matching Xpath Count [locator]
${keyword_return_value}= Get Element Attribute [locator]
...
5. 内置Build-in变量
1. 给scalar variable类型的变量赋值:${scalar variable}
(1)${scalar variable}= simple strings
(2)${scalar variable}= objects
无论scalar类型的变量被赋予什么值,这个值先被转换成Unicode,然后再按字符串显示出来。转换的过程相当于调用这个对象的__unicode__,__str__
2. 给list variable赋值: @{variable}
(1)列表变量中的值,可以是一个,也可以是多数(无数),还可以是空
(2)列表变量的值通常是字符类型的, 其它类型的也可以
(3)可以通过 @{NAME}[i]形式来访问列表中的某一个值
(4)可将list variables当作scalar类型的变量使用,使用方法是将list变量的符号从@替换成$就可以了;这种表达式让我们使用built-in类库及Collection类库中list相关的keywords成为可能。
(5)list变量可以应用在setting table中,但仅限于一部分的settings命令可用。
- 对于Library及Resource的settings来说list变量不能作为其name使用,只能作为argument使用
- 对于Setup/Teardown的settings来说list变量不能当作keyword的name使用,只能作为argument使用
- 对于tag的settings来说可以随意使用
- 对于不支持list变量的settings来说,我们可以使用scalar变量的形式来表达(如library及setup中的name不能使用list变量,我们可以用scalar变量代替list变量)
Settings | Value | Value | Comment |
---|---|---|---|
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 |
3. 给Envrionment Variables赋值: %{ENV_VAR_NAME}
(1)只能赋值string类型的值
(2)可于 OperatingSystem library类库中的keyword一块应用
4. 可以将keyword中返回的值作为变量存储
${keyword_return_value}= Get Matching Xpath Count [locator]
${keyword_return_value}= Get Element Attribute [locator]
...
5. 内置Build-in变量
Variable | Explanation |
---|---|
${CURDIR} | An absolute path to the directory where the test datafile is located. This variable is case-sensitive. |
${TEMPDIR} | An absolute path to the system temporary directory. In UNIX-likesystems this is typically/tmp, and in Windowsc:\Documents and Settings\<user>\Local Settings\Temp. |
${EXECDIR} | An absolute path to the directory where test execution wasstarted from. New in Robot Framework 2.1. |
${/} | The system directory path separator./in UNIX-likesystems,\in Windows. |
${:} | The system path element separator.:in UNIX-likesystems and;in Windows. |
Variable | Explanation | Available |
---|---|---|
${TEST NAME} | The name of the current test case. | Test case |
@{TEST TAGS} | Contains the tags of the current test case inalphabetical order. | Test case |
${TEST STATUS} | The status of the current test case, either PASS orFAIL. | Test teardown |
${TEST MESSAGE} | The possible error message of the current test case. | Test teardown |
${PREV TEST NAME} | The name of the previous test case, or an empty stringif 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 beenexecuted. | 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. Newin Robot Framework 2.5. | Everywhere |
${SUITE STATUS} | The status of the current test case, either PASS orFAIL. | Suite teardown |
${SUITE MESSAGE} | The full message of the current test suite, includingstatistics. | Suite teardown |
${OUTPUT FILE} | An absolute path to the currentoutput file. Hasdifferent values during execution whenoutputs are split. | Everywhere |
${LOG FILE} | An absolute path to the currentlog fileor stringNONE when no log file is created. Has different valuesduring execution whenoutputs are split. | Everywhere |
${REPORT FILE} | An absolute path to thereport fileor string NONEwhen no report is created. | Everywhere |
${SUMMARY FILE} | An absolute path to thesummary fileor string NONEwhen no summary is created. | Everywhere |
${DEBUG FILE} | An absolute path to thedebug fileor string NONEwhen no debug file is created. | Everywhere |
${OUTPUT DIR} | An absolute path to theoutput directory. |