appium 中,如何实现对控件长按后拖拽的动作?
求指教!
# convenience method added to Appium (NOT Selenium 3)
def drag_and_drop(self, origin_el, destination_el):
"""Drag the origin element to the destination element
:Args:
- originEl - the element to drag
- destinationEl - the element to drag to
"""
action = TouchAction(self)
action.long_press(origin_el).move_to(destination_el).release().perform()
return self
# convenience method added to Appium (NOT Selenium 3)
def tap(self, positions, duration=None):
"""Taps on an particular place with up to five fingers, holding for a
certain time
:Args:
- positions - an array of tuples representing the x/y coordinates of
the fingers to tap. Length can be up to five.
- duration - (optional) length of time to tap, in ms
:Usage:
driver.tap([(100, 20), (100, 60), (100, 100)], 500)
"""
if len(positions) == 1:
action = TouchAction(self)
x = positions[0][0]
y = positions[0][1]
if duration:
duration = duration
action.long_press(x=x, y=y, duration=duration).release()
else:
action.tap(x=x, y=y).release()
action.perform()
else:
ma = MultiAction(self)
for position in positions:
x = position[0]
y = position[1]
action = TouchAction(self)
if duration:
duration *= 1000 # we take seconds, but send milliseconds
action.long_press(x=x, y=y, duration=duration).release()
else:
action.press(x=x, y=y).release()
ma.add(action)
ma.perform()
return self
# convenience method added to Appium (NOT Selenium 3)
def swipe(self, start_x, start_y, end_x, end_y, duration=None):
"""Swipe from one point to another point, for an optional duration.
:Args:
- start_x - x-coordinate at which to start
- start_y - y-coordinate at which to end
- end_x - x-coordinate at which to stop
- end_y - y-coordinate at which to stop
- duration - (optional) time to take the swipe, in ms.
:Usage:
driver.swipe(100, 100, 100, 400)
"""
# `swipe` is something like press-wait-move_to-release, which the server
# will translate into the correct action
action = TouchAction(self)
action \
.press(x=start_x, y=start_y) \
.wait(ms=duration) \
.move_to(x=end_x, y=end_y) \
.release()
action.perform()
return self
#11 楼 @umbrella1978 我们欢迎猛插。。。@monkey
public void slide(WebElement origin_el, WebElement destination_el)
{
// appium converts longPress-moveto-release to a slide action
TouchAction touchAction = new TouchAction(this);
touchAction.longPress(origin_el).moveTo(destination_el).release().perform();
}
我使用上面的方法根据元素移动成功,但是根据坐标移动没有成功,方法如下
public void slide(int startX, int startY, int endX, int endY)
{
TouchAction touchAction = new TouchAction(this);
touchAction.longPress(startX, startY).moveTo(endX, endY).release().perform();
}
appium 报错日志如下:
info: --> POST /wd/hub/session/dfdfe492-8740-47c8-b799-c89e3fd652a2/touch/perfor
m {"actions":[{"action":"longPress","options":{"y":1365,"x":876}},{"action":"mov
eTo","options":{"y":609,"x":876}},{"action":"release","options":{}}]}
debug: Appium request initiated at /wd/hub/session/dfdfe492-8740-47c8-b799-c89e3
fd652a2/touch/perform
debug: Request received with params: {"actions":[{"action":"longPress","options"
:{"y":1365,"x":876}},{"action":"moveTo","options":{"y":609,"x":876}},{"action":"
release","options":{}}]}
debug: Pushing command to appium work queue: ["element:getLocation",{}]
error: Unhandled error: TypeError: Cannot read property 'x' of undefined
at null.<anonymous> (C:\Users\Administrator\AppData\Roaming\npm\node_modules
\appium\lib\devices\android\android-controller.js:886:27)
at next (C:\Users\Administrator\AppData\Roaming\npm\node_modules\appium\node
_modules\async\lib\async.js:801:43)
at C:\Users\Administrator\AppData\Roaming\npm\node_modules\appium\node_modul
es\async\lib\async.js:32:16
at exports.respond (C:\Users\Administrator\AppData\Roaming\npm\node_modules\
appium\lib\devices\common.js:28:9)
at null.<anonymous> (C:\Users\Administrator\AppData\Roaming\npm\node_modules
\appium\lib\devices\android\android.js:462:16)
at null.<anonymous> (C:\Users\Administrator\AppData\Roaming\npm\node_modules
\appium\lib\devices\android\uiautomator.js:93:9)
at Socket.EventEmitter.emit (events.js:95:17)
at Socket.<anonymous> (_stream_readable.js:746:14)
at Socket.EventEmitter.emit (events.js:92:17)
at emitReadable_ (_stream_readable.js:408:10) context: [POST /wd/hub/session
{"desiredCapabilities":{"platformVersion":"4.3","app":"D:\\autotest\\jsmcc_T_WA
P.apk","platformName":"Android","deviceName":"aidfm"}}]
debug: [BOOTSTRAP] [debug] Got data from client: {"cmd":"action","action":"eleme
nt:getLocation","params":{}}
debug: [BOOTSTRAP] [debug] Got command of type ACTION
debug: [BOOTSTRAP] [debug] Got command action: getLocation
debug: [BOOTSTRAP] [debug] Returning result: {"status":7}
有没有同学根据坐标拖动成功的,来帮忙瞧瞧啊
请问根据坐标拖动成功了没?