基本语法
用户关键字的写法和testcase的写法很像,在一个测试用例文件中,testcase放置在 *** Test Cases *** 部分, 关键字被放置在 *** Keywords *** 部分。
*** Keywords ***
Open Login Page
Open Browser http://host/login.html
Title Should Be Login Page
Title Should Start With
[Arguments] ${expected}
${title} = Get Title
Should Start With ${title} ${expected}
用户关键字和python编程语言的函数一样,有参数,有返回值,用户关键字可以在许多地方创建(testcase files, resources files, test suite initialization files)
基本格式
*** Keywords ***
KeywordName
[Documentation] 对关键字用途的解释,相当于对函数的注释
[Tags] 标签,和testcase的标签类似
[Arguments] 关键字参数
[Return] 关键字返回值
[Teardown] 关键字执行完毕之后需要执行的动作
[Timeout] 超时时间,关键字在规定的时间内执行完毕,若超出了时间,则该关键字执行失败
用户关键字名字和documentation
用户关键字的名字和test case名字命名差不多,可以很长,建议我们命名像函数名一样,简单且能知道是干什么用的。[Documentation] 对关键字的一些描述信息,多行的话用‘...’来连接,在运行的log里可以看到
官方文档给的例子:
*** Keywords ***
One line documentation
[Documentation] One line documentation.
No Operation
Multiline documentation
[Documentation] The first line creates the short doc.
...
... This is the body of the documentation.
... It is not shown in Libdoc outputs but only
... the short doc is shown in logs.
No Operation
Short documentation in multiple lines
[Documentation] If the short doc gets longer, it can span
... multiple physical lines.
...
... The body is separated from the short doc with
... an empty line.
No Operation
弃用关键字 [Deprecating keywords]
有时需要用新关键字替换现有关键字或将其完全删除。仅仅通知用户更改可能并不总是足够的,在运行时获得警告更有效。为了支持这一点,Robot Framework 能够标记已弃用的关键字。这使得从测试数据中找到旧关键字并删除或替换它们变得更容易。
关键字可以通过以文本 *DEPRECATED 开头,区分大小写,并在文档的第一行也有一个结束 * 来弃用。例如,DEPRECATED、DEPRECATED. 和 *DEPRECATED 都是有效标记。
执行弃用的关键字时,会记录弃用警告,并且该警告还会显示在控制台和日志文件的“测试执行错误”部分中。弃用警告以文本 Keyword '' is deprecated 开头。并且在弃用标记之后有其余的简短文档(如果有的话)。例如,如果执行以下关键字,日志文件中将出现如下所示的警告
1 自定义库
def example_keyword(argument):
"""*DEPRECATED!!* Use keyword `Other Keyword` instead.
This keyword does something to given ``argument`` and returns results.
"""
return do_something(argument)
2 关键字里
执行之后,log里的体现
用户关键字标签
用户关键字和library keywords和testcase都一样,都可以设置标签, 但Force Tags 和 Default Tags 设置可能不会影响它们。此外,可以在文档的最后一行使用标签指定关键字标签,并用逗号分隔。例如,下面两种格式都将获得相同的三个标签
*** Keywords ***
Settings tags using separate setting
[Tags] my fine tags
No Operation
Settings tags using documentation
[Documentation] I have documentation. And my documentation has tags.
... Tags: my, fine, tags
No Operation
用户关键字参数
用户关键字参数和函数参数一样,关键字参数使用与变量相同的语法,多个参数之间用两个以上的空格隔开,参数的命名,建议具有描述性,且使用小写字母,如
${my_arg} ${my arg} 或 ${myArg}
位置参数
顺序参数,传参的时候按照定义的顺序传入
*** Keywords ***
One Argument
[Arguments] ${arg_name}
Log Got argument ${arg_name}
Three Arguments
[Arguments] ${arg1} ${arg2} ${arg3}
Log 1st argument: ${arg1}
Log 2nd argument: ${arg2}
Log 3rd argument: ${arg3}
默认参数
定义参数的时候,给参数一个默认值,
${arg}=default # ${arg}和=之间不能有空格
*** Keywords ***
One Argument With Default Value
[Arguments] ${arg}=default value
[Documentation] This keyword takes 0-1 arguments
Log Got argument ${arg}
Two Arguments With Defaults
[Arguments] ${arg1}=default 1 ${arg2}=${VARIABLE}
[Documentation] This keyword takes 0-2 arguments
Log 1st argument ${arg1}
Log 2nd argument ${arg2}
One Required And One With Default
[Arguments] ${required} ${optional}=default
[Documentation] This keyword takes 1-2 arguments
Log Required: ${required}
Log Optional: ${optional}
Default Based On Earlier Argument
[Arguments] ${a} ${b}=${a} ${c}=${a} and ${b}
Should Be Equal ${a} ${b}
Should Be Equal ${c} ${a} and ${b}