1.滑动
from appium import webdriver
from time import sleep
from appium.webdriver.common.touch_action import TouchAction
caps = {}
caps["platformName"] = "Android"
caps["platformVersion"] = "5.1.1"
caps["deviceName"] = "127.0.0.1:62001"
caps["appPackage"] = "com.tal.kaoyan"
caps["appActivity"] = '.ui.activity.SplashActivity'
# caps['app']=r'D:\1乐博\maye\自动化05期\Appium\自动化05期-004-移动端特有API详细讲解\kaoyan3.1.0.apk'
driver=webdriver.Remote('http://127.0.0.1:4723/wd/hub',caps)
driver.implicitly_wait(5)
# 点击取消
driver.find_element_by_id('android:id/button2').click()
# print(driver.get_window_size())
def get_size():
x=driver.get_window_size()['width']
y=driver.get_window_size()['height']
return x,y
def swipeLeft():
size = get_size()
x1=int(size[0]*0.9)
y = int(size[1] * 0.5)
x2 = int(size[0] * 0.2)
driver.swipe(x1,y,x2,y,1000)
def swipeRight():
size = get_size()
x1=int(size[0]*0.2)
y = int(size[1] * 0.5)
x2 = int(size[0] * 0.9)
driver.swipe(x1,y,x2,y,1000)
def swipeUP():
size = get_size()
x=int(size[0]*0.5)
y1 = int(size[1] * 0.9)
y2 = int(size[1] * 0.3)
driver.swipe(x,y1,x,y2,1000)
def swipeDown():
size = get_size()
x=int(size[0]*0.5)
y1 = int(size[1] * 0.3)
y2 = int(size[1] * 0.9)
driver.swipe(x,y1,x,y2,1000)
# for i in range(2):
# swipeLeft()
# sleep(0.5)
while True:
try:
ele=driver.find_element_by_id('com.tal.kaoyan:id/activity_splash_guidfinish')
except:
swipeLeft()
else:
ele.click()
break
2.手势
滑动操作一般是两点之间的滑动,而实际使用过程中用户可能要进行一些多点连续滑动操作。
Touch Action包含一些列操作,比如按压、长按、点击、移动、暂停。由这些不同操作可以组成一套动作。
使用TochAction需要先导入对应的模块
from appium.webdriver.common.touch_action import TouchAction
from appium.webdriver.common.touch_action import TouchAction
from appium import webdriver
import time
# server 启动参数
desired_caps = {
'platformName': 'Android',
'platformVersion': '5.1.1',
'deviceName': '127.0.0.1:62001',
'appPackage': 'com.android.settings',
'appActivity': '.Settings'
}
# 声明driver对象
driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
driver.implicitly_wait(5)
def get_size():
x = driver.get_window_size()['width']
y = driver.get_window_size()['height']
return x, y
def swipeUP():
size = get_size()
x = int(size[0] * 0.5)
y1 = int(size[1] * 0.9)
y2 = int(size[1] * 0.3)
driver.swipe(x, y1, x, y2, 1000)
# 向上滑屏找到‘安全’
swipeUP()
# 点击安全
driver.find_element_by_xpath('//android.widget.TextView[@text=\"安全\"]').click()
# 点击滑动
ele = driver.find_element_by_id('android:id/summary')
TouchAction(driver).tap(element=ele).perform()
# 点击图案
driver.find_element_by_xpath('//android.widget.TextView[@text=\"图案\"]').click()
time.sleep(1)
TouchAction(driver).press(x=148, y=527).wait(1000) \
.move_to(x=471, y=527).wait(1000) \
.move_to(x=747, y=527).wait(1000) \
.move_to(x=768, y=835).wait(1000).release().perform()
3.多点触控
from appium import webdriver
from time import sleep
from appium.webdriver.common.multi_action import MultiAction
from appium.webdriver.common.touch_action import TouchAction
caps = {}
caps["platformName"] = "Android"
caps["platformVersion"] = "5.1.1"
caps["deviceName"] = "127.0.0.1:62001"
caps["appPackage"] = "com.baidu.BaiduMap"
caps["appActivity"] = "com.baidu.baidumaps.MapsActivity"
caps["notReset"]=True
driver=webdriver.Remote('http://127.0.0.1:4723/wd/hub',caps)
driver.implicitly_wait(10)
x=driver.get_window_size()['width']
y=driver.get_window_size()['height']
def pinch():
action1=TouchAction(driver)
action2 = TouchAction(driver)
pinch_achtion=MultiAction(driver)
action1.press(x=x*0.2,y=y*0.2).wait(1000).move_to(x=x*0.4,y=y*0.4).release()
action2.press(x=x * 0.8, y=y * 0.8).wait(1000).move_to(x=x * 0.6, y=y * 0.6).release()
pinch_achtion.add(action1,action2)
print('开始缩小')
pinch_achtion.perform()
def zoom():
action1=TouchAction(driver)
action2 = TouchAction(driver)
zoom_achtion=MultiAction(driver)
action1.press(x=x*0.4,y=y*0.4).wait(1000).move_to(x=x*0.2,y=y*0.2).release()
action2.press(x=x * 0.6, y=y * 0.6).wait(1000).move_to(x=x * 0.8, y=y * 0.8).release()
zoom_achtion.add(action1,action2)
print('开始放大')
zoom_achtion.perform()
if __name__=="__main__":
sleep(20)
for i in range(2):
pinch()
sleep(1)
for i in range(2):
zoom()
sleep(1)
4. 锁屏解锁
from appium import webdriver
from appium.webdriver.common.touch_action import TouchAction
import time
caps = {}
caps["platformName"] = "Android"
caps["platformVersion"] = "5.1.1"
caps["deviceName"] = "127.0.0.1:62001"
caps["appPackage"] = "com.android.settings"
caps["appActivity"] = ".Settings"
driver=webdriver.Remote('http://127.0.0.1:4723/wd/hub',caps)
print(driver.is_locked())
def get_size():
x=driver.get_window_size()['width']
y=driver.get_window_size()['height']
return x,y
def swipeUP():
size = get_size()
x=int(size[0]*0.5)
y1 = int(size[1] * 0.9)
y2 = int(size[1] * 0.3)
driver.swipe(x,y1,x,y2,1000)
#输入文字的密码,进行解锁
# swipeUP()
# 输入密码
# driver.find_element_by_id('com.android.systemui:id/passwordEntry').send_keys('lebo')
# driver.keyevent(66)
# 图案密码
#九宫格密码进行解锁
# TouchAction(driver).press(x=199,y=803)\
# .move_to(x=359,y=801).wait(1000)\
# .move_to(x=522,y=965).wait(1000)\
# .move_to(x=518,y=1122).wait(1000).release().perform()
# 当前是否锁屏
# print( driver.is_locked())
driver.lock() #锁屏 参数数字,表示锁屏几秒
driver.unlock()
# print( driver.is_locked())
# time.sleep(2)
# print( driver.is_locked())