在我的测试中,我需要与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(如title
、value
、identifier
等(并没有给我太多信息(它们主要是空字符串(。另外,我不想按位置查询它(例如:app.windows.element(boundBy: 3)
(,因为我不确定此窗口是否将始终处于此位置。
有没有其他方法可以查询Main Window
?
我试图实现的是将标识符放在窗口对象上。我尝试将其添加到视图控制器中,该视图显示我尝试使用此代码行查询的视图(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'
}
我发现最好的方法是使用框架大小过滤窗口。
let window = app.windows.allElementsBoundByIndex.first { element in
element.frame.width > 10
}!
如果我找到更好的方法,我会编辑它。