Appium + Python - TouchAction 操作

Import the webdriver appium from 
from appium.webdriver.common.touch_action Import TouchAction
from appium.webdriver.common.multi_action Import MultiAction
from Time Import SLEEP

"" "
class TouchAction (Object):
DEF the __init __ (Self, Driver = None):
self._driver Driver =
self._actions = []

DEF TAP (Self, Element = None, X = None, Y = None, COUNT =. 1):
analog finger touch screen

def press (self, el = None , x = None, y = None) :
press: a simulated finger hold the element, or the coordinate

def long_press (self, el = None , x = None, y = None, duration = 1000):
long press: a simulated hold the element, or the coordinate

def wait (self , ms = 0):
the waiting time after the hold down element

def move_to (self, el = None , x = None, y = None):
Moving the finger to another element, or coordinates, absolute coordinates are not the coordinates noted that this is offset

def release (self):
release finger

def perform (self):
perform the foregoing operation

MultiAction is for multi-touch operation, it is a complementary module TouchAction of
the multi-touch touch action is a collection of objects.
Multi-touch gestures are only two ways that add and execution.
add another touch operation for adding to a multi-touch.
When perform execution is called, added to all the multi-touch touch action are sent to AppII, and perform as if they happened at the same time. appium first implementation of a touch event all the action, and then do the second, and so on.
"" "

# EL element is positioned objects

# action0 TouchAction = (). TAP (EL)
# = TouchAction action1 (). TAP (EL)
# MultiAction (). The Add (action0) .add (action1) .perform ()

= {desired_caps
"PlatformName": "the Android",
"platformVersion": "5.1.1",
"deviceName": "127.0.0.1:62001",
"
"appActivity":"com.android.settings/.Settings",
"noReset":"true",
"unicodeKeyboard":"true",
'resetKeyboard': 'true'
}

driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)

#点击搜索
driver.find_element_by_id("com.android.settings:id/search").click()
driver.find_element_by_id("android:id/search_src_text").send_keys("安全\n")
driver.find_elements_by_id("android:id/search_src_text")[0].click()

#屏幕安全保护
driver.find_element_by_id("android.widget.RelativeLayout").click()
driver.find_element_by_xpath("//*[@text = '图案']").the Click () # squared positioning element# problem-solving ideas: first get the element coordinate position, and then get the element size, then cut the picture, respectively, to calculate the coordinates of each point

SLEEP (5)


jiu = 'resourceId("com.android.settings:id/lockPattern").index(2)'
local = driver.find_element_by_android_uiautomator(jiu).location
print("获取九宫格坐标位置:%s"%local)

s = driver.find_element_by_android_uiautomator(jiu).size
print("获取九宫格宽和高:%s"%s)

#获取九个点的坐标
google = {}
google[1] = (None,local["x"]+s["width"]/6,local["y"]+s["height"]/6)
google[2] = (None,local["x"]+s["width"]/6*3,local["y"]+s["height"]/6)
google[3] = (None,local["x"]+s["width"]/6*5,local["y"]+s["height"]/6)
google[4] = (None,local["x"]+s["width"]/6,local["y"]+s["height"]/6*3)
google[5] = (None,local["x"]+s["width"]/6*3,local["y"]+s["height"]/6*3)
google[6] = (None,local["x"]+s["width"]/6*5,local["y"]+s["height"]/6*3)
Google [. 7] = (None, local [ "X"] + S [ "width"] /. 6, local [ "Y"] + S [ "height"] /. 6 *. 5)
Google [. 8] = (None, local [ "X"] + S [ "width"] /. 6 *. 3, local [ "Y"] + S [ "height"] /. 6 *. 5)
Google [. 9] = (None, local [ "X"] + S [ "width"] /. 6 *. 5, local [ "Y"] + S [ "height"] /. 6 *. 5)
Print (Google)

DEF pianyi (A =. 1, B = 2):
'' 'is calculated from point a to point b offset '' '
G1 = Google [a]
G2 = Google [b]
R & lt = (None, G2 [. 1] -G1 [. 1], G2 [2] -G1 [2])
r return

# unlock ideas: first press hold down the first point, and then wait wait, then moved with the second point, and then wait, and finally release the finger is released, perform execution
# execution unlock
TouchAction (driver) .press (* google [ . 1]). the wait (300) .move_to (pianyi * (1,2)). the wait (300) .move_to (pianyi * (2,3)). the wait (
300) .move_to (* pianyi (. 3,5)).wait(300).move_to(*pianyi(5,7)).wait(300).move_to(*pianyi(7,8)).wait(300).move_to(*pianyi(8,9)).wait(
300).release().perform()

Guess you like

Origin www.cnblogs.com/Teachertao/p/10990965.html