APP测试之触屏操作
TouchAction
提供的方法:
- press
- release
- moveTo
- tap
- wait
- longPress
- cancel
- perform
eg1: 按下滑动再释放:
- 在 “el0”点按下移动到”el1“再释放
TouchAction().press(el0).moveTo(el1).release()
eg2: MultiTouch 多点触摸
action0 = TouchAction().tap(el)
action1 = TouchAction().tap(el)
MultiAction().add(action0).add(action1).perform()
def test_touchaction(self):
action = TouchAction(self.driver) # 使用前先导入TouchAction
action.press(x=731, y=2083).wait(200).move_to(x=731, y=484).release().perform() # 中间的.wait(200)等待200ms防止滑动过快
实现通用触摸屏从下往上的滑动操作
.......
from appium.webdriver.common.touch_action import TouchAction
# 实现通用触摸屏从下往上的滑动操作
# 为将(要定位的)坐标点固定,可以先打印整个屏幕大小,进行比例换算
def test_touchaction(self):
action = TouchAction(self.driver) # 使用前先导入TouchAction
# print(self.driver.get_window_rect())
window_rect = self.driver.get_window_rect()
width = window_rec["width"]
height = window_rec["heght"]
x1 = int(width/2)
y_start = int(height * 4/5)
y_end = int(height * 1/5)
action.press(x=x1, y=y_start).wait(200).move_to(x=x1, y=y_end).release().perform()
手势操作
思路:点击 --> 滑动 --> 滑动 --> … --> 释放 --> perform()
from appium import webdriver
from appium.webdriver.common.touch_action import TouchAction
class TestTouchAction():
def setup(self):
desired_caps = {}
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '6.0'
desired_caps['deviceName'] = 'emulator-5554'
desired_caps['appPackage'] = 'cn.kmob.screenfingermovelock'
desired_caps['appActivity'] = 'com.samsung.ui.MainActitvity'
desired_caps['noReset'] = 'true'
desired_caps['skipDeviceInitialization'] = 'true'
desired_caps['unicodeKeyBoard'] = 'true'
desired_caps['resetKeyBoard'] = 'true'
self.driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", desired_caps)
self.driver.implicity_wait(5)
def teardown(self):
self.driver.quit()
def test_touchaction_unlock(self):
# pass
# self.driver.find_element_by_id("")
action = TouchAction(driver)
action.press(x=243, y=395)wait(200).move_to(x=721, y=378)wait(200).move_to(x=1190, y=366).wait(200)\
.move_to(x=1202, y=861).wait(200).move_to(x=1195, y=1339).release().perform()
其他:adb命令获取 手势解锁app的appPackage和appActivity
# 打开app后,进行查看ActivityManager
adb logcat | grep -i displayed