前言:
前幾天有人問我,手勢登陸如何做?于是我找了一個(gè)APP試了試,所以本文來總結(jié)使用Python+Appium來實(shí)現(xiàn)手勢密碼登陸APP器一。
環(huán)境:
MacOS:10.13.4
Appium-desktop:1.6.1
Xcode:9.3.1
APP:眾安保險(xiǎn)-iOS版
一、Appium API -- TouchAction
Appium的輔助類厨内,主要針對手勢操作祈秕,比如滑動(dòng)、長按雏胃、拖動(dòng)等请毛。
1、按壓控件
方法:press()
開始按壓一個(gè)元素或坐標(biāo)點(diǎn)(x,y)丑掺。通過手指按壓手機(jī)屏幕的某個(gè)位置获印。
舉例:
TouchAction(driver).press(x=0,y=308).release().perform()
release() 結(jié)束的行動(dòng)取消屏幕上的指針。
Perform() 執(zhí)行的操作發(fā)送到服務(wù)器的命令操作街州。
2兼丰、長按控件
方法:longPress()
開始按壓一個(gè)元素或坐標(biāo)點(diǎn)(x,y)。 相比press()方法唆缴,longPress()多了一個(gè)入?yún)Ⅶ⒄鳎热婚L按,得有按的時(shí)間吧面徽。duration以毫秒為單位艳丛。1000表示按一秒鐘。其用法與press()方法相同趟紊。
舉例:
TouchAction(driver).longPress(x=1 ,y=302,duration=1000).perform().release();
3氮双、移動(dòng)
方法:moveTo()
將指針(光標(biāo))從過去指向指定的元素或點(diǎn)。
舉例:
TouchAction(driver).moveTo(x=0,y=308).perform().release();
4霎匈、暫停
方法:wait()
暫停腳本的執(zhí)行戴差,單位為毫秒。
舉例:
TouchAction(driver).wait(1000);
二铛嘱、通過觸摸多點(diǎn)坐標(biāo)進(jìn)行解鎖
根據(jù)上面API解釋暖释,我們可以得出按壓和移動(dòng)來實(shí)現(xiàn)手勢解釋袭厂,大概思路如下:
TouchAction.press(beginX,beginY).moveTo(xStep,yStep).moveTo(xStep,yStep).release().perform();
打開Appium-Inspector來查看手勢對應(yīng)的各個(gè)點(diǎn)的坐標(biāo)。
代碼如下:
# -*- coding: utf-8 -*-
# @Time : 2018/5/22 下午10:33
# @Author : WangJuan
# @File : appium-ios.py
from time import sleep
from appium import webdriver
from appium.webdriver.common.touch_action import TouchAction
cap = {
"platformName": "iOS",
"platformVersion": "11.4",
"bundleId": "com.zhongan.insurance",
"automationName": "XCUITest",
"udid": "3e8325a7c0d*******************a7e",
"deviceName": "****Iphone"
}
host = "http://0.0.0.0:4728/wd/hub"
driver = webdriver.Remote(host, cap)
sleep(3)
action = TouchAction(driver)
action.press(x=98, y=321).wait(100).move_to(x=208, y=321).wait(100).move_to(x=206, y=432).wait(100).move_to(x=98, y=432).perform().release()
三乾忱、兼容不同分辨率
直接用坐標(biāo)點(diǎn)找會(huì)有一些問題讥珍,比如手機(jī)屏幕大小不同历极,找點(diǎn)的位置可能會(huì)有偏差窄瘟,如何解決呢?
由下圖可見趟卸,先獲取第一個(gè)觸摸點(diǎn)的坐標(biāo)location及size蹄葱。分別定義為start_height、start_width锄列、start_x图云、start_y
(其中start_x、start_y
為觸摸點(diǎn)左上角的坐標(biāo))邻邮;
即可計(jì)算出第一個(gè)觸摸點(diǎn)的中心點(diǎn)坐標(biāo)分別為:
start_x + start_width/2
, start_y + start_height/2
然后在計(jì)算出第二個(gè)觸摸點(diǎn)的中心點(diǎn)大致坐標(biāo)為:
start_x+start_width*2
, y=start_y+start_height*2
其他坐標(biāo)均可按照此計(jì)算方式竣况,詳情見具體例子。
# -*- coding: utf-8 -*-
# @Time : 2018/5/22 下午10:33
# @Author : WangJuan
# @File : appium-ios.py
from time import sleep
from appium import webdriver
from appium.webdriver.common.touch_action import TouchAction
cap = {
"platformName": "iOS",
"platformVersion": "11.4",
"bundleId": "com.zhongan.insurance",
"automationName": "XCUITest",
"udid": "3e8325a7c0***************62bd4a7e",
"deviceName": "My@Iphone"
}
host = "http://0.0.0.0:4728/wd/hub"
driver = webdriver.Remote(host, cap)
sleep(3)
action = TouchAction(driver)
# action.press(x=98, y=321).wait(100).move_to(x=208, y=321).wait(100).move_to(x=206, y=432).wait(100).move_to(x=98, y=432).perform().release()
start = driver.find_element_by_xpath('//XCUIElementTypeApplication[@name="眾安保險(xiǎn)"]/XCUIElementTypeWindow[1]/XCUIElementTypeOther\
/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther[1]')
start_height = start.size['height']
start_width = start.size['width']
start_x = start.location['x']
start_y = start.location['y']
begin_x = start_x + start_width/2
begin_y = start_y + start_height/2
action.press(x=start_x, y=start_y).wait(100).move_to(x=start_x+start_width*2, y=begin_y).wait(100).move_to\
(x=start_x+start_width*2, y=start_y+start_height*2).wait(100).move_to(x=begin_x,y=start_y+start_height*2).perform().release()
以上筒严,對你有幫助的話丹泉,請點(diǎn)贊吧??~~