省的以后搜索直接就能用了,开心
谢谢上面两位大佬
Text属性的方法
driver.find_element_by_android_uiautomator('new UiSelector().text("Custom View")').click() #text
driver.find_element_by_android_uiautomator('new UiSelector().textContains("View")').click() #查找某个字符串包含View
driver.find_element_by_android_uiautomator('new UiSelector().textStartsWith("Custom")').click() #textStartsWith 查找某个串以Custom打头
driver.find_element_by_android_uiautomator('new UiSelector().textMatches("^Custom.*")').click() #textMatches
class属性的方法
driver.find_element_by_android_uiautomator('new UiSelector().className("android.widget.TextView").text("Custom View")').click() #className
driver.find_element_by_android_uiautomator('new UiSelector().classNameMatches(".*TextView$").text("Custom View")').click() #classNameMatches
伪xpath方法定位
这个方法还是比较有用,用来查找同级节点关系以及父节点关系
driver.find_element_by_android_uiautomator('new UiSelector().text("Custom View").fromParent(new UiSelector().text("Accessibility Service"))').click() #通过同级元素定位同级元素
driver.find_element_by_android_uiautomator('new UiSelector().className("android.widget.ListView").childSelector(new UiSelector().text("Custom View"))').click() #通过父级元素定位子集元素
resourceId属性的方法
driver.find_element_by_android_uiautomator('new UiSelector().resourceId("android:id/text1")') #resourceId
driver.find_element_by_android_uiautomator('new UiSelector().resourceIdMatches(".*id/text1$")') #resourceIdMatches
description方法
driver.find_element_by_android_uiautomator('new UiSelector().description("Custom View")').click() #description
driver.find_element_by_android_uiautomator('new UiSelector().descriptionStartsWith("Custom")').click() #descriptionStartsWith
driver.find_element_by_android_uiautomator('new UiSelector().descriptionMatches("^Custom.*")').click() #descriptionMatches
元素的其他属性
除了以上比较常用的方法外,UIAutomator还支持其他一些方法,比如根据控件属性是否可点击可聚焦可长按等来缩小要定位的控件的范围,具体使用方法不一一列举(checked,clickable,focesed…)
driver.find_element_by_android_uiautomator(``'new UiSelector().clickable(true).text("Custom View")'``).click()
获取控件属性值:
father=self.driver.find_element_by_android_uiautomator('new UiSelector().resourceId("com.pakeage:id/novel_name")')
**print** father.get_attribute("text")</pre>
登入操作:
Texts = self.driver.find_elements_by_xpath("//android.widget.EditText")
username = Texts[0]
username.send_keys("lius")
pwd = Texts[1]
pwd.send_keys("83698")
self.driver.find_element_by_name("登录").click()</pre>
向上滑动:
self.driver.swipe(width / 2, heigh / 2, width / 2, heigh / 4)</pre>
向下滑动
self.driver.swipe(width / 2, heigh / 4, width / 2, heigh / 2)</pre>
点击:
self.driver.tap([(width*0.74,heigh*0.778)])</pre>
方法先写这么多,不懂的可以留言。看到了会及时回复
使用appium自带键盘,很好的处理文本框输入问题和中文问题
desired_caps["unicodeKeyboard"] = "True" desired_caps["resetKeyboard"] = "True"
new command 最大时间间隔
desired_caps["newCommandTimeout"] =240
获取信息类API
(1)获取默认系统语言对应的Strings.xml文件内的数据。
get_app_string()
(2)查找某一个语言环境对应的字符串文件Strings.xml内数据。
get_app_string(String language)
(3)获取当前activity,比如(.ApiDemos)
current_activity()
App安装与卸载类API
1)根据bundleId来判断该应用是否已经安装
is_app_installed(String bundleId)
(2)安装app,appPath为应用的本地路径
install_app(String appPath)
(3)卸载app.bundleId在android中代表的是包名,而在ios中有专门的bundleId号。
remove_app(String bundleId)
(4)关闭应用,其实就是按home键把应用置于后台
close_app()
(5)启动应用
launch_app()
(6)先closeApp然后在launchAPP
reset_app()
文件操作类API
1)将字符数组用64位格式写到远程目录的某个文件中。也可以理解为把本地文件push到设备上。
push_file(String remotePath, byte[] base64Data)
(2)将设备上的文件pull到本地硬盘上
pull_file(String remotePath)
(3)将设备上的文件夹pull到本地硬盘上,一般远程文件为/data/local/tmp下的文件。
pull_folder(String remotePath)
设置相关类API
```go
1)设置手机的网络连接状态,可以开关蓝牙、wifi、数据流量。通过NetworkConnectionSetting中的属性来设置各个网络连接的状态。
set_network_connect(NetworkConnectionSetting connection)
(2)得到当前网络的状态
get_network_connection()
用户操作类API
(1)ios隐藏键盘
hide_keyboard()
(2)隐藏键盘,只能用于ios上。
hide_keyboard(String strategy, String keyName)
(3)按下某个键,具体哪个键由key值决定,key值定义在AndroidKeyCode类中
send_key_event(int key)
(4)执行一个touch动作,该touch动作是由TouchAction封装的。
perform_touchAction(TouchAction touchAction)
(5)点击element控件中心点按下,duration*5毫秒秒后松开,如此重复fingers次。
tap(int fingers, WebElement element, int duration)
(6)点击(x,y)点按下,duration*5毫秒后松开,如此重复fingers次。
tap(int fingers, int x, int y, int duration)
(7)从(startx,starty)滑到(endx,endy),分duration步滑,每一步用时是5毫秒。
swipe(int startx, int starty, int endx, int endy, int duration)
(8)2个手指操作控件,从对角线向中心点滑动。
pinch(WebElement el)
(9)以(x,y)为基准,计算得出(x,y-100),(x,y+100)两个点,然后2个手指按住这两个点同时滑到(x,y)
pinch(int x, int y)
(10)与pinch(el)的动作刚好相反。两个手指由控件的中心点慢慢向控件的左顶点后右底点滑动。
zoom(WebElement el)
(11)和pinch(x,y)相反。两个手指从(x,y)点开始向(x,y-100)和(x,y+100)滑动。
zoom(int x, int y)
(12)锁屏多少秒后解锁
lock_screen(int seconds)
(13)模拟摇晃手机
shake()
(14)滚动到某个text属性为指定的字符串的控件
scroll_to(String text)
(15)滚动方向由element1到element2
scroll(element1,element2)
(16)滚动到某个text属性包含传入的字符串的控件
scroll_to_exact(String text)
(17)设置上下文
context(String name)
(18)可用上下文
get_context_handles()
(19)当前上下文
get_context()
(20)设置屏幕横屏或者竖屏
rotate(ScreenOrientation orientation)
(21)获取当前屏幕的方向
get_orientation()
获取控件类API
seleniumdriver
find_element_by_id
find_elements_by_id
find_element_by_name
find_elements_by_name
find_element_by_link_text
find_elements_by_link_text
find_element_by_partial_link_text
find_elements_by_partial_link_text
find_element_by_tag_name
find_elements_by_tag_name
find_element_by_xpath
find_elements_by_xpath
find_element_by_class_name
find_elements_by_class_name
find_element_by_css_selector
find_elements_by_css_selector
appiumdriver
find_element_by_ios_uiautomation
find_elements_by_ios_uiautomation
find_element_by_android_uiautomator
find_elements_by_android_uiautomator
find_element_by_accessibility_id
find_elements_by_accessibility_id
AppiumDriver的辅助类
主要针对手势操作,比如滑动、长按、拖动等。TouchAction的原理是讲一系列的动作放在一个链条中,然后将该链条传递给服务器。服务器接受到该链条后,解析各个动作,逐个执行。
TouchAction()
(1)在控件上执行press操作
press(WebElement el)
(2)在坐标为(x,y)的点执行press操作
press(int x, int y)
(3)在控件el的左上角的x坐标偏移x单位,y左边偏移y单位的坐标上执行press操作。
press(WebElement el, int x, int y)
(4)释放操作,代表该系列动作的一个结束标志。
release()
(5)以el为目标,从另一个点移动到该目标上
move_to(WebElement el)
(6)以(x,y)点为目标,从另一个点移动到该目标上
move_to( int x, int y)
(7) 在控件的中心点上敲击一下
tap(WebElement el)
(8)在(x,y)点轻击一下
tap(int x, int y)
(9)以控件el的左上角为基准,x轴向右移动x单位,y轴向下移动y单位。在该点上轻击。
tap(WebElement el, int x, int y)
(10)代表一个空操作,等待一段时间
wait_action()
(11)等待ms秒
wait_action(int ms)
(12)控件长按
long_press(WebElement el)
(13)点长按
long_press(int x, int y)
(14)偏移点长按
long_press(WebElement el, int x, int y)
(15)取消执行该动作
cancel()
其他操作
from appium import webdriver
from appium.webdriver.extensions.android.nativekey import AndroidKey
desired_caps = {
"platformName":"Android", # 被测手机是安卓
"platformVersion": '8', # 手机安卓版本
"deviceName": 'xxx', # 设各名,安克手机可以随意填写
"appPackage": "tv.danmaku.bili", #启动APP Package 名称
"appActivity": ".ui.splash.SplashActivity", # 启动Activity名称
"unicodekeyboard": True, # 使用自带输入法,输入中文时填True
"resetkeyboard": True, # 执行完程序恢复原来输入法
"noReset": True, # 不要重蛋App
"newCommandTimeout":6000,
"automationName": 'UiAutomator2',
# "app": r'd:\apk\bili.apk
}
# 链接Appium Server, 初始化自动环境
driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
# 设罝缺省等待时间
driver.implicitly_wait(10)
# 根据i定位搜索位置框,点击
driver.find_element_by_id("expand_search").click()
# 根据定位搜索翰入旗,占击
sbox = driver.find_element_by_id('search_src_text')
# slox = driver.find_element_by_android_uiautomator('new UISelector().deseription("搜素查询")')
# slox = driver.find_element_by_android_uiautomator('new UiSelector().deseription("搜索查询").resourceId("tv.dapmaku.bilit:id/search_src_text")')
# slox = driver.find_element_by_android_uiautomator("new UiSelector().text('取消')")
# slox = driver.find_element_by_android_uiautomator('new UiSelector).className("android.widget.TextView").textContains("A537")')
sbox.send_keys("A")
# 输入回车鐘,确定搜索
driver.press_keycode (AndroidKey.ENTER)
#选择(定位)所有视频标题
eles = driver.find_elements_by_id("title")
# eles = driver.find_element_by_android_uiautomator('new UiSelector().resourceId("tv.danmaku.bili:id/recycler_view").childSelector(new UiSelector().className("android.widget.TextView"))')
for ele in eles:
# 打印标题
print(ele.text)
driver.find_element_by_xpath('//*@resource-id=tv.danmaku.bilitid/tabs"l//android.widget.FrameLayout[31]')
driver.quit()
Appium 界面查看工具 Ul Automator Viewer
定位元素
#### 定位元素规则
从示例代码,大家就可以发现,和Selenium Web自动化一样,要操作界面元素,必须先 定位(选择)元素。
Appium是基于Selenium的,所以 和Selenium 代码 定位元素的 基本规则相同:
- find_element_by_xxx 方法,返回符合条件的第一个元素,找不到拋出异常
- find_elements_by_xxx 方法,返回符合条件的所有元素的列表,找不到返回空列表
- 通过 WebDriver对象调用这样的方法,查找范国是整个界面
- 通过 WebElement 对象调用这样的方法,查找范围是该节点的子节点
定位元素的方法
根据ID
在Selenium Web自动化教程里,我们说过,如果能根据1D选择定位元素,最好根据ID,因为通常来说1D是唯一的,所以根据ID选择 效率高,
在安卓应用白动化的时候,同样可以根据ID查找。ID是安卓应用元素的resource-id属性
driver.find_element_by_id("expand_search" )
根据CLASS NAME
安卓界面元素的class属性 其实就是根据元素的类型,类似web里面的tagname, 所以通常不是唯一的
通常,我们根据class 属性来选择元素,是要选择多个而不是一个。当然如果你确定 要查找的 界面元素的类型 在当前界面中只有一个,就可以根据class 来唯一选择
driver.find_elements_by_class_name("android.widget.TextView")
根据ACCESSIBILITY ID
元素的 content-desc 属性是用来描述该元素的作用的。
如果要查询的界面元素有content-desc属性,我们可以通过它来定位选择元素。
driver.find_element_by_accessibility_id("找人")
Xpath
Appium 也支持通过 Xpath选择元素但是其可靠性和性能不如 Selenium Web自动化。因为Web自动化对xpath的支持是由浏览器
实现的,而Appium xpath的支持是 Appium Server实现的。毕竟,浏览器产品的成熟度比Appium要高很多。
当然,xpath是标准语法,所以这里表达式的语法规则和 以前学习的Selenium里面xpath的语法是一样的,
driver.find_element_by_xpath('//elel/ele2[@attr="value"]')
driver.find_element_by_xpath('//android.widget.TextView')
#### 界面元素杳看工且 (安卓 UIAutomator)
- 做 Selenium Web 自动化的时候,要找到元素,我们是通过浏览器的开发者工具栏来查看元素的特性,根据这些特性(属性和位置),来定位元素
- 根据id,classname, accessibilityid, xpath,这些方法选择元素,其实底层都是利用了安卓uiautomator框架的API功能实现的。
https://developer.android.google.cn/training/testing/ui-automator?hl=zh-cn (UiSelector)
程序的这些定位请求,被Appium server转发给手机自动化代理程序,就转化为为uiautomator里面相应的定位函数调用。
其实,我们的自动化程序,可以直接告诉 手机上的自动化代理程序,让它 调用UI Automator API的java代码,实现最为直接的自动化控制。
主要是通过 Uiselector 这个类里面的方法实现元素定位的,比如
code = 'new UiSelector().text("热门").className("android.widget.TextView")'
ele = driver.find_element_by_android_uiautomator(code)
ele.click()
Uiselector里面有些元素选择的方法 可以解决 前面解决不了的问题,
比如 textmartch 方法 可以使用正则表达式 选择一些元素
Uiselector 的childselector 可以选择后代元素,childSelector后面的引1号要框住整个子 uiselector 的表达式
目前有个bug:只能找到符合条件的第一个元素,参考appium 在github上的issues:比如https://github.com/appium/java-client/issues/150