Xcode UITesting:标签从层次结构中消失,不会再回来

时间:2016-02-24 00:55:15

标签: ios swift xcode-ui-testing

这是使用Xcode 7.2.1和Swift 2.0。

我有一个包含UILabel的表格单元格,用于显示错误消息。因此,在初始加载时,使用以下代码将其消隐:

cell.errorLabel.alpha = 0.0
cell.errorLabel.text = nil

然后,当我发现我要显示的错误时,我会这样做:

cell.label.text = "to"
cell.errorLabel.alpha = 1.0
cell.errorLabel.text = "Error !!!!"

这在运行应用程序时工作正常。但是,当在UI测试中运行时,我尝试测试错误是否显示如下:

let toCell = XCUIApplication().tables.... // Finds the cell with label 'to'
XCTAssertTrue(toCell.staticTexts["Error !!!!"].exists)

它失败了。我已通过检查另一个('至')标签是否已经确认我获得了正确的单元格。它是什么。但UI测试不会看到错误标签。我通过添加断点并使用如下调试器来测试它:

(lldb) p toCell.staticTexts.count
    t =   427.03s     Get number of matches for: Descendants matching type StaticText
    t =   427.29s         Snapshot accessibility hierarchy for enterprise.com.me
    t =   427.31s         Find: Descendants matching type Table
(UInt) $R2 = 1
    t =   427.31s         Find: Descendants matching type Cell
    t =   427.32s         Find: Elements containing elements matching type StaticText with identifier 'to'
    t =   427.32s         Find: Descendants matching type StaticText

(UInt) $R2 = 1表示存在一个静态文本。然而,看着模拟器,我可以清楚地看到两个UILabel。

我已经单独尝试了很多事情来隔离问题 - 只使用alpha或将文字设置为nil,或使用UIView的hidden属性。使用这些选项中的任何一个来初始隐藏标签,使其在以后变得可见时对UI测试不可见,无论我尝试什么。

我对此非常困惑,我怀疑这是一个错误。任何人都有任何想法如何让UI测试看到UILabel一旦我看到它?

P.S。我还尝试使用等待循环等待标签显示(使用expectationForPredicate(...),但UILabel未显示。

1 个答案:

答案 0 :(得分:1)

问题原因是我没有在单元格上设置accessibilityElements属性。因此,可访问性试图找出每个内容的问题。

因此,如果要为表视图构建自定义单元格,请确保设置accessibilityElements属性,以便测试可以找到单元格的内容。