【问题标题】:XCUITest get URL valueXCUITest 获取 URL 值
【发布时间】:2017-12-09 13:52:18
【问题描述】:

我将 XCUITest 与 XCode 版本 8.3.3 一起使用。该应用程序有一个登录按钮,用于启动浏览器窗口。想知道是否有人可以告诉我如何访问打开的 URL 的值。我想验证我们在选择登录按钮时启动了正确的 URL。

let app = XCUIApplication()
let loginButton = app.buttons["Login"]
loginButton.tap()

此时浏览器窗口打开:

如何获取 xyz.com 的价值?

使用辅助功能检查器,我可以看到:

【问题讨论】:

    标签: ios xcode8 xctest xcode-ui-testing ios-ui-automation


    【解决方案1】:

    我创建了一个小项目,在启动时将 google.com 加载到 SFSafariViewController 中。当我针对这个应用程序运行 UI 测试并打印窗口的调试描述时,我得到以下信息:

    (lldb) po XCUIApplication().windows.descendants(matching: .any)
        t =    29.26s     Use cached accessibility hierarchy for com.tito.Safari
        t =    29.27s     Find: Descendants matching type Window
        t =    29.27s     Find: Descendants matching type Any
    Find: Target Application 0x6000000afa20
      Output: {
        Application 0x6000001684c0: {{0.0, 0.0}, {375.0, 667.0}}, label: 'Safari'
      }
      ↪︎Find: Descendants matching type Window
        Output: {
          Window 0x600000169a80: Main Window, {{0.0, 0.0}, {375.0, 667.0}}
          Window 0x60000016a8c0: {{0.0, 0.0}, {375.0, 667.0}}
          Window 0x60000016fb40: {{0.0, 0.0}, {375.0, 667.0}}
          Window 0x60000016c780: {{0.0, 0.0}, {375.0, 667.0}}
          Window 0x600000169e40: {{-0.0, -0.0}, {375.0, 667.0}}
        }
        ↪︎Find: Descendants matching type Any
          Output: {
            Button 0x608000169900: traits: 8589934593, {{9.5, 22.0}, {43.0, 34.0}}, label: 'Done'
            Other 0x608000165b80: traits: 8589935104, {{68.5, 24.5}, {295.0, 29.0}}, identifier: 'URL', label: 'Address', value: 10%
            Button 0x608000169840: traits: 8589934593, {{336.5, 24.5}, {29.0, 29.0}}, identifier: 'StopButton', label: 'stop'
            Button 0x600000168100: traits: 146028888065, {{66.5, 24.5}, {299.0, 29.0}}, identifier: 'URL', label: 'Address', value: ‎google.com
            Other 0x6080001696c0: traits: 8589934592, {{0.0, 20.0}, {375.0, 44.0}}
            WebView 0x608000167ec0: traits: 8589934592, {{0.0, 0.0}, {375.0, 667.0}}
            Other 0x608000167e00: traits: 8589934592, {{0.0, 0.0}, {375.0, 667.0}}
            Other 0x608000169b40: traits: 8589934592, {{0.0, 0.0}, {375.0, 667.0}}
            Other 0x608000167a40: traits: 8589934592, {{0.0, 622.5}, {375.0, 0.5}}
            Button 0x6080001675c0: traits: 8589934849, {{0.5, 625.0}, {42.0, 40.0}}, label: 'Back'
            Button 0x600000167bc0: traits: 8589934849, {{104.0, 625.0}, {42.0, 40.0}}, label: 'Forward'
            Button 0x600000167c80: traits: 8589934593, {{207.5, 624.0}, {51.0, 40.0}}, label: 'Share'
            Button 0x60000016fa80: traits: 8589934593, {{320.5, 625.5}, {55.0, 40.0}}, label: 'Open in Safari'
            Toolbar 0x608000167b00: traits: 35192962023424, {{0.0, 623.0}, {375.0, 44.0}}
            Other 0x608000165ac0: traits: 8589934592, {{0.0, 0.0}, {375.0, 667.0}}
            Other 0x608000169780: traits: 8589934592, {{0.0, 0.0}, {375.0, 667.0}}
            Other 0x608000169480: 
            Other 0x608000169180: traits: 8589934592, {{0.0, 0.0}, {375.0, 667.0}}
            Other 0x60000016cc00: traits: 8589934592, {{0.0, 64.0}, {24.0, 603.0}}
            Other 0x6080001690c0: traits: 8589934592, {{0.0, 0.0}, {375.0, 667.0}}
            Other 0x600000168400: {{0.0, 0.0}, {375.0, 667.0}}
            Other 0x600000169180: traits: 8589934592, {{0.0, 0.0}, {375.0, 667.0}}
            Other 0x600000169f00: 
            Other 0x60000016a680: traits: 8589934592, {{0.0, 0.0}, {375.0, 667.0}}
            Other 0x600000167b00: {{0.0, 0.0}, {375.0, 20.0}}
            Other 0x60000016fd80: traits: 8388608, {{6.0, 0.0}, {39.0, 20.0}}
            Other 0x60000016e340: traits: 8388608, {{50.0, 0.0}, {13.0, 20.0}}, label: '3 of 3 Wi-Fi bars', value: SSID
            Other 0x60000016fe40: traits: 8389120, {{161.0, 0.0}, {57.0, 20.0}}, label: '10:33 AM'
            Other 0x60000016e880: traits: 8388608, {{337.0, 0.0}, {33.0, 20.0}}, label: '-100% battery power'
            Other 0x60000016dc80: {{0.0, 0.0}, {375.0, 20.0}}
            StatusBar 0x600000169240: {{0.0, 0.0}, {375.0, 20.0}}
            Other 0x60000016a440: traits: 8589934592, {{0.0, 0.0}, {375.0, 667.0}}
            Other 0x600000170140: 
            Other 0x60000016da40: traits: 8589934592, {{-0.0, -0.0}, {375.0, 667.0}}
            Other 0x60000016c600: 
            Other 0x60000016af80: traits: 8589934592, {{-0.0, -0.0}, {375.0, 667.0}}
          }
    

    您可以看到包含值google.com 的元素被视为无障碍框架的按钮。

    Button 0x600000168100: traits: 146028888065, {{66.5, 24.5}, {299.0, 29.0}}, identifier: 'URL', label: 'Address', value:
    

    这似乎是错误的,但事实就是如此,对此您无能为力。访问该元素值的方法如下

    XCUIApplication().buttons['URL'].value!
    

    唯一的问题是它返回以下内容

    ‎gen.xyz, secure and validated connection
    

    所以最后要做的是解析这个,你会得到地址

    let value = XCUIApplication().buttons["URL"].value as! String
    let address = value.components(separatedBy: ",").first!
    

    不过,这绝对是 hacky,如果 Apple 决定在可访问性方面将自己的建议应用到自己的产品中,将来可能会改变。

    【讨论】:

    • 感谢您的详细解答!!很有帮助。
    • 返回值时,开头有\u(e2),你知道为什么吗?
    • 嗯,不,您要加载什么 URL?
    • 查看这篇我可以添加屏幕截图和更多信息的帖子:stackoverflow.com/questions/44955383/…
    • 您是否尝试过使用其他 URL 来查看问题是否来自那里?
    【解决方案2】:

    我发现其他响应没有获得完整的 URL。这两种方法都只会为您获取域名。

    通过首先点击 URL 栏,我可以获取结果文本字段的值。

    let safari = XCUIApplication(bundleIdentifier: "com.apple.mobilesafari")
    safari.buttons["URL"].tap()
    let url = safari.textFields["URL"].value as! String
    

    然后断言。

    【讨论】:

      【解决方案3】:

      能够在 iOS 13.5 中从 Safari 获取 URL

      override public func setUpWithError() throws {
              app = XCUIApplication()
              safari = XCUIApplication(bundleIdentifier: "com.apple.mobilesafari")
      
              app.launch()
              continueAfterFailure = false
          }
      
      func test_URL() throws {
              safari.launch()
              safari.buttons.matching(identifier: "URL").element(at: 0).value
          }
      

      【讨论】:

        【解决方案4】:

        为了让 Xcode 13 在 iOS 15 上运行,我使用了这段代码

        let safari = XCUIApplication(bundleIdentifier: "com.apple.mobilesafari")
        safari.textFields["Address"].tap()
        let val = safari.textFields["Address"].value as! String
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-12-04
          • 1970-01-01
          • 1970-01-01
          • 2012-03-28
          • 2014-01-03
          • 2016-09-23
          • 1970-01-01
          • 2011-03-04
          相关资源
          最近更新 更多