使用 appium-python-client 在 android 中缩放操作

Zoom action in android using appium-python-client

有人知道如何通过 appium python 客户端在 android 中缩放元素吗?

我目前正在使用

self.driver.zoom(self.element, percent) 但这给出了一个错误

self.driver.zoom(self.element, percent)
File "/usr/local/lib/python2.7/site-packages/appium/webdriver/webdriver.py", line 308, in zoom
self.execute_script('mobile: pinchOpen', opts)
File "/usr/local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 461, in execute_script
{'script': script, 'args':converted_args})['value']
File "/usr/local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 233, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python2.7/site-packages/appium/webdriver/errorhandler.py", line 29, in check_response
raise wde
WebDriverException: Message: Method has not yet been implemented

我也试过 MultiAction.

loc = self.element.location
print loc
xx, yy = loc["x"], loc["y"]
xx=700
action1 = TouchAction(self.driver)
action1.long_press(x=xx, y=yy).move_to(x=0, y=1000).release()
action2 = TouchAction(self.driver)
action2.long_press(x=xx, y=yy).move_to(x=0, y=-1000).release()
m_action = MultiAction(self.driver)
m_action.add(action1, action2)
m_action.perform()

但是这同样不会执行任何 zoom.Instead 它会向下滚动 list.Does 任何人都知道这里出了什么问题。

Appium Logs

[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got data from client: {"cmd":"action","action":"element:getLocation","params":{"elementId":"83"}}
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command of type ACTION
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command action: getLocation
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Returning result: {"status":0,"value":{"x":0,"y":1225}}
[debug] [AndroidBootstrap] Received command result from bootstrap
[MJSONWP] Responding to client with driver.getLocation() result: {"x":0,"y":1225}
[HTTP] <-- GET /wd/hub/session/c1a4d17f-0dc6-4445-bfad-776ec65bddb5/element/83/location 200 26 ms - 88 
[HTTP] --> POST /wd/hub/session/c1a4d17f-0dc6-4445-bfad-776ec65bddb5/touch/multi/perform {"sessionId":"c1a4d17f-0dc6-4445-bfad-776ec65bddb5","actions":[[{"action":"longPress","options":{"y":1225,"x":700,"duration":1000}},{"action":"moveTo","options":{"y":1000,"x":0}},{"action":"release","options":{}}],[{"action":"longPress","options":{"y":1225,"x":700,"duration":1000}},{"action":"moveTo","options":{"y":-1000,"x":0}},{"action":"release","options":{}}]]}
[MJSONWP] Calling AppiumDriver.performMultiAction() with args: [[[{"action":"longPress","o...
[debug] [AndroidBootstrap] Sending command to android: {"cmd":"action","action":"performMultiPointerGesture","params":{"actions":[[{"action":"longPress","time":0.005,"touch":{"y":1225,"x":700,"duration":1000}},{"action":"moveTo","time":0.01,"touch":{"y":2225,"x":700}}],[{"action":"longPress","time":0.005,"touch":{"y":1225,"x":700,"duration":1000}},{"action":"moveTo","time":0.01,"touch":{"y":225,"x":700}}]]}}
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got data from client: {"cmd":"action","action":"performMultiPointerGesture","params":{"actions":[[{"action":"longPress","time":0.005,"touch":{"y":1225,"x":700,"duration":1000}},{"action":"moveTo","time":0.01,"touch":{"y":2225,"x":700}}],[{"action":"longPress","time":0.005,"touch":{"y":1225,"x":700,"duration":1000}},{"action":"moveTo","time":0.01,"touch":{"y":225,"x":700}}]]}}
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command of type ACTION
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command action: performMultiPointerGesture
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Returning result: {"status":0,"value":"OK"}
[debug] [AndroidBootstrap] Received command result from bootstrap
[MJSONWP] Responding to client with driver.performMultiAction() result: "OK"
[HTTP] <-- POST /wd/hub/session/c1a4d17f-0dc6-4445-bfad-776ec65bddb5/touch/multi/perform 200 133 ms - 76 
[HTTP] --> DELETE /wd/hub/session/c1a4d17f-0dc6-4445-bfad-776ec65bddb5 {}

MultiAction 尝试看起来不错,但在我的 phone 的本机相机应用程序上测试了一下之后,我能够通过在 moveTo() 之后添加 500ms wait() 来获得不错的缩放手势:

# Zoom
action1.long_press(x=xx, y=yy).move_to(x=0, y=50).wait(500).release()
action2.long_press(x=xx, y=yy).move_to(x=0, y=-50).wait(500).release()
m_action.add(action1, action2)

# Pinch
action3.long_press(x=xx, y=yy-50).move_to(x=0, y=50).wait(500).release()
action4.long_press(x=xx, y=yy+50).move_to(x=0, y=-50).wait(500).release()
m_action2.add(action3, action4)

m_action.perform()
m_action2.perform()

这导致相机应用的缩放效果很好但很慢。没有 wait() 手势太快了,并没有真正做太多。 Appium 的文档 Github 中提到 wait() 可用于控制手势的时间:https://github.com/appium/appium/blob/master/docs/en/writing-running-appium/touch-actions.md

我将 xxyy 设置到相机应用程序的屏幕中间:

xx = self.driver.get_window_size()['width']/2
yy = self.driver.get_window_size()['height']/2

请记住,坐标永远不能超出设备屏幕边界,因此如果您想将其变成可重复使用的函数,检查屏幕边界可能会有用。

我在自动化 Chrome 时也无法使用 MultiAction 手势(甚至在更改为 NATIVE_APP 上下文时也无法使用。手势没有效果。)所以有可能在 WebView 场景中使用 MultiActions不支持。