【问题标题】:Can not access the programatically added UIView in XCTest无法访问 XCTest 中以编程方式添加的 UIView
【发布时间】:2017-05-15 16:55:37
【问题描述】:

下面是我记录的 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

另外,我参考了以下 Apple 文档:https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/iPhoneAccessibility/Making_Application_Accessible/Making_Application_Accessible.html#//apple_ref/doc/uid/TP40008785-CH102-SW2

https://developer.apple.com/reference/uikit/uiview

但所有这些似乎都不能解决问题。任何帮助将不胜感激。

【问题讨论】:

  • 我不知道答案,但是如果您查看报告日志,它会告诉您有关失败原因的详细信息。它甚至可能包括一个屏幕截图,显示当时的界面。

标签: ios xcode swift3 xctest ui-testing


【解决方案1】:

有问题的线非常脆弱。每次应用启动时,您的应用的视图可能并不总是以相同的顺序可用,具体取决于竞争条件,因此录制会话中记录的内容不一定适用于应用的所有启动。您可能会发现代码实际上正在运行,但并未点击您期望的元素。

为使您的代码不那么脆弱,请在您的应用代码中向 UIView 添加一个可访问性标识符,并使用 otherElements 查询来查找 UIView。

// app code
let view: UIView!
view.accessibilityIdentifier = "myAccessibilityIdentifier"

// UI test code
app.otherElements["myAccessibilityIdentifier"].tap()

【讨论】:

  • 我也试过这个。它说:没有找到“myAccessibilityIdentifier”其他匹配项
  • 这行得通。实际上,在我的情况下,我想要访问的 UIView 的父视图将 isAccessibilityElement 设置为 YES。所以它之前没有工作。
猜你喜欢
  • 1970-01-01
  • 2010-10-14
  • 2013-04-08
  • 2019-07-25
  • 2012-10-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多