下面是我录制的XCTest
let app = XCUIApplication()
let tablesQuery = app.tables
tablesQuery.staticTexts["Video"].tap()
tablesQuery.staticTexts["\tWindowed"].tap()
app.buttons["Launch"].tap()
app.buttons["Popout Video"].tap()
app.children(matching: .window).element(boundBy: 0).children(matching: .other).element(boundBy: 1).tap()
当我尝试运行测试时,最后一部分是:
app.children(matching: .window).element(boundBy: 0).children(matching: .other).element(boundBy: 1).tap()
无法访问。它不会抛出任何错误,但最后一行代码不会执行。
我尝试通过引用以下 stackoverflow 问题来解决该问题: Xcode UI Tests can't find views that are added programatically
https://developer.apple.com/reference/uikit/uiview
但这一切似乎并不能解决问题。任何帮助将不胜感激。
最佳答案
有问题的线路非常脆弱。您的应用程序的 View 可能并不总是在每次应用程序启动时以相同的顺序提供,具体取决于竞争条件,因此记录 session 中记录的内容不一定适用于应用程序的所有启动。您可能会发现代码实际上正在运行,但没有点击您期望的元素。
为了降低代码的脆弱性,请在应用代码中向 UIView 添加辅助功能标识符,并使用 otherElements
查询来查找 UIView。
// app code
let view: UIView!
view.accessibilityIdentifier = "myAccessibilityIdentifier"
// UI test code
app.otherElements["myAccessibilityIdentifier"].tap()
关于ios - 无法访问XCTest中以编程方式添加的UIView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41412339/