问题描述
有人知道如何通过 appium python 客户端在 android 中缩放元素吗?
Does anybody know how to zoom an element in android via appium python client?
我正在使用
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()
但这同样不会执行任何缩放.而是向下滚动列表.有人知道这里出了什么问题吗.
But again this does not perform any zoom.Instead it scrolls down the list.Does anybody have any idea about what's wrong here.
Appium 日志
[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 尝试看起来不错,但在我手机的原生相机应用程序上测试了一些之后,我能够通过添加 500ms wait() 来获得一个不错的缩放手势在 moveTo() 之后:
The MultiAction attempt looks good, but after testing a bit on my phone's native camera app, I was able to get a nice zoom gesture by adding 500ms wait() after the moveTo():
# 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一个>
This resulted in a nice and slow zoom to the camera app. Without the wait() the gestures were too quick and didn't really do much. It is mentioned at Appium's documentation at Github, that wait() can be used to control the timing of a gesture: https://github.com/appium/appium/blob/master/docs/en/writing-running-appium/touch-actions.md
我将 xx
和 yy
设置为我的相机应用程序的屏幕中间:
I set the xx
and yy
to the middle of the screen in my camera app with:
xx = self.driver.get_window_size()['width']/2
yy = self.driver.get_window_size()['height']/2
请记住,坐标永远不应超出设备屏幕边界,因此如果您想将其变成可重复使用的功能,检查屏幕边界可能会很有用.
Please remember that coordinates should never go out of device screen bounds, so checks for screen borders may be useful, if you want to make this into a re-usable function.
在自动化 Chrome 时,我也无法使用 MultiAction 手势(即使在更改为 NATIVE_APP 上下文时也无法使用.手势无效.)因此可能不支持在 WebView 场景中使用 MultiAction.
I also couldn't use the MultiAction gestures when automating Chrome (not even when changing to NATIVE_APP context. The gestures had no effect.) so it's possible that using MultiActions on WebView scenarios isn't supported.
这篇关于使用 appium-python-client 在 android 中缩放操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!