Xcode UITesting:标签从层次结构中消失并且不会回来
Xcode UITesting: label disappears from hierarchy and won't come back
这是使用 Xcode 7.2.1 和 Swift 2.0。
我有一个包含 UILabel
的 table 单元格,用于显示错误消息。所以在初始加载时使用如下代码将其消隐:
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)
它失败了。我已经通过检查另一个 ('to') 标签是否存在来验证我得到了正确的单元格。它是什么。但是 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
表示存在一个静态文本。但是看着模拟器我可以清楚地看到两个 UILabels.
我单独尝试了很多方法来找出问题 - 仅使用 alpha
或将文本设置为 nil
,或使用 UIView 的 hidden
属性。使用这些选项中的任何一个来最初隐藏标签会使它对 UI 测试不可见,当后来变得可见时,无论我尝试什么。
我对此很困惑,我怀疑这是一个错误。任何人都知道如何进行 UI 测试以在 UI 标签可见后看到它?
P.S。我也试过使用等待循环等待标签出现(使用 expectationForPredicate(...)
,但 UILabel
没有出现。
问题原来是我没有在单元格上设置accessibilityElements
属性。因此,可访问性在试图弄清楚每个文件中的内容时遇到了问题。
因此,如果您要为 table 视图构建自定义单元格,请确保设置 accessibilityElements
属性 以便测试可以找到单元格的内容。
这是使用 Xcode 7.2.1 和 Swift 2.0。
我有一个包含 UILabel
的 table 单元格,用于显示错误消息。所以在初始加载时使用如下代码将其消隐:
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)
它失败了。我已经通过检查另一个 ('to') 标签是否存在来验证我得到了正确的单元格。它是什么。但是 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
表示存在一个静态文本。但是看着模拟器我可以清楚地看到两个 UILabels.
我单独尝试了很多方法来找出问题 - 仅使用 alpha
或将文本设置为 nil
,或使用 UIView 的 hidden
属性。使用这些选项中的任何一个来最初隐藏标签会使它对 UI 测试不可见,当后来变得可见时,无论我尝试什么。
我对此很困惑,我怀疑这是一个错误。任何人都知道如何进行 UI 测试以在 UI 标签可见后看到它?
P.S。我也试过使用等待循环等待标签出现(使用 expectationForPredicate(...)
,但 UILabel
没有出现。
问题原来是我没有在单元格上设置accessibilityElements
属性。因此,可访问性在试图弄清楚每个文件中的内容时遇到了问题。
因此,如果您要为 table 视图构建自定义单元格,请确保设置 accessibilityElements
属性 以便测试可以找到单元格的内容。