代码之家  ›  专栏  ›  技术社区  ›  Mladen

如何在iOS用户界面测试中查询“主窗口”?

  •  1
  • Mladen  · 技术社区  · 8 年前

    在我的测试中,有一点我需要与内部视图交互 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}}
        }
    

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

    全部打印 XCUIElementAttributes (就像 title ,请 value ,请 identifier 等等)没有给我太多信息(主要是空字符串)。另外,我不想按职位查询(如: app.windows.element(boundBy: 3) )因为我不确定这个窗口是否总是在这个位置。

    有其他查询方法吗 主窗口 是吗?

    2 回复  |  直到 8 年前
        1
  •  0
  •   osrl    8 年前

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

        2
  •  0
  •   Mladen    8 年前

    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}}
        }
    

    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'
        }