Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 如何查询";“主窗口”;在iOS用户界面测试中?_Swift_Xcode Ui Testing_Ui Testing_Xcuitest - Fatal编程技术网

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

Swift 如何查询";“主窗口”;在iOS用户界面测试中?,swift,xcode-ui-testing,ui-testing,xcuitest,Swift,Xcode Ui Testing,Ui Testing,Xcuitest,在测试中,我需要与主窗口中的视图进行交互。当我执行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

在测试中,我需要与
主窗口中的视图进行交互。当我执行
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
不是窗口视图的标识符

打印所有内容(如
标题
标识符
等等)并没有给我多少信息(它们主要是空字符串)。另外,我不想按位置查询它(比如:
app.windows.element(boundBy:3)
),因为我不确定这个窗口是否总是在这个位置


还有其他方法可以查询主窗口吗?

我找到的最好的方法是使用窗口的框架大小过滤窗口

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

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

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

我一直在错误的地方得到标识符:

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[“主窗口”]
,并确保我的目标是真正的
主窗口

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

你找到解决办法了吗?@osrl没有,还没有,对不起。这个办法行不通。在这种情况下(由于列表中所有4个窗口的宽度始终大于10;您可以在日志中看到),这与编写此
app.windows.element(boundBy:0)
基本相同。我想避免使用这种方法。在这种情况下工作的是这种情况(因为
主窗口的原点是
{0.0,0.5}
):
元素.frame.origin.y>0
。依靠索引查找窗口元素,我仍然没有遇到任何问题,所以我的解决方案可以保持不变。是的,你是对的。我的窗口大小为0x0或2x2。这对我来说很有效。我知道这不是一个好办法
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'
    }