【问题标题】:How to query "Main Window" in iOS UI tests?如何在 iOS UI 测试中查询“主窗口”?
【发布时间】:2018-07-05 11:53:59
【问题描述】:

在我的测试中,我需要与Main Window 中的视图进行交互。当我执行po app.windows 时,我得到了这个:

Find: Target Application 0x1c40d7680
  Output: {
    Application, 0x1c4389170, pid: 4765, {{0.0, 0.0}, {375.0, 667.0}}, label: 'Mercedes PRO [testb2b]'
  }
  ↪︎Find: Descendants matching type Window
    Output: {
      Window, 0x1c43890a0, {{0.0, 0.0}, {375.0, 667.0}}
      Window, 0x1c438dc30, {{0.0, 0.0}, {375.0, 667.0}}
      Window, 0x1c438dea0, {{0.0, 0.0}, {375.0, 667.0}}
      Window, 0x1c438e6c0, Main Window, {{0.0, 0.5}, {375.0, 667.0}}
    }

我需要查询Main Window,因为我在这个列表的第一个窗口中拥有几乎相同的视图,所以我想将它们分开。所以,我尝试用app.windows["Main Window"] 查询它,但似乎Main Window 不是窗口视图的标识符。

打印所有XCUIElementAttributes(如titlevalueidentifier 等)并没有给我太多信息(它们主要是空字符串)。另外,我不想按位置查询它(像这样:app.windows.element(boundBy: 3)),因为我不确定这个窗口是否会一直在这个位置。

还有其他方法可以查询Main Window吗?

【问题讨论】:

  • 你找到解决办法了吗?
  • @osrl 不,还没有,抱歉。

标签: swift xcode-ui-testing ui-testing xcuitest


【解决方案1】:

我试图实现的是将标识符放入窗口对象。我尝试将它添加到 ViewController 中,显示我试图用这行代码查询的视图(在 viewDidLoad() 中):

UIApplication.shared.delegate?.window??.accessibilityIdentifier = "Main Window"

而且我一直在错误的地方获取标识符:

Find: Target Application 0x60c0000c1260
  Output: {
    Application, 0x60c00018c640, pid: 33496, {{0.0, 0.0}, {414.0, 736.0}}, label: '[testb2b]'
  }
  ↪︎Find: Descendants matching type Window
    Output: {
      Window, 0x60c00018c710, {{0.0, 0.0}, {414.0, 736.0}}, identifier: 'Main Window'
      Window, 0x60c000191370, {{0.0, 0.0}, {414.0, 736.0}}
      Window, 0x60c0001915e0, {{0.0, 0.0}, {414.0, 736.0}}
      Window, 0x60c000191c60, Main Window, {{0.0, 0.5}, {414.0, 736.0}}
    }

然后我在代码中找到了一个地方,开发人员在其中创建了一个新窗口,用于保存该 VC。

let actionVC = UIStoryboard.findViewController(withIdentifier: "ActionViewController")
if let appWindow = UIApplication.shared.delegate?.window {
     let window = UIWindow.init(frame: appWindow.frame)
     window.rootViewController = actionVC
     window.accessibilityIdentifier = "Main Window"
}

这让我可以编写这样的查询:app.windows["Main Window"],并确保我的目标是真正的Main Window

Find: Target Application 0x60c0000c1260
  Output: {
    Application, 0x60c00018c640, pid: 33496, {{0.0, 0.0}, {414.0, 736.0}}, label: '[testb2b]'
  }
  ↪︎Find: Descendants matching type Window
    Output: {
      Window, 0x60c00018c710, {{0.0, 0.0}, {414.0, 736.0}}
      Window, 0x60c000191370, {{0.0, 0.0}, {414.0, 736.0}}
      Window, 0x60c0001915e0, {{0.0, 0.0}, {414.0, 736.0}}
      Window, 0x60c000191c60, Main Window, {{0.0, 0.5}, {414.0, 736.0}}, identifier: 'Main Window'
    }

【讨论】:

    【解决方案2】:

    我发现最好的方法是使用框架大小过滤窗口。

    let window = app.windows.allElementsBoundByIndex.first { element in
                    element.frame.width > 10
                }!
    

    如果我找到更好的方法,我会编辑它。

    【讨论】:

    • 此解决方案不起作用。在这种情况下(由于列表中所有 4 个窗口的宽度始终大于 10;您可以在日志中看到它),这与编写此 app.windows.element(boundBy: 0) 基本相同。我想避免使用这种方法。在这种情况下起作用的是这种情况(因为Main Window的起源是{0.0, 0.5}):element.frame.origin.y > 0。我仍然没有依赖按索引查找窗口元素的问题,所以我可能会保持我的解决方案不变。
    • 是的,你是对的。我的窗口大小是 0x0 或 2x2。所以这对我有用。我知道这不是一个好的解决方案
    猜你喜欢
    • 1970-01-01
    • 2011-04-14
    • 2021-01-01
    • 1970-01-01
    • 2015-07-22
    • 1970-01-01
    • 1970-01-01
    • 2012-01-01
    • 1970-01-01
    相关资源
    最近更新 更多