python - Appium中如何获取iOS设备显示的最大宽高坐标?

标签 python ios instruments appium pytest

我正在使用 appium python-client图书馆运行 [py.]test在 iOS 和安卓设备上。我注意到许多 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 设备上通过,在 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 = 320max_height = 480 因为它是 2x retina display - 正是 Appium 告诉我们的和预期的。

如果您增加任一变量,脚本将按预期失败并显示:脚本引发未捕获的 JavaScript 错误:点击点不在新脚本第 8 行的屏幕范围内

这意味着 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 中的选项卡是相对于应用程序窗口(而不是完整显示窗口)执行的。

长话短说

我现在必须使用 <driver>.find_element_by_xpath('//UIAApplication[1]').size替换 <driver>.get_window_size()适用于 iOS。

关于python - Appium中如何获取iOS设备显示的最大宽高坐标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28199128/

相关文章:

ios - iPhone X 横向的 UICollectionView

iphone - 有以下问题可以提交申请吗?

ios - 为什么在滚动 uitableviewcontroller 时会调用layoutIfNeeded?

python - 为什么 numpy.fromstring 读取数字是错误的?

python - SqlAlchemy - 按定义为 ForeignKey 的字段过滤

ios - iOS 共享实例在哪里释放

ios - NSMutable 数组未在另一种方法中返回值

python - 理解 Python 中的 Axis

python - 日期范围 : is there a more pythonic way? 中的所有星期二和星期三

ios - Xcode/Swift-无法解决乐器的内存问题