UIautomator、swipe滑动、press_keycod
2020-02-28 本文已影响0人
清水秋香
- UIautomator 选择元素方法
appium是通过一些底层框架来控制设备的,比如安卓设备是通过UIautomator
选择界面元素的方式,
根据id,classname,accessibilityid,xpath
这些方法其实底层都是利用了uiautomator的API功能实现的
如图:
image.png
手机上bootstrap.jar通过调用uiautomator中的api来完成自动化操作
关于uiautomator详细信息,可以访问如下网址:
https://developer.android.google.cn/training/testing/#UIAutomator - UiSelector
指定布局层次结构中要测试的元素,并按诸如文本值,内容描述,类名和状态信息之类的属性过滤。您还可以通过元素在布局层次结构中的位置来定位。
具体操作见文档
java 中string要用双引号
new UiSelector().resourceId("com.hpbr.bosszhipin:id/tv_position_name")
new UiSelector().className("android.widget.TextView")
new UiSelector().text("测试工程师").className("android.widget.TextView")
如果选择的为多个默认只显示第一个,可以用index来选择相应元素
new UiSelector().resourceId("com.hpbr.bosszhipin:id/ll_actions").childSelector(new UiSelector().className("android.widget.ImageView").index(2))
new UiSelector().textContains('工程师')
new UiSelector().textMatches('\d{2}.\d{2}')
可加分号进行匹配选择
new UiSelector().textMatches("\d{2}-\d{2}.*?");new UiSelector().resourceId("com.hpbr.bosszhipin:id/tv_position_name")
- 滑动屏幕
appium中提供的方法
def swipe(self, start_x, start_y, end_x, end_y, duration=None):
"""Swipe from one point to another point, for an optional duration.
Args:
start_x (int): x-coordinate at which to start
start_y (int): y-coordinate at which to start
end_x (int): x-coordinate at which to stop
end_y (int): y-coordinate at which to stop
duration (:obj:`int`, optional): time to take the swipe, in ms.
Usage:
driver.swipe(100, 100, 100, 400)
Returns:
`WebElement`
"""
# `swipe` is something like press-wait-move_to-release, which the server
# will translate into the correct action
action = TouchAction(self)
action \
.press(x=start_x, y=start_y) \
.wait(ms=duration) \
.move_to(x=end_x, y=end_y) \
.release()
action.perform()
return self
参数就是起点和终点x/y坐标
duration是滑动从起点到终点坐标所耗费的时间,单位是毫秒,
这个时间十分重要,慢速滑动和快速滑动效果是不同的,快速滑动会有惯性效果。
ele = driver.find_element_by_id('xxx')
location = ele.location #左上角坐标
size = ele.size #元素的高度和宽度
#计算起始点坐标
start_x = location['x'] + int(size['width'])*0.2
start_y = location['y'] + int(size['height'])*0.5
end_x = location['x'] + int(size['width'])*0.8
end_y = location['y'] + int(size['height'])*0.5
滑动屏幕打印信息
import time
from appium import webdriver
#准备自动化配置信息
from selenium.webdriver.common.by import By
desired_caps={
#移动设备平台
'platformName':'Android',
#平台OS版本号,写整数位即可
'plathformVersion':'8',
#设备的名称--值可以随便写
'deviceName':'test0106',
#提供被测app的信息-包名,入口信息
'appPackage':'com.hpbr.bosszhipin',
'appActivity':'.module.launcher.WelcomeActivity',
#确保自动化之后不重置app
'noReset':True,
#设置session的超时时间,单位秒
'newCommandTimeout':6000
}
#初始化driver对象-用于控制手机
driver=webdriver.Remote('http://localhost:4723/wd/hub',desired_caps)
driver.implicitly_wait(10)#稳定元素
# driver.find_element_by_android_uiautomator('new UiSelector().resourceId("com.hpbr.bosszhipin:id/tv_position_name")').click()
#滑动
start_x = 500
start_y = 1500
end_x = start_x
# y坐标减去滑动长度
end_y = start_y - 1000
#如果滑动的距离很长,时间很短会有惯性效果
time.sleep(7)
# 不指定不会出现惯性效果,如果指定可能会出现惯性效果
driver.swipe(start_x,start_y,end_x,end_y,500)
for i in range(10):
driver.swipe(start_x, start_y, end_x, end_y)
print(driver.find_element_by_android_uiautomator('new UiSelector().resourceId("com.hpbr.bosszhipin:id/tv_position_name")').text)
- press_keycode
按键事件
官网网址
使用方法为driver.press_keycode()括号中填入值,值可在官网进行获取
import time
from appium import webdriver
#准备自动化配置信息
from selenium.webdriver.common.by import By
desired_caps={
#移动设备平台
'platformName':'Android',
#平台OS版本号,写整数位即可
'plathformVersion':'8',
#设备的名称--值可以随便写
'deviceName':'test0106',
#提供被测app的信息-包名,入口信息
'appPackage':'com.hpbr.bosszhipin',
'appActivity':'.module.launcher.WelcomeActivity',
#确保自动化之后不重置app
'noReset':True,
#设置session的超时时间,单位秒
'newCommandTimeout':6000
}
#初始化driver对象-用于控制手机
driver=webdriver.Remote('http://localhost:4723/wd/hub',desired_caps)
driver.implicitly_wait(10)#稳定元素
time.sleep(3)
#打开通知
driver.open_notifications()
time.sleep(2)
#后退键,填入的是值,值在文档中
driver.press_keycode(4)
time.sleep(3)
#press_keycode可模拟键盘按键操作
- 通知栏操作方式也是页面元素,可以dump下来,查看元素、定位元素