在robotframework当中,要实现App自动化,则需要使用AppiumLibrary这个库。

目前版本中,有90+关键字。随着版本的更新,关键字的个数和名字也会有所变动。

http://serhatbolsu.github.io/robotframework-appiumlibrary/AppiumLibrary.html

以下列举常用的关键字:

连接设备并打开App使用的关键字:
RF AppiumLibrary 关键字分类解读

一、实用函数

关键字

含义

实例

备注

Click Button

点击按钮

Click Button 注销

这里“注销”的class属性,必须是Button

Click Element

点击元素

Click Element [locator]

Locator可以是resource-id,也可以是xpath;但必须是当前页面唯一存在的;一般用该关键字可以完成所有元素点击

Click Text

点击文字

Click Text 我的

“我的”需要是当前页面唯一存在的

Input Text

在指定元素中输入文本

InputText [locator] 111111

Locator可以是resource-id,也可以是xpath

Swipe

滑动屏幕

Swipe 1000 1800 100 1800

 

 

Get Text

获取某标签内容

${account}   Get Text  [locator]  

获取内容后存储到指定变量中,从而在后续脚本中调用该内容

 

二、校验函数

关键字

含义

实例

备注

Element Name Should Be

检查元素的name属性

 

Element Name Should Be  [locator]  登录

locator可以是resource-id,也可以是xpath

Element Should Be Enabled

检查元素是否可用/可见

Element Should Be Enabled  [locator]

 

Page Should Contain Element

检查界面包含某些元素

Page Should Contain Element   [locator]

 

Page Should Contain Text

检查界面包含某些文字信息

Page Should Contain Text  预约挂号

 

Page Should Not Contain Element

检查界面不包含某些元素

Page Should Not Contain Element  [locator]

 

Page Should Not Contain Text

检查界面不包含某些文字信息

Page Should Not Contain Text  预约挂号

 

 

三、等待关键字

(关键字中包含wait的, timeout参数默认为seleniumlibrary初始化值,默认为5秒):

关键字

参数

说明

Wait Activity activity, timeout, interval=1

等待页面出现:等待中,直到目标页面出现或超时失败。

仅适用安卓。

activity - 目标页面
timeout - 最长等待时间,以秒为单位
interval - 重试之间的等待间隔(秒)

Wait Until Element Is Visible locator, timeout=None, error=None

等待直到用定位器指定的元素可见。如果在元素可见之前超时,则失败。

error - 可用于覆盖默认错误消息。

Wait Until Page Contains

text, timeout=None, error=None

等待直到界面中包含某些文字,才进行下一步

Wait Until Page Contains  预约挂号

Wait Until Page Contains Element

locator, timeout=None, error=None

等待直到界面中包含某些元素,才进行下一步

Wait Until Page Contains Element  [locator]

locator可以是resource-id,也可以是xpath

Wait Until Page Does Not Contain

text, timeout=None, error=None

等待直到界面中不包含某些文字,才进行下一步

Wait Until Page Does Not Contain  预约挂号

Wait Until Page Does Not Contain Element

 

locator, timeout=None, error=None

等待直到界面中不包含某些元素,才进行下一步

Wait Until Page Does Not Contain Element  [locator]

 

 

四、断言关键字(关键字中包含should的均是):

Element Attribute Should Match locator, attr_name, match_pattern, regexp=False Verify that an attribute of an element matches the expected criteria.
Element Name Should Be locator, expected  
Element Should Be Disabled locator, loglevel=INFO  
Element Should Be Enabled locator, loglevel=INFO  
Element Should Be Visible locator, loglevel=INFO  
Element Should Contain Text locator, expected, message=  
Element Should Not Contain Text locator, expected, message=  
Element Text Should Be locator, expected, message=  
Element Value Should Be locator, expected  
Page Should Contain Element locator, loglevel=INFO  
Page Should Not Contain Element locator, loglevel=INFO  
Page Should Contain Text text, loglevel=INFO  
Page Should Not Contain Text text, loglevel=INFO  
Text Should Be Visible text, exact_match=False, loglevel=INFO  
Xpath Should Match X Times xpath, count, error=None, loglevel=INFO  

 

五、其他函数

1.条件判断函数

关键字:Run Keyword If

用法:

Run Keyword If

…ELSE IF   

…ELSE

实例:

1)用Run Keyword If执行单条语句时

RF AppiumLibrary 关键字分类解读

2)用Run Keyword If执行多条语句时,在需要执行多条语句的if或else if或else后,增加关键字:Run Keywords

RF AppiumLibrary 关键字分类解读

 

 2.检查某关键字的返回状态

关键字:Run Keyword And Return Status

返回值:布尔值,True False

实例   :通常和Run Keyword If关键字搭配使用       

${isMessage} Run Keyword And Return Status Wait Until Page Contains Element [locator]
Run Keyword If ${isMessage} do something  

 

 

含义  :判断当前界面中是否存在取消按钮;如果存在,则do something

 

3.For循环

关键字:For

实例:

1)For循环单独使用时

RF AppiumLibrary 关键字分类解读

2)For循环嵌套Run Keyword If语句,退出循环

RF AppiumLibrary 关键字分类解读

 

4.自定义关键字

创建方法:测试项目-New Resource-New User Keyword

实例:

RF AppiumLibrary 关键字分类解读

在需要使用自定义关键字的测试套件Edit标签页,Resource导入关键字,导入结果为蓝色,则导入正确

RF AppiumLibrary 关键字分类解读

用法:导入resource后,在测试用例中,直接调用

RF AppiumLibrary 关键字分类解读

相关文章: