Appium中如何获取iOS设备显示的最大宽高坐标?
How to get the maximum width and height coordinates of an iOS device display in Appium?
我在 iOS 和 Android 设备上使用 appium python-client library to run a [py.]test。我注意到许多 iOS 测试因 tap point is not within the bounds of the screen
而崩溃。查看测试似乎很奇怪,因为提供的坐标似乎在设备显示的报告范围内。
以下片段说明了我的问题:
from appium.webdriver.common.touch_action import TouchAction
def test_max_screen_size(appium_driver):
driver = appium_driver
window_size = driver.get_window_size()
max_width = window_size["width"] - 1
max_height = window_size["height"] - 1
action = TouchAction(driver)
action.tap(None, max_width, max_height).perform()
我在这里尝试点击显示器的最极端点。
此测试 在 Android 设备上通过,在 [=] 上失败 58=]iOS 模拟器设备(我还没有测试真正的 iOS 设备 - 但我确定它也会崩溃)。
这是 Appium 错误还是我做错了什么?
我的环境:
Appium 1.3.4
iOS 8.1
current Appium Python client
Python 2.7.6
更新
我在这个问题上花了更多时间。
首先,我 运行 通过 Instruments 创建这个简单的 UIAutomation 脚本:
var target = UIATarget.localTarget();
var max_width = target.rect().size.width;
var max_height = target.rect().size.height
UIALogger.logMessage("width: " + max_width + " height:" + max_height);
target.tap({x:max_width, y:max_height});
当你 运行 在 iPhone 4s 上你得到 max_width = 320
和 max_height = 480
因为它是 2x
retina display - 正好Appium 告诉我们什么以及预期是什么。
如果您增加任一变量,脚本将按预期失败并显示:Script threw an uncaught JavaScript error: tap point is not within the bounds of the screen on line 8 of New Script
。
这意味着 Appium 必须发送一些 Instruments 不喜欢的东西。
因此,下一个故障点可能是 appium python 客户端。我使用 pdb 到达客户端与 appium 服务器对话的位置:
../remote_connection.py(349)execute()
-> return self._request(command_info[0], url, body=data)
(Pdb) l
344 command_info = self._commands[command]
345 assert command_info is not None, 'Unrecognised command %s' % command
346 data = utils.dump_json(params)
347 path = string.Template(command_info[1]).substitute(params)
348 url = '%s%s' % (self._url, path)
349 -> return self._request(command_info[0], url, body=data)
350
351 def _request(self, method, url, body=None):
352 """
353 Send an HTTP request to the remote server.
354
(Pdb) url
u'http://127.0.0.1:4723/wd/hub/session/c9e49cb0-d291-4fb5-8aef-d89b9ceaa759/touch/perform'
(Pdb) data
'{"sessionId": "c9e49cb0-d291-4fb5-8aef-d89b9ceaa759",
"actions": [{"action": "tap", "options": {"y": 479, "x": 319, "count": 1}}]}'
这表明 appium python 客户端 似乎也在做您期望的事情。
这使得 Appium 服务器成为下一个故障点...
好的,经过一段时间的挖掘,我终于找到了答案。与 Instruments 不同,在 Instruments 中您可以执行相对于全屏的任意点击,Appium 选择将您限制在应用程序的范围内。这意味着,如果您显示 菜单栏 ,它将减少您的可点击区域,减少菜单栏的高度。
您可以在 appium 服务器日志中看到:
1 info: [debug] Got result from instruments: {"status":0,"value":{"width":320,"height":480}}
2 info: [debug] Responding to client with success: {"status":0,"value":{"width":320,"height":480},"sessionId":"b363bc3f-969b-4d7c-94e9-a504ffa08661"}
3 info: <-- GET /wd/hub/session/b363bc3f-969b-4d7c-94e9-a504ffa08661/window/current/size 200 90.756 ms - 98 {"status":0,"value":{"width":320,"height":480},"sessionId":"b363bc3f-969b-4d7c-94e9-a504ffa08661"}
4 info: --> POST /wd/hub/session/b363bc3f-969b-4d7c-94e9-a504ffa08661/touch/perform {"sessionId":"b363bc3f-969b-4d7c-94e9-a504ffa08661","actions":[{"action":"tap","options":{"y":479,"x":319,"count":1}}]}
5 info: [debug] Pushing command to appium work queue: "UIATarget.localTarget().frontMostApp().rect()"
6 info: [debug] Sending command to instruments: UIATarget.localTarget().frontMostApp().rect()
7 info: [debug] [INST] 2015-01-29 00:27:53 +0000 Debug: Got new command 8 from instruments: UIATarget.localTarget().frontMostApp().rect()
8 info: [debug] [INST] 2015-01-29 00:27:53 +0000 Debug: evaluating UIATarget.localTarget().frontMostApp().rect()
9 info: [debug] [INST] 2015-01-29 00:27:53 +0000 Debug: evaluation finished
10 info: [debug] [INST] 2015-01-29 00:27:53 +0000 Debug: responding with:
11 info: [debug] [INST] 2015-01-29 00:27:53 +0000 Debug: Running system command #9: /usr/local/Cellar/node/0.10.35_2/bin/node /usr/local/lib/node_modules/appium/node_modules/appium-uiauto/bin/command-proxy-client.js /tmp/instruments_sock 2,{"status":0,"value":{"origin":{"x":0,"y":20},"size":{"width":320,"height":460}}}...
12 info: [debug] Socket data received (82 bytes)
13 info: [debug] Socket data being routed.
14 info: [debug] Got result from instruments: {"status":0,"value":{"origin":{"x":0,"y":20},"size":{"width":320,"height":460}}}
15 info: [debug] Pushing command to appium work queue: "UIATarget.localTarget().frontMostApp().tapWithOptions({\"tapOffset\":{\"x\":0.996875,\"y\":1.041304347826087},\"tapCount\":1,\"touchCount\":1})"
16 info: [debug] Sending command to instruments: UIATarget.localTarget().frontMostApp().tapWithOptions({"tapOffset":{"x":0.996875,"y":1.041304347826087},"tapCount":1,"touchCount":1})
在 line 2
中,您可以看到服务器如何响应 设备分辨率。
在 line 14
中,您可以看到应用程序分辨率:320x460
,在下一行中,您可以看到我的(现在是相对的)点击实际到达的位置:{"x":0.996875,"y":1.041304347826087}
。
这是出乎意料的,我希望 API 描述能指出这一点 - Appium 中的选项卡是相对于应用程序执行的 -window(不是完整显示 window ).
长话短说
我现在必须使用 <driver>.find_element_by_xpath('//UIAApplication[1]').size
来替换 <driver>.get_window_size()
为 iOS。
我在 iOS 和 Android 设备上使用 appium python-client library to run a [py.]test。我注意到许多 iOS 测试因 tap point is not within the bounds of the screen
而崩溃。查看测试似乎很奇怪,因为提供的坐标似乎在设备显示的报告范围内。
以下片段说明了我的问题:
from appium.webdriver.common.touch_action import TouchAction
def test_max_screen_size(appium_driver):
driver = appium_driver
window_size = driver.get_window_size()
max_width = window_size["width"] - 1
max_height = window_size["height"] - 1
action = TouchAction(driver)
action.tap(None, max_width, max_height).perform()
我在这里尝试点击显示器的最极端点。
此测试 在 Android 设备上通过,在 [=] 上失败 58=]iOS 模拟器设备(我还没有测试真正的 iOS 设备 - 但我确定它也会崩溃)。
这是 Appium 错误还是我做错了什么?
我的环境:
Appium 1.3.4
iOS 8.1
current Appium Python client
Python 2.7.6
更新
我在这个问题上花了更多时间。
首先,我 运行 通过 Instruments 创建这个简单的 UIAutomation 脚本:
var target = UIATarget.localTarget();
var max_width = target.rect().size.width;
var max_height = target.rect().size.height
UIALogger.logMessage("width: " + max_width + " height:" + max_height);
target.tap({x:max_width, y:max_height});
当你 运行 在 iPhone 4s 上你得到 max_width = 320
和 max_height = 480
因为它是 2x
retina display - 正好Appium 告诉我们什么以及预期是什么。
如果您增加任一变量,脚本将按预期失败并显示:Script threw an uncaught JavaScript error: tap point is not within the bounds of the screen on line 8 of New Script
。
这意味着 Appium 必须发送一些 Instruments 不喜欢的东西。
因此,下一个故障点可能是 appium python 客户端。我使用 pdb 到达客户端与 appium 服务器对话的位置:
../remote_connection.py(349)execute()
-> return self._request(command_info[0], url, body=data)
(Pdb) l
344 command_info = self._commands[command]
345 assert command_info is not None, 'Unrecognised command %s' % command
346 data = utils.dump_json(params)
347 path = string.Template(command_info[1]).substitute(params)
348 url = '%s%s' % (self._url, path)
349 -> return self._request(command_info[0], url, body=data)
350
351 def _request(self, method, url, body=None):
352 """
353 Send an HTTP request to the remote server.
354
(Pdb) url
u'http://127.0.0.1:4723/wd/hub/session/c9e49cb0-d291-4fb5-8aef-d89b9ceaa759/touch/perform'
(Pdb) data
'{"sessionId": "c9e49cb0-d291-4fb5-8aef-d89b9ceaa759",
"actions": [{"action": "tap", "options": {"y": 479, "x": 319, "count": 1}}]}'
这表明 appium python 客户端 似乎也在做您期望的事情。
这使得 Appium 服务器成为下一个故障点...
好的,经过一段时间的挖掘,我终于找到了答案。与 Instruments 不同,在 Instruments 中您可以执行相对于全屏的任意点击,Appium 选择将您限制在应用程序的范围内。这意味着,如果您显示 菜单栏 ,它将减少您的可点击区域,减少菜单栏的高度。
您可以在 appium 服务器日志中看到:
1 info: [debug] Got result from instruments: {"status":0,"value":{"width":320,"height":480}}
2 info: [debug] Responding to client with success: {"status":0,"value":{"width":320,"height":480},"sessionId":"b363bc3f-969b-4d7c-94e9-a504ffa08661"}
3 info: <-- GET /wd/hub/session/b363bc3f-969b-4d7c-94e9-a504ffa08661/window/current/size 200 90.756 ms - 98 {"status":0,"value":{"width":320,"height":480},"sessionId":"b363bc3f-969b-4d7c-94e9-a504ffa08661"}
4 info: --> POST /wd/hub/session/b363bc3f-969b-4d7c-94e9-a504ffa08661/touch/perform {"sessionId":"b363bc3f-969b-4d7c-94e9-a504ffa08661","actions":[{"action":"tap","options":{"y":479,"x":319,"count":1}}]}
5 info: [debug] Pushing command to appium work queue: "UIATarget.localTarget().frontMostApp().rect()"
6 info: [debug] Sending command to instruments: UIATarget.localTarget().frontMostApp().rect()
7 info: [debug] [INST] 2015-01-29 00:27:53 +0000 Debug: Got new command 8 from instruments: UIATarget.localTarget().frontMostApp().rect()
8 info: [debug] [INST] 2015-01-29 00:27:53 +0000 Debug: evaluating UIATarget.localTarget().frontMostApp().rect()
9 info: [debug] [INST] 2015-01-29 00:27:53 +0000 Debug: evaluation finished
10 info: [debug] [INST] 2015-01-29 00:27:53 +0000 Debug: responding with:
11 info: [debug] [INST] 2015-01-29 00:27:53 +0000 Debug: Running system command #9: /usr/local/Cellar/node/0.10.35_2/bin/node /usr/local/lib/node_modules/appium/node_modules/appium-uiauto/bin/command-proxy-client.js /tmp/instruments_sock 2,{"status":0,"value":{"origin":{"x":0,"y":20},"size":{"width":320,"height":460}}}...
12 info: [debug] Socket data received (82 bytes)
13 info: [debug] Socket data being routed.
14 info: [debug] Got result from instruments: {"status":0,"value":{"origin":{"x":0,"y":20},"size":{"width":320,"height":460}}}
15 info: [debug] Pushing command to appium work queue: "UIATarget.localTarget().frontMostApp().tapWithOptions({\"tapOffset\":{\"x\":0.996875,\"y\":1.041304347826087},\"tapCount\":1,\"touchCount\":1})"
16 info: [debug] Sending command to instruments: UIATarget.localTarget().frontMostApp().tapWithOptions({"tapOffset":{"x":0.996875,"y":1.041304347826087},"tapCount":1,"touchCount":1})
在 line 2
中,您可以看到服务器如何响应 设备分辨率。
在 line 14
中,您可以看到应用程序分辨率:320x460
,在下一行中,您可以看到我的(现在是相对的)点击实际到达的位置:{"x":0.996875,"y":1.041304347826087}
。
这是出乎意料的,我希望 API 描述能指出这一点 - Appium 中的选项卡是相对于应用程序执行的 -window(不是完整显示 window ).
长话短说
我现在必须使用 <driver>.find_element_by_xpath('//UIAApplication[1]').size
来替换 <driver>.get_window_size()
为 iOS。