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

Posted

技术标签:

【中文标题】使用 appium-python-client 在 android 中缩放操作【英文标题】:Zoom action in android using appium-python-client 【发布时间】:2016-11-28 15:21:39 【问题描述】:

有人知道如何通过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()

但这同样不会执行任何缩放。而是向下滚动列表。有没有人知道这里出了什么问题。

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 

【问题讨论】:

【参考方案1】:

MultiAction 尝试看起来不错,但是在我手机的原生相机应用上进行了一些测试后,我能够通过在 moveTo() 之后添加 500 毫秒 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() 手势太快了,并没有真正做太多。在 Github 的 Appium 文档中提到,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 场景中使用 MultiAction。

【讨论】:

我认为 500ms 在大多数情况下都可以正常工作。我添加了一个指向文档的链接,其中提到 wait 是为了准确地为手势计时。如果对您有帮助,您能否将我的回答标记为已接受? 在答案中添加了捏合手势。通过调整起始坐标和方向,似乎可以使用完全相反的手势。 这个解决方案在 appium 版本 1.9.1 上不适合我

以上是关于使用 appium-python-client 在 android 中缩放操作的主要内容,如果未能解决你的问题,请参考以下文章

配置Appium-Python-Client

Appium搭建五:安装selenium+Appium-Python-Client

pip 升级 Appium-Python-Client

Pycharm中使用from appium import webdriver时报错:ModuleNotFoundError: No module named 'appium'(示例代码

Pycharm中使用from appium import webdriver时报错:ModuleNotFoundError: No module named 'appium'(示例代码

Appium环境配置