BuiltIn的学习与总结


 

1.keyword系列关键字:

1.Run Keyword/Run Keywords/Run Keyword And Continue On Failure/Run Keyword And Expect Error/Run Keyword And Ignore Error/Wait Until Keyword Succeeds

Run Keyword:执行给定的关键字,待执行的关键字可以使一个动态变化的变量。

Run Keywords:执行给定的一系列关键字,多用于setups和teardowns 中。

Run Keyword And Continue On Failure:在给定的关键字执行失败后继续执行,在case结束后报错。

Run Keyword And Expect Error

Run Keyword And Ignore Error:执行给定的关键字,忽视错误,返回两个值:第一个值时关键字的执行结果(Pass/Fail),第二个值是关键字的返回值或错误信息。

Wait Until Keyword Succeeds:在给定时间内等待关键字执行成功。

2.return from keyword/return from keyword if/Run Keyword And Return/Run Keyword And Return If/Run Keyword And Return Status

return from keyword:当满足条件时,从关键字中跳出来,不执行该关键字之后的步骤。

使用在关键字中,不能在测试用例中直接使用(会报Invalid 'Return From Keyword' usage.的错误)

return from keyword if:后跟条件,当条件成立时,从用户关键字中跳出,类似于上图中第一种情况。

Run Keyword And Return:执行给定关键字KEYWORD1,然后从用户关键字中跳出,类似于run keywords KEYWORD1 AND return from keyword

Run Keyword And Return       KEYWORD1

Run Keyword And Return If:满足一定条件后执行给定关键字KEYWORD1,然后从用户关键字中跳出,类似于run keyword if 条件 关键字1,return from keyword

Run Keyword And Return If       条件    KEYWORD1

Run Keyword And Return Status:执行关键字,并返回关键字的状态(True/False)

注意:此关键字不会捕获由无效语法、超时或致命异常引起的错误。多与Run Keyword And Continue On Failure配合使用,较鸡肋。

3.Run Keyword If/Run Keyword Unless/Run Keyword If All Critical Tests Passed/Run Keyword If All Tests Pased/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/Run Keyword Unless:如果条件满足/不满足,执行后跟关键字

Run Keyword If All Critical Tests Passed/Run Keyword If Any Critical Tests Failed/Run Keyword If All Tests Passed/Run Keyword If Any Tests Failed只能被用在suite teardown

Run Keyword If Test Failed/Run Keyword If Test Passed/Run Keyword If Timeout Occurred只能被用在test teardown

2.Should 系列关键字:

Should Be Empty/Should Not Be Empty

Should Be Equal/Should Not Be Equal

Should Be Equal As Integers/Should Not Be Equal As Integers

Should Be Equal As Numbers/Should Not Be Equal As Numbers

Should Be Equal As Strings/Should Not Be Equal As Strings

Should Be True/Should Not Be True

Should Contain/Should Not Contain

Should Contain Any/Should Not Contain Any

Should End With/Should Not End With

Should Not Start With/Should Start With

Should Match/Should Not Match

Should Match Regexp/Should Not Match Regexp

Variable Should Exist/Variable Should Not Exist

Should Contain X Times

3.Convert To系列关键字:

Convert To Binary · Convert To Boolean  Convert To Bytes  Convert To Hex · Convert To Integer · Convert To Number · Convert To Octal · Convert To String

4.set 系列关键字

Set Global Variable/Set Suite Variable/Set Test Variable

Set Library Search Order

用法:该关键字用于解决测试数据中的关键字名称与多个关键字匹配时的冲突。

例如:file should exist在OperatingSystem与SSHLibrary中都存在,如果在使用时未指定库,则会报错:Keyword 'File Should Exist' found both from a custom test library 'SSHLibrary' and a standard library 'OperatingSystem'. The custom keyword is used. To select explicitly, and to get rid of this warning, use either 'SSHLibrary.File Should Exist' or 'OperatingSystem.File Should Exist'.

使用该关键字指定OperatingSystem为第一搜索顺序:

注意:该关键字执行后只适用于该suite;resources 中关键字的优先级要比库中的高;设置参数(library/resource name)要注意大小写&空格

Set Suite Documentation/Set Test Documentation

用法:设置suite/test的描述信息

Set Test Message

设置当前测试用例的消息,设置后在case的teardown中通过RF 内置变量${TEST MESSAGE}来获取到。

Set Suite Metadata

用法:设置当前用例的元数据,设置后可以通过内置变量${SUITE METADATA}来获取到。

Set Log Level

Valid levels are TRACE, DEBUG, INFO (default), WARN, ERROR and NONE (no logging)

Set Tags

在当前case或整个suite所有case中增加新的标签,可通过内置变量@{TEST TAGS}获取所有标签

Set Variable

用法很广泛,用时需注意,建议只把它当成一个对普通变量进行赋值的关键字,例如第一第二种用法:

Set Variable If

 1.png

5.loop/execution类关键字

continue for loop/continue for loop if

用法:跳过该次循环中后续的步骤,继续下一次的循环


结果:


 Exit For Loop/Exit For Loop If

用法:退出循环



结果:

pass execution/pass execution if

用法:退出

 

6.import类关键字

import library/import resource/import variables

7.get 类关键字

Get Count/Get Length/Get Library Instance/Get Time/Get Count/Get Variable Value/Get Variables

8.常用类关键字

Sleep

comment

在日志中显示信息。

evaluate

create dictionary   create list 

Fail

catenate

log /log many/log to console/log variables

9.其他类关键字

call method

Arguments: [ object | method_name | *args | **kwargs ]

Fatal Error

keyword should exist

length should be

no operation

regexp escape

reload library

remove tags

repeat keyword

replace variables

 

 

 

Evaluate 关键字:如果你需要进行一些数值运算并得到结果,你就需要用到Evaluate关键字。Evaluate会把你想要计算的表达式直接传递给Python,并把Python的计算结果返回给你。这是最经常要用到的。
 
Should 系列关键字:Should系列关键字是Should大头的一系列关键字。
 Should Be Empty · Should Be Equal · Should Be Equal As Integers · Should Be Equal As Numbers · Should Be Equal As Strings · Should Be True · Should Contain · Should Contain X Times· Should End With · Should Match · Should Match Regexp · 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 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
做类型转换比不可少。
 
Run keyword系列关键字:
 Run Keyword If · 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 · 
 
这些关键字能根据一个判断条件的真假来看是否执行关键字。一般使用这些关键字来实现高级语言中的if else功能。最常用的是Run Keyword If 和 Run Keyword unless 他们俩实现的效果正好相反。
 
Exit For Loop关键字:
用作退出循环,一般和Run keyword if 关键字联合使用,来实现条件退出。
 
 
Wait Until Keyword Succeeds
这是一个将异步调用变为同步调用的关键字。举一个例子:如果call某个WebService,并且需要得到返回结果才能做下一部操作。我们就会用到这个关键字。
 
BuiltIn库里还有很多宝贝,比如日期相关的关键字Get Time。让测试暂停的Sleep等。都相当有用。还等什么?
去这个链接遍历一遍它吧:)http://robotframework.googlecode.com/hg/doc/libraries/BuiltIn.html
 
 
注1:RF目前仅有2个内置关键字:FOF 和 IN,来实现循环结构。功能还是比较弱的。

原文地址:https://www.cnblogs.com/ting152/p/12516852.html