Appium [已解决] 请问如何 实现长按的 持续时间?( long_press duration python)

Vincent · August 29, 2014 · Last by Tester Xxinxin replied at June 05, 2017 · 2839 hits

谢谢大家,不知道这是不是Appium 的bug,在它的API 里面 看到 有一个长按的函数,但它的持续时间不知道如何调用。
搜了帖子 基本上就是如何长按,但是没有长按 多久。
下面是 它原生的函数

def long_press(self, el=None, x=None, y=None, duration=1000):
"""Begin a chain with a press down that lasts duration milliseconds
"""
self._add_action('longPress', self._get_opts(el, x, y))

return self

我在谷歌上搜了写方法,但是还是不能实现

action = TouchAction(self.driver)
el = self.driver.find_element_by_id('XXXXX')
action.long_press(el).wait(10000).release().perform()
以上的方法仅能实现,不到1秒的短按,达不到长按10秒的效果
报的是这个错:WebDriverException: Message: u'An unknown server-side error occurred while processing the command.'

谢谢大家。第一次发帖,希望没有触犯发帖规范 ~~~~~~ :)

共收到 16 条回复 时间 点赞
driver.executeScript("mobile: tap", new HashMap<String, Double>() {
{
put("tapCount", (double) 1);
put("touchCount", (double) 1);
put("duration", 0.5);
put("x", x);
put("y", y);
}
});

如果你要調整時間的話,修改duration的參數即可

不是有duration么,只是默认参数罢了

tap方法可以试一下

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

经过实践,发现不可行
不管duration传多少,在UIAutomation中time总是0.2,依然是普通Tap的效果
Code:

self.driver.tap([(176, 86)], duration=5000)

附Log:

info: --> POST /wd/hub/session/75108063-b03d-42f1-9922-0b31b641cf3e/touch/perform {"sessionId":"75108063-b03d-42f1-9922-0b31b641cf3e","actions":[{"action":"longPress","options":{"y":86,"x":176}},{"action":"release","options":{}}]}

debug: Appium request initiated at /wd/hub/session/75108063-b03d-42f1-9922-0b31b641cf3e/touch/perform

debug: Request received with params: {"sessionId":"75108063-b03d-42f1-9922-0b31b641cf3e","actions":[{"action":"longPress","options":{"y":86,"x":176}},{"action":"release","options":{}}]}

debug: Pushing command to appium work queue: "target.touch([{\"touch\":[{\"x\":176,\"y\":86}],\"time\":0.2}])"
debug: Sending command to instruments: target.touch([{"touch":[{"x":176,"y":86}],"time":0.2}])
debug: Sending command to instruments: target.touch([{"touch":[{"x":176,"y":86}],"time":0.2}])
debug: [INST] 2014-08-30 02:51:33 +0000 Debug: evaluation finished

debug: [INST] 2014-08-30 02:51:33 +0000 Debug: Running system command #12: /Applications/Appium.app/Contents/Resources/node/bin/node /Applications/Appium.app/Contents/Resources/node_modules/appium/node_modules/appium-uiauto/bin/command-proxy-client.js /tmp/instruments_sock 2,{"status":0,"value":null}...


debug: [INST] 2014-08-30 02:51:38 +0000 Debug: Got new command 12 from instruments: target.touch([{"touch":[{"x":176,"y":86}],"time":0.2}])


debug: [INST] 2014-08-30 02:51:38 +0000 Debug: evaluating target.touch([{"touch":[{"x":176,"y":86}],"time":0.2}])


debug: [INST] 2014-08-30 02:51:38 +0000 Debug: target.touch(__NSCFArray)

用UIAutomation自己的tapWithOptions({tapOffset:{x:2.02, y:3.41}, duration:10});就可以

@gigayaya @xiaomayi0323
谢谢你们的回答,很可惜,还是没找到合适的解决方案,我不太想用坐标定位的方法,毕竟有机型的适配问题。

还在找解决方法中,谢谢大家。

#6楼 @alvn
哈哈,非常感谢,就是这样的,这个办法好。

也要谢谢大家的思路。。

@seasoncool2011 这是我被折磨了2天的成果啊:)

#8楼 @alvn 你知道如何模拟长按返回键吗?或者楼主@seasoncool2011 你知道吗?多谢帮助 :)

#6楼 @alvn 你好,你上面说的方法确定有效吗?我在appium 1.4的版本测试多次仍然只有1s,还需要其他特殊代码?

TouchAction action = new TouchAction(driver);
action.longPress(element).waitAction(5000).release().perform();
完美的解决了ios长按的功能

#11楼 @kgjinsonghao 亲,我的情况跟你一样,测试多次,长按的时间都不长,想请问你解决了吗?

#13楼 @miumiu 我这边也是这样,请问你那边解决了吗?

我在IOS中使用appium也遇到了类似这个问题,不过我要实现的是拖动,封装的drag_and_drop方法很坑爹,根本用不了。
需要在它的基础上加个等待时间就好。

TouchAction(self.driver).long_press(origin_el).wait(5000).move_to(destination_el).release().perform()

扬帆 回复

这个也已做到拖动是吗

需要 Sign In 后方可回复, 如果你还没有账号请点击这里 Sign Up