gpt4 book ai didi

swift - 如何在 iOS UI 测试中查询 "Main Window"?

转载 作者:行者123 更新时间:2023-11-28 07:45:22 24 4
gpt4 key购买 nike

在我的测试中,有一次我需要与 Main Window 中的 View 进行交互。当我执行 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 因为我在这个列表的第一个窗口中有几乎相同的 View ,所以我想将它们分开。因此,我尝试使用 app.windows["Main Window"] 查询它,但似乎 Main Window 不是窗口 View 的标识符。

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

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

最佳答案

我试图实现的是为窗口对象添加一个标识符。我尝试将它添加到显示 View 的 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'
}

关于swift - 如何在 iOS UI 测试中查询 "Main Window"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51190708/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com