iOS + appium java客户端:长按TouchAction不起作用?

时间:2015-12-21 17:51:45

标签: java ios appium

我正在尝试在原生iOS应用中自动执行长按触摸操作。

方法1:模拟左上角长按不起作用。

new TouchAction(driver).longPress(0,0).perform() Appium日志显示没有错误:

info: [debug] [INST] 2015-12-21 17:41:19 +0000 Debug: Got new command 5 from instruments: target.touch([{"touch":[{"x":0,"y":0}],"time":0.2}])
info: [debug] [INST] 2015-12-21 17:41:19 +0000 Debug: evaluating target.touch([{"touch":[{"x":0,"y":0}],"time":0.2}])
info: [debug] [INST] 2015-12-21 17:41:19 +0000 Debug: target.touch(__NSCFArray)
info: [debug] [INST] 2015-12-21 17:41:20 +0000 Debug: evaluation finished
info: [debug] [INST] 2015-12-21 17:41:20 +0000 Debug: responding with:
info: [debug] [INST] 2015-12-21 17:41:20 +0000 Debug: Running system command #6: /usr/local/Cellar/node/4.1.1/bin/node /usr/local/lib/node_modules/appium/node_modules/appium-uiauto/bin/command-proxy-client.js /tmp/instruments_sock 2,{"status":0,"value":""}...
info: [debug] Socket data received (25 bytes)
info: [debug] Socket data being routed.
info: [debug] Got result from instruments: {"status":0,"value":""}
info: [debug] Responding to client with success: {"status":0,"value":"","sessionId":"332523ee-5a1b-427a-8beb-3c150587d08b"}

方法2:执行javascript以模拟长按

((IOSDriver)driver).executeScript("mobile: longClick", ImmutableMap.of("x", 5, "y", 5));

Appium日志异常如下:

TypeError: cb is not a function
    at [object Object].iOSController.touchLongClick (/usr/local/lib/node_modules/appium/lib/devices/ios/ios-controller.js:614:3)
    at Object.exports.touchLongClick [as longClick] (/usr/local/lib/node_modules/appium/lib/server/controller.js:371:16)
    at Object.exports.executeMobileMethod (/usr/local/lib/node_modules/appium/lib/server/controller.js:984:22)
    at exports.execute (/usr/local/lib/node_modules/appium/lib/server/controller.js:940:15)
    at Layer.handle [as handle_request] (/usr/local/lib/node_modules/appium/node_modules/express/lib/router/layer.js:82:5)

2 个答案:

答案 0 :(得分:1)

您必须将2个参数传递给longPress()方法。它们是您要保留的元素(下面的代码中的元素)和时间(2000毫秒)它必须保持多长时间。

MobileElement element = driver.findElementByClassName("xxx");
new TouchAction(driver).longPress(element, 2000).perform();

答案 1 :(得分:0)

new TouchAction(driver).longPress(0,0).perform()更改为

TouchAction action = new TouchAction(driver).longPress(0,0).release();

action.perform();

确保你有正确的坐标。