【问题标题】:How to get the maximum width and height coordinates of an iOS device display in Appium?如何在 Appium 中获取 iOS 设备显示的最大宽度和高度坐标?
【发布时间】:2015-03-27 18:12:41
【问题描述】:

我正在使用 appium python-client 库在 iOS 和 Android 设备上运行 [py.]test。我注意到许多 iOS 测试在使用 tap point is not within the bounds of the screen 时崩溃了。查看测试似乎很奇怪,因为提供的坐标似乎在设备显示的报告范围内。

下面的 sn-p 说明了我的问题:

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 告诉我们的内容和预期的内容。

如果您增加任一变量,脚本将按预期失败: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 服务器成为下一个故障点...

【问题讨论】:

    标签: python ios instruments appium pytest


    【解决方案1】:

    好的,经过一番挖掘,我终于找到了答案。与 Instruments 不同,您可以相对于全屏执行任何点击,Appium 选择将您限制在应用程序的范围内。这意味着如果您有 menu-bar 显示,它将通过菜单栏的高度减少您的可点击区域。

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

    长话短说

    我现在必须使用 &lt;driver&gt;.find_element_by_xpath('//UIAApplication[1]').size 替换 iOS 的 &lt;driver&gt;.get_window_size()

    【讨论】:

      猜你喜欢
      • 2014-07-27
      • 2012-12-17
      • 1970-01-01
      • 1970-01-01
      • 2017-02-14
      • 1970-01-01
      • 2012-04-27
      • 1970-01-01
      • 2012-11-07
      相关资源
      最近更新 更多