robotframework--标准库-Build In的API介绍

介紹下Robotframework接口测试--常用的API,掌握其api就可以做更多的操作啦!

======================

库类似于对象,关键字类似于对象中的函数、方法

那自定义关键字时,可以理解成自己写函数、方法。

 

Robot Framework 并没有像其它框架一样提供一份完整的 API 文档,所以,我们没办法通过官方 API文档进行习。RIDE 提供了 F5 快捷键来打开帮助文档。

一、标准库之 BuiltIn 

提供的关键字有用于验证的(e.g. Should Be Equal, Should Contain),转换的(e.g. Convert To Integer),其他(e.g. Log, Sleep, Run Keyword If, Set Global Variable)。

1.Name: Call Method

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

Calls the named method of the given object with the provided arguments. 

调用对象object的方法method_name

2.Name: Catenate

Arguments:[ *items ]

Catenates the given items together and returns the resulted string. 

连接给定参数,返回字符串(默认用空格连接)

例子:

${str1} =

Catenate

Hello

world

 

${str2} =

Catenate

SEPARATOR=---

Hello

world

${str3} =

Catenate

SEPARATOR=

Hello

world实际结果:

=>

- ${str1} = 'Hello world'

- ${str2} = 'Hello---world'

- ${str3} = 'Helloworld'

3.Name: Comment

Arguments:[ *messages ]

Displays the given messages in the log file as keyword arguments.

在日志文件中以关键字参数形式显示指定信息。

Comment对参数不进行任何处理,仅仅是显示在日志中,用于显示简单信息。

如果要显示变量值,可以使用Log 或 Log Many关键字。

4.Name: Continue For Loop

Arguments:[  ]

Skips the current for loop iteration and continues from the next. 

跳出当前迭代,继续下一个迭代

Convert 转换系列

5.Name: Convert To Binary

Arguments:[  item | base=base | prefix=prefix | length=length ]

Converts the given item to a binary string. 

转换参数为二进制字符串

Examples:
${result} =Convert To Binary    10     # Result is 1010

6.Name: Convert To Boolean  转换参数为布尔值true或false。

7.Name: Convert To Bytes  根据输入类型将给定的输入转换为字节。

8.Name: Convert To Hex  Convert To Hex    转换参数为十六进制字符串

${result} =Convert To Hex  255  # Result is FF

9.Name: Convert To Integer    转换参数为整数。

  10.Name: Convert To Number  

Arguments:[ item | precision=precision ]

Converts the given item to a floating point number. 

转换参数为浮点数。

注意:precision=-1时的使用

11.Name: Convert To Octal  转换参数为八进制字符串

12.Name: Convert To string 转换参数为Unicode字符串

13.Name: Create Dictionary

Arguments:[ *items ]

Creates and returns a dictionary based on the given items. 

根据给定参数创建字典

 这里不能换成${dict}哦

 小知识:robotframework可以使用 变量 列表 或 字典, 使用语法: ${变量}  @{列表} 和 &{字典}

14.Name: Create List

Arguments:[ *items ]

Returns a list containing given items. The returned list can be assigned both to ${scalar} and @{list} variables. 

根据给定参数创建list,同时返回的列表可以被分配给 ${变量}  @{列表}使用。

15.Name: Evaluate

Arguments:[ *items ]

Arguments:[ expression | modules=modules | namespace=namespace ]
Evaluates the given expression in Python and returns the results.

用Python计算表达式并返回结果。

 注意:如果表达式很复杂,建议在测试库中实现逻辑。

16. Name:  Exit For Loop   

立即停止执行for循环。

17.Name:Fail  

停止执行当前测试用例 

Fails the test with the given message and optionally alters its tags. 

18.Name:Fatal Error 

 停止执行整个测试。 后续用例不会执行,可能的teardown也不会执行。

Stops the whole test execution. 

Get系列

19.Name: Get Count

Arguments:[ item1 | item2 ]

Returns and logs how many times item2 is found from item1. 

返回并在日志中记录item2在Item1中出现的次数。

20.Name: Get Length

Arguments:[ item]

Returns and logs the length of the given item as an integer. 

返回并在日志中记录item的长度.  item可以是任何有一个长度的对象,例如,字符串,列表,或映射.

   len1=‘hello,world!’ 字符串的长度,  而len2=['heoo,','word!']列表的长度是2

21.Name:Get Library Instance  

返回指定测试库的当前活动实例。

22.Name:Get Time

Arguments:[ format=format | time_=time_ ]

Returns the given time in the requested format. 

按指定格式返回时间(下面第3行,返回的是UNIX 时间戳)

1)       如果格式中包含epoch,返回的是(Jan 1, 1970 0:00:00)开始的秒数。

2)       如果格式包含任何'year', 'month', 'day', 'hour', 'min', or 'sec',则返回对应部分。

3)       默认格式'2006-02-24 15:08:31'

默认返回当前时间。

23.Name:Get Variable Value 

Arguments:[ name, default=None ]

Returns variable value or default if the variable does not exist. 

返回变量值,如果变量不存在就返回default

23.Name:Get Variables 

 返回包含在当前范围内的所有变量的字典

24.name:Import Library

在test suite导入库。

可以在测试执行时动态导入库。支持库名和绝对路径导入。使用'WITH NAME'还可以更改库名。

25.name:Import Resource

在test suite导入资源文件。path必须是绝对路径。斜线/作为路径分隔符(包含Windows)。

26.name:Import Variables

导入变量文件

27.name:Keyword Should Exist

Arguments:[ name | msg=msg ]

Fails unless the given keyword exists in the current scope

除非关键字在当前作用域存在,否则失败。有多个同名的关键字存在也会Fail

28.Name:Length Should Be

Arguments: [ item | length | msg=msg ]
Verifies that the length of the given item is correct.

验证item的长度。(先通过关键字Get Length获取item的长度。再使用该方法验证)

 Log系列

29.Name:Log  

在指定日志级别下记录message。

有效的日志级别有TRACE, DEBUG, INFO (default), HTML and WARN。

注意,无效的HTML会破坏整个日志文件,一定要谨慎使用。HTML消息实际上是使用了INFO级别。

WARN级别记录的消息将在控制台和日志中测试执行的错误部分都可以看到。

30.Name:Log Many

INFO级别下记录messages为独立的条目。分别打印每一项元素。

31.Name:Log To Console

  把信息在控制台打印出来

32.Name:Log Variables  

用给定的日志级别记录当前作用域中的所有变量。

 33.Name:No Operation 

无操作,不做任何事情

34.Name:Pass Execution

Arguments:[ message | *tags ]

Skips rest of the current test, setup, or teardown with PASS status.

无条件的跳过后面的步骤

35.Name:Pass Execution if 

满足条件时,跳过后面的步骤

36.Name:Regexp Escape  

Returns each argument string escaped for use as a regular expression.

返回转义后用作正则表达式的每个参数字符串

此关键字可用于转义要与“should match regexp”和“should not match regexp”关键字一起使用。

37.Name:Reload Library  

重新检查指定库提供的关键字。

可以在测试数据中显式调用,也可以在它提供的关键字发生更改时由库本身调用。

38.name:Remove Tags 

 从当前测试用例或suite中删除标签 。

Arguments:[ *tags ]

Tag可以精确地给出,或者使用模式:'*'匹配所有字符,“?”单个字符的通配符给出。

39.name:Repeat Keyword

Arguments:[ repeat | name | *args ]

Executes the specified keyword multiple times. 

执行指定的关键字times次 或多少时间(e.g. 1 minute, 2 min 3 s)

40.Name:Replace Variables 

替换text中的变量。

如果文本中包含未定义的变量时,关键字执行Fail。

例子:假定文件“template.txt”的内容为“Hello $ {NAME}!”,变量的'${NAME}'的值为'Robot'。

41.name:Return From Keyword

Arguments:[ *return_values ]
Returns from the enclosing user keyword.

此关键字可用于从具有pass状态的用户关键字返回,而无需完全执行它。

此关键字通常包装为其他关键字,如“run keyword if”或“run keyword if test passed”,以根据条件返回:

42.name:Return From Keyword if

43.name:Run Keyword

 Arguments:[ name | *args ]

Executes the given keyword with the given arguments.

根据参数args执行关键字name。

name可以动态设定,例如,来自另一个关键字的返回值或命令行。

44.name:Run Keyword And Continue On Failure

根据参数args执行关键字name,错误时依旧继续执行。

45.name:Run Keyword And Expect Error

Arguments:[ expected_error | name | *args ]

运行关键字并检查是否发生了预期的错误。

预期的错误以与robot框架报告相同的格式给出预期错误。默认情况下,它被解释为带有*、?的全局模式。和[chars]作为通配符。

从robot framework 3.1开始,预期错误可以使用下表中解释的各种前缀进行更改。前缀区分大小写,必须用冒号和可选的空格(如prefix:message或prefix:message)将其与实际消息分开。

 46.name:Run Keyword And Ignore Error

根据参数args执行关键字,并忽略错误

47.name:Run Keyword And Return

48.name:Run Keyword And Return if

49.name:Run Keyword And Return Status 

返回状态值作为一个Boolean变量。

50.name:Run Keyword If 

满足条件时run keyword

51.name:Run Keyword If All Critical Tests Passed  

如果所有Critical测试用例通过,则根据参数args执行关键字。

只能用于suite teardown。

52.name:Run Keyword If All Tests Passed

如果所有测试用例通过,则根据参数args执行关键字。

只能用于suite teardown。

53.Run Keyword If Any Critical Tests Failed

如果有Critical测试用例Fail,则根据参数args执行关键字。

只能用于suite teardown。

54.Run Keyword If Any Tests Failed

55.Run Keyword If Test Failed

如果测试用例Fail,则根据参数args执行关键字。

只能用于test teardown。

56.Run Keyword If Test Passed

57.Run Keyword If Timeout Occurred 

如果测试用例超时,则根据参数args执行关键字,只能用于test teardown。

58.Run Keyword Unless 

如果condition为假,根据参数args执行关键字。

59.Run Keywords 

按序执行所有关键字

主要用于setups 和 teardowns中,不想创建更高级别的关键字但是有很多很多动作要进行

set系列

 

通常情况下,每个变量默认都是局部变量。

 

一个case里的变量,作用域在这个case内部;

 

一个userkeyword里的变量,作用域在这个userkeyword内部;

 

一个文件型suite里的变量,作用域在这个suite内部,所有下面的case也都可以使用。

 

一个目录型suite里的变量,作用域在这个目录内,他下面的文件型suite是无法使用的,所以一般在目录下新增变量没有太大意义。

 

作用域是可以修改的,即通过一些系统关键字,对变量进行作用域的设定,常用的关键字有:

 

Set Global Variable ——设定全局级变量

 

Set Suite Variable ——设定suite级变量

 

Set Test Variable ——设定case级变量

 

 

60.name:Set Global Variable

 

Arguments:[ name | *values ]

 

Makes a variable available globally in all tests and suites. 

 

设置全局变量。

 

和命令行使用'--variable' or '--variablefile'创建的效果一样。

 

65.name:Set Suite Variable 

 

Makes a variable available everywhere within the scope of the current suite. 

 

此关键字设置的变量在当前执行的测试套件范围内的任何位置都可用,

 

默认情况下,可能的子测试套件看不到该变量,

 

它的作用域?

 

  第2行,children=true是说能作用于子suite吗?

 

如果变量的值本身是变量(转义或非转义),则必须始终使用转义格式设置变量??

 

此限制也适用于“set test variable”、“set global variable”、“variable should exist”、“variable should not exist”和“get variable value”关键字。

 

71.name:Set Variable

 

主要用于设置标量变量。

 

 

 

此外,它还可以用于将包含列表的标量变量转换为列表变量或多个标量变量。建议在创建新列表时使用“create list”

 

 

本关键字创建的变量仅仅在当前范围有效。

 

72.name:Set Variable if  

 

Arguments:[ condition | *values ]

 

Sets variable based on the given condition. 

 

基于condition设置变量。

 

 ${rc} is assumed to be zero. 

 

   

 

多个Set Variable If关键字可以嵌套,效果类似于'Else If'……

 

61.name:Set Library Search Order

Sets the resolution order to use when a name matches multiple keywords. 

name与多个关键字匹配时,设置库查找顺序

 当测试数据中的关键字名称与多个关键字匹配时,库搜索顺序用于解决冲突。

62.name:Set Log Level

Sets the log threshold to the specified level and returns the old level. 

设置log级别并返回旧的log级别

默认级别是INFO.可用的log级别有:TRACE, DEBUG, INFO (default), WARN and NONE (no logging),对应的信息由多到少

63.name:Set Suite Documentation

64.name:Set Suite Metadata  元数据

 

66.name:Set Tags

Adds given tags for the current test or all tests in a suite. 

在测试用例或者suite中设置标签。

Teardown中不能使用该关键字。

67.name:Set Task Variable

Arguments:[ name | *values ]
Makes a variable available everywhere within the scope of the current task.

 使变量在当前任务范围内的任何位置都可用

68.name:Set Test Documentation

69.name:Set Test Message 

 为当前测试用例设置消息。

 This keyword can not be used in suite setup or suite teardown.

 70.name:Set Test Variable

 

should系列

73.name:Should Be Empty  期望为空

Arguments:[  item | msg=msg ]

Verifies that the given item is empty. 

验证给定的项目为空。

Item的长度通过Get Length关键字获取。

74.name:Should Be Equal   期望相等

Arguments:[ first | second | msg=msg | values=values | ignore_case=ignore_case ]

If msg is given and values gets a true value (default), the error message is <msg>: <first> != <second>.

  这就是message的作用

  省略时,默认的values=true,即发生错误时,要打印出来

75.name:Should Be Equal As Integers

 Arguments:[ first | second | msg=msg | values=values | base=base ]

 Fails if objects are equal after converting them to integers. 

转换为整数数,比较,是否相等

76.name:Should Be Equal As Numbers 

 转换为实数后,期望相等

77.name:Should Be Equal As Strings

转换为字符串后,期望相等

78.name:Should Be True 

 期望condition为true

79.name:Should Contain 

 期望container中包含item

80.name:Should Contain Any               

  期望container中包含itemS中的任意一项

80.name:Should Contain Any  X Times   

期望container中包含item X次

81.name:Should End With     

期望str1以str2结尾

82.name:Should Match   

string匹配模式pattern

模式匹配是和shell中的文件匹配类似,它区分大小写,'*'匹配所有字符,“?”单个字符。

83.name:Should Match Regexp

string能匹配正则表达式pattern

84.name:Should Not Be Empty

变量不能为空

85.name:Should Not Be Equal

期望两个参数不相等

86.name:Should Not Be Equal As Integers

两个参数转换为整数后,期望不相等

87.name:Should Not Be Equal As Numbers

 转换为实数后,是否相等

采用Convert To Number关键字使用指定的精度进行转换。

88.name:Should Not Be Equal As Strings

转换为字符串后,是否相等

89.name:Should Not Be True

 Arguments:[ condition | msg=msg ]

期望condition不为true

90.name:Should Not Contain

 Arguments:[ container | item | msg=msg | values=values | ignore_case=ignore_case ]

Fails if container contains item one or more times. 

期望container中不包含item

91.name:Should Not Contain Any

 Arguments:[ container | *items | **configuration ]

Fails if container contains one or more of the *items. 

期望container中不包含itemS中的任意一项

92.name:Should Not End With

 Arguments:[ str1 | str2 | msg=msg | values=values | ignore_case=ignore_case ]

Fails if the string str1 ends with the string str2. 

93.name:Should Not  Match   

string不能匹配模式pattern

模式匹配是和shell中的文件匹配类似,它区分大小写,'*'匹配所有字符,“?”单个字符。

94.name:Should Not Match Regexp

string不能匹配正则表达式pattern

95.name:Should Not Start With

 Arguments:[ str1 | str2 | msg=msg | values=values | ignore_case=ignore_case ]

不以str2开头

96.name:Should Start With

以str2开头

97.name:Sleep

暂停指定时间

时间可以是一个数字(单位是:秒)或时间字符串。时间字符串格式如'1 day 2 hours 3 minutes 4 seconds 5milliseconds' 或 '1d 2h 3m 4s 5ms'等。

98.name:Variable Should Exist

给定的变量在当前范围内,期望存在

99.name:Variable Should Not Exist

给定的变量在当前范围内,期望不存在

100.name:Wait Until Keyword Succeeds

等到指定的关键字成功或超时

======================

参考1:http://blog.chinaunix.net/uid-20393955-id-3074760.html 
先學習這些吧!熟悉掌握這些API,讓其做更多的事,還有常用API(二)會後繼更新~

无想法就无成就!

-----------------------------------------Have a good day!---------------------------------------------------------------------------------------------------
原文地址:https://www.cnblogs.com/ww-xiaowei/p/10330733.html