【Robot Framework】BuiltIn库
作为一门表格语言,为了保持简单的结构,RF没有像别的高级语言那样提供类似if else while等内置关键字来实现各种逻辑功能,而是提供给了用户BuiltIn库。如果用户想在测试用例中实现比较复杂的逻辑,那就需要对BuiltIn中的重要关键字有一些了解。BuiltIn库中还封装了很多常见方法和能够控制RF运行状态的关键字,如果想用好RF,一定要对BuiltIn库中的函数有一个比较全面的理解。下面就带着大家认识一下BuiltIn库中比较重要的关键字。
Evaluate 关键字:
Arguments:[ expression | modules=None | namespace=None ]Evaluates the given expression in Python and returns the results.
如果你需要进行一些数值运算并得到结果,你就需要用到Evaluate关键字。Evaluate会把你想要计算的表达式直接传递给Python,并把Python的计算结果返回给你。这是最经常要用到的。
Should 系列关键字:
Should系列关键字是Should打头的一系列关键字。
Should Be Empty--------Verifies that the given item is empty.
Should Be Equal--------Fails if the given objects are unequal.
Should Be Equal--------Fails if the given objects are unequal.
Should Be Equal As Integers --------Fails if objects are unequal after converting(转换) them to integers.
Should Be Equal As Numbers--------Fails if objects are unequal after converting(转换) them to real numbers.
Should Be Equal As Numbers--------Fails if objects are unequal after converting(转换) them to real numbers.
Should Be Equal As Strings--------Fails if objects are unequal after converting(转换) them to strings.
Should Be True--------Fails if the given condition(状态) is not true.
Should Be True--------Fails if the given condition(状态) is not true.
Should Contain--------Arguments:[ container | item | msg=None | values=True | ignore_case=False ]Fails if container does not contain(包含) item one or more times.
Should Contain X Times--------Arguments:[ item1 | item2 | count | msg=None | ignore_case=False ]Fails if item1 does not contain item2 count times.
Should End With--------Arguments:[ str1 | str2 | msg=None | values=True | ignore_case=False ]Fails if the string str1 does not end with the string str2.
Should Match--------Arguments:[ string | pattern | msg=None | values=True | ignore_case=False ]Fails unless the given string matches the given pattern.
Should Contain X Times--------Arguments:[ item1 | item2 | count | msg=None | ignore_case=False ]Fails if item1 does not contain item2 count times.
Should End With--------Arguments:[ str1 | str2 | msg=None | values=True | ignore_case=False ]Fails if the string str1 does not end with the string str2.
Should Match--------Arguments:[ string | pattern | msg=None | values=True | ignore_case=False ]Fails unless the given string matches the given pattern.
Should Match Regexp --------Arguments:[ string | pattern | msg=None | values=True ]Fails if string does not match pattern as a regular expression(正则表达式).
Should Start With--------Arguments:[ str1 | str2 | msg=None | values=True | ignore_case=False ]Fails if the string str1 does not start with the string str2.
Should Not Be Empty
Should Not Be Equal
Should Not Be Equal As Integers
Should Not Be Equal As Numbers
Should Not Be Equal As Strings
Should Not Be True
Should Not Contain
Should Not End With
Should Not Match
Should Not Match Regexp
Should Not Start With
Should Not Be Equal
Should Not Be Equal As Integers
Should Not Be Equal As Numbers
Should Not Be Equal As Strings
Should Not Be True
Should Not Contain
Should Not End With
Should Not Match
Should Not Match Regexp
Should Not Start With
这些关键字都是用作判断时用的,每个用例都会用到,比如我们的执行结果得到了一个字符串,我们要判断这个字符串要与一个预期字符串相等,否则用例就无法通过,这时候,肯定会用上Should Be Equal As String关键字,其它关键字我们通过关键字的名字就能顾名思义,知道它的作用。
Convert To系列关键字:
Convert To Binary
Convert To Boolean
Convert To Hex
Convert To Integer
Convert To Number
Convert To Octal
Convert To String
Convert To Boolean
Convert To Hex
Convert To Integer
Convert To Number
Convert To Octal
Convert To String
做类型转换,将某个值转换为相对应的类型。
Run keyword系列关键字:
Run Keyword If------Arguments:[ condition | name | *args ]Runs the given keyword with the given arguments, if condition is true.
Run Keyword If All Critical Tests Passed
Run Keyword If All Tests Passed
Run Keyword If Any Critical Tests Failed
Run Keyword If Any Tests Failed
Run Keyword If Test Failed
Run Keyword If Test Passed
Run、Keyword If Timeout Occurred
Run Keyword If All Critical Tests Passed
Run Keyword If All Tests Passed
Run Keyword If Any Critical Tests Failed
Run Keyword If Any Tests Failed
Run Keyword If Test Failed
Run Keyword If Test Passed
Run、Keyword If Timeout Occurred
这些关键字能根据一个判断条件的真假来看是否执行关键字。一般使用这些关键字来实现高级语言中的ifelse功能。最常用的是Run Keyword If 和 Run Keyword unless 他们俩实现的效果正好相反。
Exit For Loop关键字:
用作退出循环,一般和Run Keyword If关键字联合使用,来实现条件退出。
Wait Until Keyword Succeeds关键字:
这是一个将异步调用变为同步调用的关键字。举一个例子:如果call某个WebService,并且需要得到返回结果才能做下一部操作。我们就会用到这个关键字
BuiltIn库里还有很多宝贝
比如日期相关的关键字GetTime。
让测试暂停的Sleep等。都相当有用。
RF目前仅有2个内置关键字:FOF 和 IN,来实现循环结构。功能还是比较弱的。
我走的很慢,但从不后退