RobotFramework自动化测试
一、web自动化测试
1、browserManagement
open browser:打开浏览器
close browser:关闭浏览器
go to:跳转到某个URL
go back:相当于浏览器的回退
select window:选择窗口
close window:关闭窗口
wait until keywords succeeds:与选择窗口配合使用,确保能够选中窗口
select window main:回到主窗口
switch browser:切换浏览器,参数为打开浏览器的index顺序或alias别名
close all browsers:关闭所有浏览器
select window by handle:通过handle选择窗口
get window handles:获取当前线程所有窗口的handles,返回list
get current window handle:获取当前窗口的handle
select frame:选择frame或iframe
unselect frame:跳出所有frame或iframe
2、cookie
cookie是一些网站为辨别用户身份储存在用户本地的数据,有一些可能是涉及敏感信息,比如储存用户的用户名密码、计算机名、访问过的网站等。
add cookie:添加cookie
delete cookie:删除cookie
delete all cookies:删除所有cookie
get cookies:获取所有cookies
get cookie value:获取cookie的值
3、elements
click element:点击元素
click link:点击链接
click image:点击图片
press key \13:模拟按回车键
assign id to element:指定id
mouse down、mouse up、mouse over、mouse out:模拟鼠标操作
get horizontal position、get vertical position:获取当前对象的左上角顶点距离整个页面的横坐标和纵坐标
get value、get text:获取网页元素的value或者text
获取元素其他属性值,get element attribute,比如要获取class值,要用get element attribute element_id@class来获取
submit form:提交表单
select checkbox:勾选复选框(多选按钮)
unselect checkbox:不勾选复选框
select radio button:二选一按钮
text fields
choose file:用于上传文件时选择文件
input text:输入文本
input password:输入密码
click button:点击按钮
selectelement介绍列表框,列表框分为单选和多选
select from list:选择列表框中的某一项
select from list by index、select from list by value、select from list by label:通过index、value、label选择列表项
select all from list:全部选择
unselect from list、select from list by index、select from list by value、select from list by label:取消选择
get list items:获取所有的列表项
get selected list label(s)、get selected list value(s):确认选中的列表项
tableElement
get table cell:获取指定table里指定行列元素的文本值
get index in table column、get index in table row:获取某个文本在表格的哪一行或哪一列
4、JavaScript
execute JavaScript:执行JavaScript脚本
execute async JavaScript:异步执行JavaScript脚本
confirm action:自动进行对话框的确认,默认为确认
要点击取消则需要用choose cancel on next confirmation进行设置,然后再confirm action
要想恢复成默认点确认需要用choose ok on next confirmation进行设置,然后再confirm action
5、screenshot
capture page screenshot:对当前页面截图
6、waiting
显示等待
wait for condition、wait until page contains、wait until page contains element
二、接口自动化测试
1、关键字
create session:创建一个session,连接某个服务器
create ntlm session:也是创建一个session,只不过加上了域、用户名、密码用于NTLM认证
get request:通过GET方式发起请求
post request:通过POST方式发起请求
head request:发送一个HEAD请求
to json:将文本转换成json对象
三、 RF自动化测试库
标准库 (standard library)
Robot Framework可以直接导入使用的库,包括:
• Builtin:包含经常需要的关键字。自动导入无需import,因此总是可用的
• Dialogs:提供了暂停测试执行和从用户的输入方式。
• Collections:提供一组关键词处理Python列表和字典。
• OperatingSystem:允许执行各种操作系统相关的任务。允许执行各种操作系统相关的任务,使各种操作系统相关的任务在robotframework正在运行的系统中执行。
• Remote:远程库接口的一部分。没有自己的任何关键字,作为robotframework和测试库之间的代理的特殊库。实际测试库可以在不同的机器上运行,可以使用任何编程语言支持XML-RPC协议的实现。
• Screenshot:提供关键字来捕获和存储桌面的截图。
• String:用于处理字符串并验证它们的内容的库,用于生成、修改和验证字符串
• Telnet:支持连接到Telnet服务器上打开的连接执行命令。
• XML:用于生成、修改和验证XML文件的库。
• Process:系统中运行过程的库。
• DateTime:日期和时间转换的库,支持创建和验证日期和时间值以及它们之间的计算
扩展库 (external library)
Robot Framework需要下载安装后才能使用的库,包括:
• HTTP library (livetest):内部使用LiveTest工具的HTTP测试的库。
• HTTP library (Requests):内部使用request工具的HTTP测试的库。
• Database Library (Java):基于Java的数据库测试库。也可使用Jython和Maven central.
• Database Library (Python):基于Python数据库测试库。支持任何Python解释器,包括Jython。
• seleniumlibrary:Web测试库,内部使用比较流行的selenium工具。利用早期的selenium1.0和本身已经过时。
• selenium2library:使用selenium2的Web测试库。替换了大部分老的seleniumlibrary。
• Django Library:为Django的库,一个Python Web框架。
• Archive library:处理.zip和.tar压缩包的库。
• Diff Library:比较两个文件的库。
• FTP library:Robot Framework上测试和使用FTP服务的库。
• SSHLibrary:通过SSH连接的在远程机器上执行命令。还支持使用SFTP进行文件传输
• rammbock:通用的网络协议测试库;提供简单的方法来指定网络数据包,并检查发送和接收数据包的结果。
1、标准库——BuiltIn
1、提供的关键字有用于验证的(e.g. Should Be Equal, Should Contain),转换的(e.g. Convert To Integer),其他(e.g. Log, Sleep, Run Keyword If, Set Global Variable)。
(1)Call Method,调用方法
Arguments: [object | method_name | *args | **kwargs ]
Calls the named method of the given object with the provided arguments.
调用对象object的方法method_name
Examples:
[Arguments] ${object} ${method_name} @{args}
${res} Call Method ${object} ${method_name} @{args}
[Return] ${res}
(2)Catenate,连接给定的字符串,返回拼接后的字符串
Arguments:[ *items ]
Catenates the given items together and returns the resulted string.
连接给定参数,返回字符串(默认用空格连接)
Examples:
${str1} = Catenate Hello world
${str2} = Catenate SEPARATOR=— Hello world
${str3} = Catenate SEPARATOR= Hello world
实际结果:
- ${str1} = ‘Hello world’
- ${str2} = ‘Hello—world’
- ${str3} = ‘Helloworld’
(3)Comment
Arguments:[ *messages ]
Displays the given messages in the log file as keyword arguments.
在日志文件中以关键字参数形式显示指定信息。
Comment对参数不进行任何处理,仅仅是显示在日志中,用于显示简单信息。
(4)Continue For Loop
Arguments:[ ]
Skips the current for loop iteration and continues from the next.
跳出当前迭代,继续下一个迭代
(5)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)Convert To Boolean,将给定的对象转成布尔值,true或false
(7)Convert To Bytes 根据输入类型将给定的输入转换为字节
(8)Convert To Hex 转换参数为十六进制字符串
Examples:
${result} = Convert To Hex 255 # Result is FF
(9)Convert To Integer 转换参数为整数(后面只能传整数字符串参数)
Examples:
${result} = Convert To Integer 100 # Result is 100
${result} = Convert To Integer FFAA 16 # Result is 65450
${result} = Convert To Integer 100 8 # Result is 64
${result} = Convert To Integer -100 2 # Result is -4
(10)Convert To Number 转浮点数,可以指定几位小数
Arguments:[ item | precision=precision ]
Converts the given item to a floating point number.
转换参数为浮点数。
Examples:
${result} = Convert To Number 42.512 # Result is 42.512
${result} = Convert To Number 42.512 1 # Result is 42.5
${result} = Convert To Number 42.512 0 # Result is 43.0
${result} = Convert To Number 42.512 -1 # Result is 40.0
(11)Convert To Octal 转换参数为八进制字符串
(12)Convert To string 转换参数为Unicode字符串
(13)Create Dictionary,创建并返回一个字典
Arguments:[ *items ]
Creates and returns a dictionary based on the given items.
根据给定参数创建字典
Examples:
&{dict}= Create Dictionary key value
Tips: robotframework可以使用 变量 , 列表 或 字典, 使用语法: ${变量} @{列表} 和 &{字典}
(14)Create List,创建并返回一个list
Arguments:[ *items ]
Returns a list containing given items. The returned list can be assigned both to ${scalar} and @{list} variables.
根据给定参数创建list,同时返回的列表可以被分配给 ${变量} @{列表}使用。
Examples:
@{list}= Create List a b c # @{list=[a b c]}
(15)Evaluate
Arguments:[ *items ]
Arguments:[ expression | modules=modules | namespace=namespace ]
Evaluates the given expression in Python and returns the results.
用Python计算表达式并返回结果。
(16)Exit For Loop
立即停止执行for循环。
(17)Fail ,错误,给出消息
停止执行当前测试用例
Fails the test with the given message and optionally alters its tags.
遇到Fail后,本case后面的语句不执行,但是不影响后面case的执行
(18)Fatal Error ,停止整个测试执行
Stops the whole test execution.
遇到Fatal Error,本case后面的语句不执行,且后面的case也都fail
(19)Get Count
Arguments:[ item1 | item2 ]
Returns and logs how many times item2 is found from item1.
返回并在日志中记录item2在Item1中出现的次数。
Examples:
${res} get count abca a
log ${res} #res为2
(20)Get Length,返回item的长度
Arguments:[ item]
Returns and logs the length of the given item as an integer.
返回并在日志中记录item的长度. item可以是任何有一个长度的对象,例如,字符串,列表,或映射.
Examples:
${res} Get Length abca
log ${res} #res为4
(21)Get Library Instance
返回指定测试库的当前活动实例。
Examples:
${res} Get Library Instance name=BuiltIn
log ${res}
(22)Get Time
Arguments:[ format=format | time_=time_ ]
Returns the given time in the requested format.
按指定格式返回时间
1)如果格式中包含epoch,返回的是(Jan 1, 1970 0:00:00)开始的秒数。
2)如果格式包含任何’year’, ‘month’, ‘day’, ‘hour’, ‘min’, or ‘sec’,则返回对应部分。
3)默认格式’2006-02-24 15:08:31’
默认返回当前时间。
Examples:
${time} Get Time
log ${time} #2019-01-29 11:19:01
${time2} Get Time epoch #1548731941
${time3} Get Time return year #2019
${yyyy} ${mm} ${dd} Get Time year,month,day #2019 #01 #29
(23)Get Variable Value ,取参数值
Arguments:[ name, default=None ]
Returns variable value or default if the variable does not exist.
返回变量值,如果变量不存在就返回default
(24)Get Variables ,取变量
返回包含在当前范围内的所有变量的字典
Examples:
${mydict} Create Dictionary name duzl age
${x}= Get Variables ${mydict}
log ${x}
(25)Import Library
在test suite导入库。
可以在测试执行时动态导入库。支持库名和绝对路径导入。使用’WITH NAME’还可以更改库名。
(26)Import Resource
在test suite导入资源文件。path必须是绝对路径。斜线/作为路径分隔符(包含Windows)。
(27)Import Variables
导入变量文件
(28)Keyword Should Exist,除非给定的关键字存在,不然就报错
Arguments:[ name | msg=msg ]
Fails unless the given keyword exists in the current scope
除非关键字在当前作用域存在,否则失败。有多个同名的关键字存在也会Fail
(29)Length Should Be,长度应该为多少
Arguments: [ item | length | msg=msg ]
Verifies that the length of the given item is correct.
验证item的长度。(先通过关键字Get Length获取item的长度。再使用该方法验证)
(30)Log ,打印日志信息
在指定日志级别下记录message。
有效的日志级别有TRACE, DEBUG, INFO (default), HTML and WARN。
注意,无效的HTML会破坏整个日志文件,一定要谨慎使用。HTML消息实际上是使用了INFO级别。
WARN级别记录的消息将在控制台和日志中测试执行的错误部分都可以看到。
(31)Log Many
在INFO级别下记录messages为独立的条目。分别打印每一项元素。
log 相当于python中的print 打印功能,log many 用于打印一个列表。
(32)Log To Console
把信息在控制台打印出来
(33)Log Variables
用给定的日志级别记录当前作用域中的所有变量。
(34)No Operation
无操作,不做任何事情
(35)Pass Execution
Arguments:[ message | *tags ]
Skips rest of the current test, setup, or teardown with PASS status.
无条件的跳过后面的步骤
(36)Pass Execution if
满足条件时,跳过后面的步骤
(37)Regexp Escape
Returns each argument string escaped for use as a regular expression.
返回转义后用作正则表达式的每个参数字符串
此关键字可用于转义要与“should match regexp”和“should not match regexp”关键字一起使用。
(38)Reload Library
重新检查指定库提供的关键字。
可以在测试数据中显式调用,也可以在它提供的关键字发生更改时由库本身调用。
(39)Remove Tags
从当前测试用例或suite中删除标签 。
Arguments:[ tags ]
Tag可以精确地给出,或者使用模式:’'匹配所有字符,“?”单个字符的通配符给出。
Remove Tags mytag something-* ?ython
(40)Repeat Keyword
Arguments:[ repeat | name | *args ]
Executes the specified keyword multiple times.
执行指定的关键字times次 或多少时间(e.g. 1 minute, 2 min 3 s)
(41)Replace Variables
替换text中的变量。
如果文本中包含未定义的变量时,关键字执行Fail。
(42)Return From Keyword
Arguments:[ *return_values ]
Returns from the enclosing user keyword.
此关键字可用于从具有pass状态的用户关键字返回,而无需完全执行它。
此关键字通常包装为其他关键字,如“run keyword if”或“run keyword if test passed”,以根据条件返回:
(43)Return From Keyword if
(44)Run Keyword
Arguments:[ name | *args ]
Executes the given keyword with the given arguments.
根据参数args执行关键字name。
name可以动态设定,例如,来自另一个关键字的返回值或命令行。
(45)Run Keyword And Continue On Failure
根据参数args执行关键字name,错误时依旧继续执行。
(46)Run Keyword And Expect Error
Arguments:[ expected_error | name | args ]
运行关键字并检查是否发生了预期的错误。
预期的错误以与robot框架报告相同的格式给出预期错误。默认情况下,它被解释为带有、?的全局模式。和[chars]作为通配符。
从robot framework 3.1开始,预期错误可以使用下表中解释的各种前缀进行更改。前缀区分大小写,必须用冒号和可选的空格(如prefix:message或prefix:message)将其与实际消息分开。
(47)Run Keyword And Ignore Error
根据参数args执行关键字,并忽略错误
(48)Run Keyword And Return
(49)Run Keyword And Return if
(50)Run Keyword And Return Status
返回状态值作为一个Boolean变量。
(51)Run Keyword If
满足条件时run keyword
(52)Run Keyword If All Critical Tests Passed
如果所有Critical测试用例通过,则根据参数args执行关键字。
只能用于suite teardown。
(53)Run Keyword If All Tests Passed
如果所有测试用例通过,则根据参数args执行关键字。
只能用于suite teardown。
(54)Run Keyword If Any Critical Tests Failed
如果有Critical测试用例Fail,则根据参数args执行关键字。
只能用于suite teardown。
(55)Run Keyword If Any Tests Failed
(56)Run Keyword If Test Failed
如果测试用例Fail,则根据参数args执行关键字。
只能用于test teardown。
(57)Run Keyword If Test Passed
(58)Run Keyword If Timeout Occurred
如果测试用例超时,则根据参数args执行关键字,只能用于test teardown。
(59)Run Keyword Unless
如果condition为假,根据参数args执行关键字。
(60)Run Keywords
按序执行所有关键字
主要用于setups 和 teardowns中,不想创建更高级别的关键字但是有很多很多动作要进行
(61)Set Global Variable,设置全局变量
Arguments:[ name | *values ]
Makes a variable available globally in all tests and suites.
和命令行使用’–variable’ or '–variablefile’创建的效果一样。
(62)Set Suite Variable ,设定suite级变量
,Makes a variable available everywhere within the scope of the current suite.
此关键字设置的变量在当前执行的测试套件范围内的任何位置都可用,
默认情况下,可能的子测试套件看不到该变量,
(63)Set Test Variable ——设定case级变量
(64)Set Variable
主要用于设置标量变量。
(65)Set Variable if
Arguments:[ condition | *values ]
Sets variable based on the given condition.
基于condition设置变量。
(66)Set Library Search Order
Sets the resolution order to use when a name matches multiple keywords.
name与多个关键字匹配时,设置库查找顺序
当测试数据中的关键字名称与多个关键字匹配时,库搜索顺序用于解决冲突。
(67)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),对应的信息由多到少
(68)Set Suite Documentation
(69)Set Suite Metadata 元数据
(70)Set Tags
Adds given tags for the current test or all tests in a suite.
在测试用例或者suite中设置标签。
Teardown中不能使用该关键字。
(71)Set Task Variable
Arguments:[ name | *values ]
Makes a variable available everywhere within the scope of the current task.
使变量在当前任务范围内的任何位置都可用
(72)Set Test Documentation
(73)Set Test Message
为当前测试用例设置消息。
(74)Should Be Empty 期望为空
Arguments:[ item | msg=msg ]
Verifies that the given item is empty.
验证给定的项目为空。
Item的长度通过Get Length关键字获取。
(75)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 : != .
(76)Should Be Equal As Integers
Arguments:[ first | second | msg=msg | values=values | base=base ]
Fails if objects are equal after converting them to integers.
转换为整数数,比较,是否相等
(77)Should Be Equal As Numbers
转换为实数后,期望相等
(78)Should Be Equal As Strings
转换为字符串后,期望相等
(79)Should Be True
期望condition为true
(80)Should Contain
期望container中包含item
(81)Should Contain Any
期望container中包含itemS中的任意一项
(82)Should Contain Any X Times
期望container中包含item X次
(83)Should End With
期望str1以str2结尾
(84)Should Match
string匹配模式pattern
模式匹配是和shell中的文件匹配类似,它区分大小写,’*'匹配所有字符,“?”单个字符。
(85)Should Match Regexp
string能匹配正则表达式pattern
(86)Should Not Be Empty
变量不能为空
(87)Should Not Be Equal
期望两个参数不相等
(88)Should Not Be Equal As Integers
两个参数转换为整数后,期望不相等
(89)Should Not Be Equal As Numbers
转换为实数后,是否相等
采用Convert To Number关键字使用指定的精度进行转换。
(90)Should Not Be Equal As Strings
转换为字符串后,是否相等
(91)Should Not Be True
Arguments:[ condition | msg=msg ]
期望condition不为true
(92)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
(93)Should Not Contain Any
Arguments:[ container | *items | **configuration ]
Fails if container contains one or more of the *items.
期望container中不包含itemS中的任意一项
(94)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.
(95)Should Not Match
string不能匹配模式pattern
模式匹配是和shell中的文件匹配类似,它区分大小写,’*'匹配所有字符,“?”单个字符。
(96)Should Not Match Regexp
string不能匹配正则表达式pattern
(97)Should Not Start With
Arguments:[ str1 | str2 | msg=msg | values=values | ignore_case=ignore_case ]
不以str2开头
(98)Should Start With
以str2开头
(99)Sleep
暂停指定时间
时间可以是一个数字(单位是:秒)或时间字符串。时间字符串格式如’1 day 2 hours 3 minutes 4 seconds 5milliseconds’ 或 '1d 2h 3m 4s 5ms’等。
(100)Variable Should Exist
给定的变量在当前范围内,期望存在
(101)Variable Should Not Exist
给定的变量在当前范围内,期望不存在
(102)Wait Until Keyword Succeeds
等到指定的关键字成功或超时
2、标准库——String
https://blog.csdn.net/weixin_33915554/article/details/93562949
(1)Convert To Lowercase,将字符串转换为小写形式。
Converts string to lowercase.
Arguments:[string]
Examples:
${str1}= Convert To Lowercase ABC
${str2}= Convert To Lowercase 1A2c3D
Should Be Equal ${str1} abc
Should Be Equal ${str2} 1a2c3d
(2)Convert To Uppercase,将字符串转换为大写。
Arguments:[string]
Converts string to uppercase.
Examples:
${str1} = Convert To Uppercase abc
${str2} = Convert To Uppercase 1a2C3d
Should Be Equal ${str1} ABC
Should Be Equal ${str2} 1A2C3D
(3)Decode Bytes To String,使用给定的编码将给定的字节解码为Unicode字符串。
Arguments:[bytes, encoding,errors=strict]
Decodes the given bytes to a Unicode string using the given encoding.
errors argument controls what to do if decoding some bytes fails. All values accepted by decode method in Python are valid, but in practice the following values are most useful:
strict: fail if characters cannot be decoded (default)
ignore: ignore characters that cannot be decoded
replace: replace characters that cannot be decoded with a replacement character
使用给定的编码将给定的字节解码为Unicode字符串。错误参数控制在解码某些字节失败时该做什么。Python中的decode方法接受的所有值都是有效的,但在实践中下列值最有用:
严格:如果无法解码字符将失败(默认)
忽略:忽略无法解码的字符
替换:替换无法用替换字符解码的字符
Examples:
${string} = Decode Bytes To String ${bytes} UTF-8
${string} = Decode Bytes To String ${bytes} ASCII errors=ignore
(4)Encode String To Bytes,使用给定的编码将给定的Unicode字符串编码为字节。
Arguments:[string, encoding,errors=strict]
Encodes the given Unicode string to bytes using the given encoding.
errors argument controls what to do if encoding some characters fails. All values accepted by encode method in Python are valid, but in practice the following values are most useful:
strict: fail if characters cannot be encoded (default)
ignore: ignore characters that cannot be encoded
replace: replace characters that cannot be encoded with a replacement character
使用给定的编码将给定的Unicode字符串编码为字节。错误参数控制在编码某些字符失败时该做什么。Python中的encode方法接受的所有值都是有效的,但在实践中下列值最有用:
严格:如果字符不能编码,则失败(默认)
忽略:忽略无法编码的字符
替换:替换不能用替换字符编码的字符
Examples:
${bytes} = Encode String To Bytes ${string} UTF-8
${bytes} = Encode String To Bytes ${string} ASCII errors=ignore
(5)Fetch From Left,返回标记第一次出现之前的字符串内容。
Arguments:[string, marker]
如果没有找到标记,则返回整个字符串。
(6)Fetch From Right,返回标记最后一次出现后字符串的内容。
Arguments:[string, marker]
如果没有找到标记,则返回整个字符串。
(7)Generate Random String,从给定字符生成一个期望长度的字符串。
Arguments:[length=8, chars=[LETTERS][NUMBERS]]
填充序列字符包含生成随机字符串时使用的字符。它可以包含任何字符,可以使用特殊的标记解释在下表:
Marker Explanation
[LOWER] Lowercase ASCII characters from a to z.
[UPPER] Uppercase ASCII characters from A to Z.
[LETTERS] Lowercase and uppercase ASCII characters.
[NUMBERS] Numbers from 0 to 9.
(8)Get Line,从给定字符串返回指定的行。
Arguments:[string, line_number]
行编号从0开始,可以使用负指标(负数)来指代台词。返回的行没有换行符。
Examples:
${first} = Get Line ${string} 0
${2nd last} = Get Line ${string} -2
(9)Get Line Count,返回并记录给定字符串中的行数。
Arguments:[string]
(10)Get Lines Containing String,返回包含模式的给定字符串的行。
Arguments:[string, pattern,case_insensitive=False]
模式通常被认为是一个普通的字符串,而不是一个glob或regexp模式。如果模式在行上的任何位置找到,则行匹配。
默认情况下,匹配是大小写敏感的,但是给case_insensitive一个true值会使它不区分大小写。如果一个非空字符串不等于false或no,则认为该值为true。如果该值不是字符串,则在Python中直接获取其真值。
行作为一个字符串返回,并与换行连接在一起。可能的后置换行永远不会返回。匹配的行数被自动记录下来。
Examples:
${lines} = Get Lines Containing String ${result} An example
${ret} = Get Lines Containing String ${ret} FAIL case-insensitive
(11)Get Lines Matching Pattern,返回与模式匹配的给定字符串的行。
Arguments:[string, pattern,case_insensitive=False]
只有完全匹配模式的行才匹配。
默认情况下,匹配是大小写敏感的,但是给case_insensitive一个true值会使它不区分大小写。如果一个非空字符串不等于false或no,则认为该值为true。如果该值不是字符串,则在Python中直接获取其真值。
行作为一个字符串返回,并与换行连接在一起。可能的后置换行永远不会返回。匹配的行数被自动记录下来。
Examples:
${lines} = Get Lines Matching Pattern ${result} Wild??? example
${ret} = Get Lines Matching Pattern ${ret} FAIL: * case_insensitive=true
(12)Get Lines Matching Regexp,返回与regexp模式匹配的给定字符串的行。
Arguments:[string, pattern,partial_match=False]
内装式。有关Python正则表达式语法的更多信息,特别是如何在Robot框架测试数据中使用它,请与Regexp匹配。
默认情况下,只有完全匹配模式的行才匹配,但是可以通过为partial_match参数提供一个真值来启用部分匹配。如果一个非空字符串不等于false或no,则认为该值为true。如果该值不是字符串,则在Python中直接获取其真值。
如果模式为空,则默认情况下它只匹配空行。当启用部分匹配时,空模式将匹配所有行。
注意,要使匹配不区分大小写,需要在模式前面加上不区分大小写的标志(?i)。
行作为一个字符串返回,并与新行连接在一起。可能的后置换行永远不会返回。匹配的行数被自动记录下来。
Examples:
${lines} = Get Lines Matching Regexp ${result} Reg\w{3} example
${lines} = Get Lines Matching Regexp ${result} Reg\w{3} example partial_match=true
${ret} = Get Lines Matching Regexp ${ret} (?i)FAIL: .*
(13)Get Regexp Matches,返回给定字符串中所有非重叠匹配的列表。
Arguments:[string, pattern,*groups]
string是查找匹配项的字符串,pattern是正则表达式。内装式。有关Python正则表达式语法的更多信息,特别是如何在Robot框架测试数据中使用它,请与Regexp匹配。
如果没有使用组,则返回的列表包含完整的匹配项。如果使用一个组,则列表只包含该组的内容。如果使用多个组,则列表包含包含单个组内容的元组。所有组都可以指定为索引(从1开始),命名组也可以指定为名称。
Examples:
${no match} = Get Regexp Matches the string xxx
${matches} = Get Regexp Matches the string t…
${one group} = Get Regexp Matches the string t(…) 1
${named group} = Get Regexp Matches the string t(?P…) name
${two groups} = Get Regexp Matches the string t(.)(.) 1 2
=>
${no match} = []
${matches} = [‘the’, ‘tri’]
${one group} = [‘he’, ‘ri’]
${named group} = [‘he’, ‘ri’]
${two groups} = [(‘h’, ‘e’), (‘r’, ‘i’)]
(14)Get Substring,返回从起始索引到结束索引的子字符串。
Arguments:[string, start,end=None]
可截取到想要的自字符串,开始索引是包含性的,结束是排他性的。索引从0开始,可以使用负索引来引用结尾的字符。
Examples:
${ignore first} = Get Substring ${string} 1
${ignore last} = Get Substring ${string} -1
${5th to 10th} = Get Substring ${string} 4 10
${first two} = Get Substring ${string} 1
${last two} = Get Substring ${string} -2
(15)Remove String,从给定字符串中删除所有可移除项。
Arguments:[string, *removables]
移除掉字符串中给定的字符
removables用作文字字符串。每个可移动的对象都将匹配一个临时字符串,之前的可移动对象已经从该字符串中删除。参见下面的第二个示例。
如果需要更强大的模式匹配,请使用Regexp删除字符串。如果只需要删除一定数量的匹配项,则可以使用Replace String或使用Regexp替换字符串。
返回字符串的修改版本,并且不改变原始字符串。
Examples:
${str} = Remove String Robot Framework work
Should Be Equal ${str} Robot Frame
${str} = Remove String Robot Framework o bt
Should Be Equal ${str} R Framewrk
(16)Remove String Using Regexp,从给定字符串中删除模式。
Arguments:[string, *patterns]
这个关键字与删除字符串是相同的,但是要搜索的模式被认为是一个正则表达式。有关正则表达式语法的更多信息,请参见使用Regexp替换字符串。如果只需要删除一定数量的出现,也可以使用该关键字。
(17)Replace String,用replace_with替换给定字符串中的search_for。
Arguments:[string, search_for,replace_with,count=-1]
search_for用作文字字符串。如果需要更强大的模式匹配,请参阅使用Regexp替换字符串。如果您需要删除一个字符串,请参阅删除字符串。
如果提供了可选的参数计数,则只替换从左到右出现的次数。负数意味着所有发生的事件都被替换(默认行为),而0意味着什么都不做。
返回字符串的修改版本,并且不改变原始字符串。
Examples:
${str} = Replace String Hello, world! world tellus
Should Be Equal ${str} Hello, tellus!
${str} = Replace String Hello, world! l ${EMPTY} count=1
Should Be Equal ${str} Helo, world!
(18)Replace String Using Regexp,用replace_with替换给定字符串中的模式。
Arguments:[string, pattern,replace_with,count=-1]
这个关键字与Replace String完全相同,但是要搜索的模式被认为是一个正则表达式。内装式。有关Python正则表达式语法的更多信息,特别是如何在Robot框架测试数据中使用它,请与Regexp匹配。
如果需要删除一个字符串,请使用Remove String Using Regexp.。
Examples:
${str} = Replace String Using Regexp ${str} 20\d\d-\d\d-\d\d <DATE>
${str} = Replace String Using Regexp ${str} (Hello|Hi) ${EMPTY} count=1
(19)Should Be Byte String,如果给定的项不是字节字符串,则失败。
Arguments:[item, msg=None]
如果您想验证项目是否是Unicode字符串,那么用 Should Be Unicode String;如果Unicode和字节字符串都没问题,那么用 Should Be String。有关Unicode字符串和字节字符串的详细信息,请参阅“ Should Be Unicode String”。
可以使用可选的msg参数覆盖默认的错误消息。
(20)Should Be Lowercase,如果给定字符串不是小写的,则失败。
Arguments:[string, msg=None]
例如,“string”和“with specials!”'会通过,‘String’, '会失败。
可以使用可选的msg参数覆盖默认的错误消息。
(21)Should Be String,如果给定的项不是字符串,则失败。
Arguments:[item, msg=None]
可以使用可选的msg参数覆盖默认的错误消息。
(22)Should Be Titlecase,如果给定的字符串不是标题,则失败。
Arguments:[string, msg=None]
如果字符串中至少有一个字符,大写字符只跟在无大小写字符后面,小写字符只跟在大小写字符后面,那么它就是一个有标题的字符串。
例如,'This Is Title’将通过,‘Word In UPPER’, ‘Word In lower’和’'将失败。
可以使用可选的msg参数覆盖默认的错误消息。
(23)Should Be Unicode String,如果给定的项不是Unicode字符串,则失败。
Arguments:[item, msg=None]
可以使用可选的msg参数覆盖默认的错误消息。
(24)Should Be Uppercase,如果给定的字符串不是大写的,则失败。
Arguments:[string, msg=None]
例如,“STRING”和“WITH SPECIALS!”'会通过,‘String’, '会失败。
可以使用可选的msg参数覆盖默认的错误消息。
(25)Should Not Be String,如果给定的项是字符串,则失败。
Arguments:[item, msg=None]
(26)Split String,使用分隔符分隔字符串。
Arguments:[string,separator=None,max_split=-1]
如果没有提供分隔符,任何空白字符串都是分隔符。在这种情况下,也可能忽略连续的空白以及开头和结尾的空白。
分割词作为列表返回。如果给定了可选的max_split,那么最多执行max_split拆分,并且返回的列表将具有最大的max_split + 1元素。
Examples:
@{words} = Split String ${string}
@{words} = Split String ${string} ,${SPACE}
${pre} ${post} = Split String ${string} :: 1
(27)Split String From Right,使用分隔符从右开始分割字符串。
Arguments:[string,separator=None,max_split=-1]
与 Split String相同,但分割是从右开始的。只有在给定max_split时才会产生效果。
Examples:
${first} ${rest} = Split String ${string} - 1
${rest} ${last} = Split String From Right ${string} - 1
(28)Split String To Characters,将给定的字符串拆分为字符。
Arguments:[string]
Example:
@{characters} = Split String To Characters ${string}
(29)Split To Lines,将给定的字符串分成几行。
Arguments:[string, start=0,end=None]
从开始到结束可能只得到一个行选择,这样开始索引就包含而结束是排他的。行编号从0开始,可以使用负索引来引用从末端开始的行。
返回没有换行的行。返回的行数被自动记录下来。
Examples:
@{lines} = Split To Lines ${manylines}
@{ignore first} = Split To Lines ${manylines} 1
@{ignore last} = Split To Lines ${manylines} -1
@{5th to 10th} = Split To Lines ${manylines} 4 10
@{first two} = Split To Lines ${manylines} 1
@{last two} = Split To Lines ${manylines} -2
(30)Strip String,从给定字符串中删除开头和/或结尾的空白。
Arguments:[ string, mode=both,characters=None]
模式可以是左移删除前导字符,右移删除尾随字符,两者(默认)都删除字符串两边的字符,或者不删除返回未修改的字符串。
如果提供了可选字符,那么它必须是一个字符串,并且字符串中的字符将在字符串中被删除。请注意,这不是要删除的子字符串,而是字符列表,参见下面的示例。
Examples:
${stripped}= Strip String ${SPACE}Hello${SPACE}
Should Be Equal ${stripped} Hello
${stripped}= Strip String ${SPACE}Hello${SPACE} mode=left
Should Be Equal ${stripped} Hello${SPACE}
${stripped}= Strip String aabaHelloeee haracters=abe
Should Be Equal ${stripped} Hello