最近在做一些安卓自动化的项目,新手踩了一些坑,总结一下。
现在用过的常用库包括TouchAction、TouchActions、MultiAction。总结一下api和使用。
TouchAction
TouchAction是appium的辅助类,主要是一些手势操作。
from appium.webdriver.common.touch_action import TouchAction
- tap:对元素执行点击操作
跳进函数看一下源码
def tap(self, element=None, x=None, y=None, count=1):
"""Perform a tap action on the element
Args:
element (`appium.webdriver.webelement.WebElement`): the element to tap
x (:obj:`int`, optional): x coordinate to tap, relative to the top left corner of the element.
y (:obj:`int`, optional): y coordinate. If y is used, x must also be set, and vice versa
Returns:
`TouchAction`: self instance
"""
opts = self._get_opts(element, x, y)
opts['count'] = count
self._add_action('tap', opts)
return self
这里可以看出来参数应该如何传,注意第一个参数传的是webElement,它将‘tap‘append到action队列中,在执行perform时,发指定指令执行。
使用
def Tap(self, data):
_driver = self._cache.current
ele = self._element_find(data[0], True, True)
TouchAction(_driver).tap(ele).perform()
还有一些其他的接口:
看源码:
- press
def press(self, el=None, x=None, y=None, pressure=None):
"""Begin a chain with a press down action at a particular element or point
Args:
el (:obj:`appium.webdriver.webelement.WebElement`, optional): the element to press
x (:obj:`int`, optional): x coordiate to press. If y is used, x must also be set
y (:obj:`int`, optional): y coordiate to press. If x is used, y must also be set
pressure (:obj:`float`, optional): [iOS Only] press as force touch. Read the description of `force` property on Apple's UITouch class
(https://developer.apple.com/documentation/uikit/uitouch?language=objc) for more details on possible value ranges.
Returns:
`TouchAction`: self instance
"""
self._add_action('press', self._get_opts(el, x, y, pressure=pressure))
return self
- long_press
多了一个时间参数
def long_press(self, el=None, x=None, y=None, duration=1000):
"""Begin a chain with a press down that lasts `duration` milliseconds
Args:
el (:obj:`appium.webdriver.webelement.WebElement`, optional): the element to press
x (:obj:`int`, optional): x coordiate to press. If y is used, x must also be set
y (:obj:`int`, optional): y coordiate to press. If x is used, y must also be set
duration (:obj:`int`, optional): Duration to press
Returns:
`TouchAction`: self instance
"""
self._add_action('longPress', self._get_opts(el, x, y, duration))
return self
- move_to
def move_to(self, el=None, x=None, y=None):
"""Move the pointer from the previous point to the element or point