AppiumLibrary中文翻译

AppiumLibrary

原文链接: http://serhatbolsu.github.io/robotframework-appiumlibrary/AppiumLibrary.html

版本:1.5.0.7
生效范围:GLOBAL
具名参数:支持

介绍

AppiumLibrary是一个Robot Framework的移动应用测试库

定位及指定元素

AppiumLibrary中,所有关键字都需要一个定位符locator或页面元素webelement参数,以便在页面上找到元素。 定位符locator是描述定位策略及语法的一个字符串。 页面元素webelement是一个代表页面元素的WebElement对象。

使用定位符

默认情况下,提供定位符后,它会根据指定的关键属性来匹配特定元素类型。 对于iOS和Android,id 是所有元素的关键属性,只需使用id即可轻松定位元素,例如:

Click  Element  id=my_element

AppiumLibrary1.4中的新增功能,使用id和xpath定位可以省略定位方式。xpath表达式以//开头时,可以省略定位方式xpath,否则需要明确指定使用xpath定位。

例如:

Click Element  my_element
Wait Until Page Contains Element  //*[@type="android.widget.EditText"]

Appium还支持Mobile JSON Wire Protocol的一些其他定位策略。 AppiumLibrary还支持,通过指定定位方式加定位表达式来定位元素。支持的定位方式有:

定位方式示例说明注意
identifierClick Element identifier=my_element通过id属性定位
idClick Element id=my_element通过resource-id属性定位
accessibility_idClick Element accessibility_id=button3通过content-desc属性定位
xpathClick Element xpath=//UIATableView/UIATableCell/UIAButton通过任意XPath定位
classClick Element class=UIAPickerWheel通过class定位
androidClick Element android=UiSelector().description(‘Apps’)通过Android UI Automator定位
iosClick Element ios=.buttons().withName(‘Apps’)通过iOS UI Automation定位
nspClick Element nsp=name==“login”通过iOSNsPredicate定位Check PR: #196
chainClick Element chain=XCUIElementTypeWindow[1]/*通过iOS Class Chain定位
cssClick Element css=.green_button通过css定位webview元素
nameClick Element name=my_element通过name属性定位仅支持Selendroid引擎

使用页面元素

从AppiumLibrary的1.4版开始,除定位符locator外,还可以传递一个包含页面元素WebElement对象。可以使用Get WebElementsGet WebElement关键字,来获取面元素WebElement对象。

例如:

@{elements}  Get Webelements  class=UIAButton
Click Element  @{elements}[2]

导入

参数说明
timeout=5, run_on_failure=Capture Page ScreenshotAppiumLibrary导入时可以包含一些可选参数。
timeout 是所有等待操作的默认等待时间. 也可以后面使用关键字 Set Appium Timeout进行设置。
run_on_failure 用于指定AppiumLibrary关键字执行失败时要执行的关键字 (可以来这任何可用的库),
默认使用Capture Page Screenshot对当前页面截图。
使用No Operation 将完全禁用此功能。
该功能的更对信息可以参考关键字Register Keyword To Run On Failure
示例:
# 设置默认超时时间为10秒,出错后不做任何操作
Library | AppiumLibrary | timeout=10 | run_on_failure=No Operation

捷径

Background App · Capture Page Screenshot · Clear Text · Click A Point · Click Button · Click Element · Click Element At Coordinates · Click Text · Close All Applications · Close Application · Element Attribute Should Match · Element Name Should Be · Element Should Be Disabled · Element Should Be Enabled · Element Should Be Visible · Element Should Contain Text · Element Should Not Contain Text · Element Text Should Be · Element Value Should Be · Execute Adb Shell · Execute Async Script · Execute Script · Get Activity · Get Appium SessionId · Get Appium Timeout · Get Capability · Get Contexts · Get Current Context · Get Element Attribute · Get Element Location · Get Element Size · Get Matching Xpath Count · Get Network Connection Status · Get Source · Get Text · Get Webelement · Get Webelements · Get Window Height · Get Window Width · Go Back · Go To Url · Hide Keyboard · Input Password · Input Text · Input Value · Install App · Is Keyboard Shown · Landscape · Launch Application · Lock · Log Source · Long Press · Long Press Keycode · Open Application · Page Should Contain Element · Page Should Contain Text · Page Should Not Contain Element · Page Should Not Contain Text · Pinch · Portrait · Press Keycode · Pull File · Pull Folder · Push File · Quit Application · Register Keyword To Run On Failure · Remove Application · Reset Application · Scroll · Scroll Down · Scroll Up · Set Appium Timeout · Set Location · Set Network Connection Status · Shake · Start Activity · Start Screen Recording · Stop Screen Recording · Swipe · Swipe By Percent · Switch Application · Switch To Context · Tap · Text Should Be Visible · Toggle Touch Id Enrollment · Touch Id · Wait Activity · Wait Until Element Is Visible · Wait Until Page Contains · Wait Until Page Contains Element · Wait Until Page Does Not Contain · Wait Until Page Does Not Contain Element · Xpath Should Match X Times · Zoom

关键字

关键字参数说明
Background Appseconds=5将应用置于设备后台一段时间。
Capture Page Screenshotfilename=None对当前页面截图并嵌入日志。
filename 参数用于指定写入截图的文件名称。
如果没有指定filename, 截图将以appium-screenshot-.png名称保存在日志文件所在目录中。
如果filename没有使用绝对路径,则也被视为使用同一目录。
css 可用于修改截图样式。
默认情况下,会自动更改背景色以避免页面布局被破坏时背景泄漏等可能问题。
Clear Textlocator清空定位符locator所定位到的元素。
元素定位详情,可以参考介绍
Click A Pointx=0, y=0, duration=100点击指定坐标
Click Buttonindex_or_name点击按钮
Click Elementlocator点击定位符locator指定元素。
任意元素的关键属性为indexname
元素定位详情,可以参考介绍
Click Element At Coordinatescoordinate_X, coordinate_Y点击元素指定坐标
Click Texttext, exact_match=False单击以text标识的文本。
默认情况下,单击定位到的第一个文本。
如果想单击完全匹配的文本,则将exact_match设置为True
如果不想要第一个,可以使用locatorGet Web Elements结合使用。
Close All Applications关闭所有应用。
该关键字用于用例及测试套件teardown测试清理过程,以确保测试结束前所有应用都被关闭。
执行该关键字后,由Open Application返回的应用所有被重置为1.
Close Application关闭应用及WebDriver会话
Element Attribute Should Matchlocator, attr_name, match_pattern, regexp=False验证定位符定位到的元素的指定属性,匹配指定规则。
元素定位详情,可以参考介绍 。如果匹配到多个元素,则选择第一个元素。
attr_name是所选元素内的属性的名称。
match_pattern用于匹配,如果match_pattern为如下值:
布尔值或’True’/‘true’,则将 ‘False’/’false’字符串,然后应用布尔匹配
任何其他字符串都会导致字符串匹配。
regexp定义是否使用正则表达式(如BuiltIn库的Should Match Regexp)或字符串匹配(如BuiltIn库的Should Match
示例:
Element Attribute Should Match| xpath=//*[contains(@text,'foo')] | text | *foobar
Element Attribute Should Match | xpath=//*[contains(@text,'foo')] | text | f.*ar | regexp=True
Element Attribute Should Match | xpath=//*[contains(@text,'foo')] | enabled | True
1. 字符串匹配,如匹配字符串以foobar结尾
2. 常规匹配,如text属性应匹配正则表达式f.\*ar
3. 布尔类型匹配,如enabled属性应为出True
注意:
在Android上,支持的属性名称在AndroidElement类的getBoolAttribute()getStringAttribute()方法中进行了硬编码。
可用属性如下
contentDescription, text, className, resourceId
checkable, clickable, focusable, longClickable, scrollable
enabled, focused, checked, selected, displayed
注意: 某些属性使用两种方式计算都可以,例如一下计算得到相同的结果。
Element Attribute Should Match | xpath=//*[contains(@text,'example text')] | name | txt_field_name
Element Name Should Be | xpath=//*[contains(@text,'example text')] | txt_field_name
Element Name Should Belocator, expected验证定位符locator指定元素name属性为expected文本。
Element Should Be Disabledlocator, loglevel=INFO验证定位符locator指定元素不可用。任意元素的关键属性为idname
元素定位详情,可以参考介绍
Element Should Be Enabledlocator, loglevel=INFO验证定位符locator指定元素可用。任意元素的关键属性为idname
元素定位详情,可以参考介绍
Element Should Be Visiblelocator, loglevel=INFO验证定位符locator指定元素可见。
任意元素的关键属性为idname
元素定位详情,可以参考介绍
AppiumLibrary 1.4.5新功能
Element Should Contain Textlocator, expected, message=验证定位符locator指定元素text属性中包含expected文本。如果想验证元素文本完全等于指定文本,可以使用Element Text Should Be.
任意元素的关键属性为idxpath
message用于覆盖默认的出错信息。
AppiumLibrary 1.4新功能
Element Should Not Contain Textlocator, expected, message=验证定位符locator指定元素text属性中不包含expected文本。
message用于覆盖默认的出错信息。 S详情参考Element Should Contain Text
Element Text Should Belocator, expected, message=验证locator指定元素text属性中应为expected指定文本。相反关键字为Element Should Contain Text, 本关键字不支持包含,需要需要完全等于。
message用于覆盖默认的出错信息。
AppiumLibrary 1.4新功能
Element Value Should Belocator, expected元素value属性应为expected指定文本
Execute Adb Shellcommand, **args*执行Adb Shell命令,仅支持Android。
command - Adb Shell命令
args - 命令参数
返回Adb Shell命令退出码exit code。
需要在Appium服务器上设置服务器标志--relaxed-security
Execute Async Scriptscript(仅限Web页面)
在当前框架中注入异步JavaScript脚本。
函数最后一个参数为指定的回调函数,异步执行的脚本执行后调用回调函数,并将回调函数的返回值返回给客户端。
AppiumLibrary 1.5新功能
Execute Scriptscript(仅限Web页面)
在当前框架中注入通过JavaScript脚本。
执脚本执行后将返回值返回给客户端。
AppiumLibrary 1.5新功能
Get Activity获取设备当前Activity页面,仅支持Android。
Get Appium SessionId返回当前会话ID作为参考
Get Appium Timeout返回当前设置的Appium超时时间,单位秒。相关解释可参考[Set Appium Timeout
Get Capabilitycapability_name返回desired capability指定配置项的值
Get Contexts获取当前所有可用的上下文
Get Current Context获取当前上下文。
Get Element Attributelocator, attribute获取元素属性,支持的属性为: name, value。
示例:
Get Element Attribute | locator | name
Get Element Attribute | locator | value
Get Element Locationlocator获取locationKey属性 for arbitrary elements are id and name.
元素定位详情,可以参考介绍
Get Element Sizelocator获取元素尺寸。
任意元素的关键属性为idname..
元素定位详情,可以参考介绍
Get Matching Xpath Countxpath返回Xpath匹配到的元素个数,Xpath表达式不可以使用xpath=前缀。
正确示例:
${count} | Get Matching Xpath Count | //android.view.View[@text='Test']
错误示例:
${count} | Get Matching Xpath Count | xpath=//android.view.View[@text='Test']
如果要断言匹配到的元素个数,可以使用Xpath Should Match X Times
AppiumLibrary 1.4新功能
Get Network Connection Status返回设备网络状态整数位掩码,仅支持Android。
详情参看Set network connection status
Get Source获取当前页面的整个源码
Get Textlocator获取元素文本(在混合App和移动浏览器中使用xpath定位符, 其他可能会有问题)
示例:
${text} | Get Text | //*[contains(@text,'foo')]
AppiumLibrary 1.4新功能
Get Webelementlocator返回 locator定位到的第一个WebElement对象。
示例:
${element} Get Webelement id=my_element Click Element ${element}AppiumLibrary 1.4新功能
Get Webelementslocator返回locator定位到的 WebElement对象列表。
示例:
@{elements} | Get Webelements | id=my_element
Click Element | @{elements}[2]
该关键字在AppiumLibrary 1.4修改为:
名称由原来的Get Elements 改为当前名称。
废弃参数fail_on_error, 必要时可以使用Run Keyword and Ignore Error
AppiumLibrary 1.4新功能
Get Window Height获取当前设备屏幕高度。
示例:
${width} | Get Window Width
${height} | Get Window Height
Click A Point | ${width} | ${height}
AppiumLibrary 1.4.5新功能
Get Window Width获取当前设备屏幕宽度。
示例:
${width} | Get Window Width
${height} | Get Window Height
Click A Point | ${width} | ${height}
AppiumLibrary 1.4.5新功能
Go Back在浏览器中返回上一个页面
Go To Urlurl在默认浏览器上打开URL
示例:
Open Application | http://localhost:4755/wd/hub | platformName=iOS | platformVersion=7.0 | deviceName='iPhone Simulator' | browserName=Safari
Go To URL | http://m.webapp.com
Hide Keyboardkey_name=None在设备上隐藏键盘。
可选参数
在iOS,上,使用 key_name 按下指定键, 如. Done
在Android上, 无须参数
Input Passwordlocator, text在通过locator定位到的元素中输入指定密码
Input Text不同的是,密码不会记录在日志中
元素定位详情,可以参考介绍
Input Textlocator, text在定位符指定元素输入框中输入指定text文本。
元素定位详情,可以参考介绍
Input Valuelocator, text仅限iOS设备,使用set_value在通过locator定位到的元素中输入指定文本
元素定位详情,可以参考介绍
Install Appapp_path, app_package通过Appium安装App,仅支持Android。
app_path - Apk文件路径
app_package - 待验证app包名
Is Keyboard Shown如果Android键盘显示则返回True,否则返回False,无须参数。
Landscape设置屏幕方向为横屏模式
Launch Application加载应用,可以在Appium会话中加载应用。
该关键字可用于在用例中或用例间启动应用
在使用Open Application打开应用运行测试时,推荐在用例之间,使用Launch ApplicationQuit Application打开和关闭应用。
可以在测试套件的Test Setup中使用Open Application来启动应用。
示例:
以下为伪代码,Setup/Teardown使用可参考RF Guide:
[Setup Suite] | Open Application | http://localhost:4723/wd/hub | platformName=Android | deviceName=192.168.56.101:5555 | app=${CURDIR}/demoapp/OrangeDemoApp.apk
[Test Setup] | Launch Application
<<<test execution>>><<<test execution>>>
[Test Teardown] | Quit Application
[Suite Teardown] | Close Application
参考 Quit Application for quiting application but keeping Appium sesion running。
AppiumLibrary 1.4新功能
Lockseconds=5锁定屏幕一定时间,仅支持iOS。
Log Sourceloglevel=INFO记录日志并返回当前页面的HTML源码
loglevel参数用于定义用例等级
可用日志等级为 WARN, INFO (默认), DEBUG, TRACE and NONE (不输出日志)
Long Presslocator, duration=1000长按元素指定时间
Long Press Keycodekeycode, metastate=None在设备上长按一个键
详情参考press keycode
Open Applicationremote_url, alias=None, **kwargs在指定Appium Server上打开一个新的应用。
Andorid及iOS相关Appium Server预期功能配置请参考Appium server-args
remote_url Appium server url
alias 别名
示例:
Open Application | http://localhost:4723/wd/hubalias=Myapp1 | platformName=iOS | platformVersion=7.0 | deviceName='iPhone Simulator' | app=your.app
Open Application | http://localhost:4723/wd/hub | platformName=Android | platformVersion=4.2.2 | deviceName=192.168.56.101:5555 | app=${CURDIR}/demoapp/OrangeDemoApp
... | apkappPackage=com.netease.qa.orangedemoapp | Activity=MainActivity
Page Should Contain Elementlocator, loglevel=INFO验证当前页面包含定位符 locator指定元素,如果验证失败,则自动在日志中记录页面源码。
可以使用loglevel参数指定输出日志等级,设置为NONE关闭输出页面源码。
Page Should Contain Texttext, loglevel=INFO验证当前页面包含text指定文本,如果验证失败,则自动在日志中记录页面源码。
可以使用loglevel参数指定输出日志等级,设置为NONE关闭输出页面源码。
Page Should Not Contain Elementlocator, loglevel=INFO验证当前页面不包含locator指定元素, 如果验证失败,则自动在日志中记录页面源码。
可以使用loglevel参数指定输出日志等级,设置为NONE关闭输出页面源码。
Page Should Not Contain Texttext, loglevel=INFO验证当前页面不包含text指定文本,如果验证失败,则自动在日志中记录页面源码。
可以使用loglevel参数指定输出日志等级,设置为NONE关闭输出页面源码。
Pinchlocator, percent=200%, steps=1在指定元素出捏合指定倍速
Portrait将设备方向设置为竖向
Press Keycodekeycode, metastate=None向设备发送一个按键指令,仅支持Android。
可用按键编码keycodes及元状态metastate可参考: Android官方KeyEvent
状态state,描述诸如Shift、Ctrl和Alt键之类的键修饰符的按下状态。
Meta State是一个整数,其中设置为1的每个位代表一个按下的meta键,
例如:META_SHIFT_ON = 1META_ALT_ON = 2metastate = 1->按下Shift<br/>metastate = 2->按下Alt按下<br/>metastate = 3-> 按下Shift + Alt组合键-_keycode--要发送到device_metastate的键码--元键的状态| | <span id="pull-file">Pull File</span>| *path*, *decode=False* | 获取*path*指定文件的并返回其内容,仅支持Android。<br/>*path* - 设备上的文件路径,<br/>*decode* - True/False,返回前是转为base64编码(默认为False) | | <span id="pull-folder">Pull Folder</span>| *path*, *decode=False* | 获取*path*指定目录,返回文件夹的zip压缩包,仅支持Android。<br/>*path* - 设备上文件夹路径<br/>*decode* - True/False 返回钱是否编码为base64,默认为False | | <span id="push-file">Push File</span>| *path*, *data*, *encode=False* | 上传出局到*path*指定文件,仅支持Android。<br/>*path* - 设备的文件路径<br/>*data* - 需要写人文件的数据<br/>*encode* - True/False 返回前是转为base64编码(默认为False) | | <span id="quit-application">Quit Application</span>| | 退出应用但不关闭Appium会话。该关键字可以用于在用例中或用例间关闭应用。详细解释可参考[Launch Application](#launch-application)。<br/>AppiumLibrary 1.4新功能 | | <span id="register-keyword-to-run-on-failure">Register Keyword To Run On Failure</span>| *keyword* |设置AppiumLibrary关键字执行时要执行的关键字<br/>*keyword_name* 指定AppiumLibary关键字执行失败是要执行的关键字(可以来自任意库) <br/>不支持带参数的关键字<br/>使用关键字"Nothing"禁用此功能<br/>初始化关键字在*importing*中,默认使用关键字[Capture Page Screenshot](#capture-page-screenshot)<br/>失败时截图非常有用但是注意会拖慢运行速度<br/>该关键字返回之前注册的关键字名称,将来可以用于存储原始值<br/>示例:<br/># 失败时运行[Log Source](#log-source)<br/>Register Keyword To Run On Failure
Zoomlocator, percent=200%, steps=1在元素出放大指定倍数

共94个关键字。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

临渊-测试开发

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值