gpt4 book ai didi

ios - Xcode UI测试 : label disappears from hierarchy and won't come back

转载 作者:搜寻专家 更新时间:2023-10-31 23:00:54 24 4
gpt4 key购买 nike

这是使用 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)

它失败了。我已经通过检查是否存在其他(“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 表示存在一个静态文本。但是看着模拟器我可以清楚地看到两个 UILabel。

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

我对此很困惑,我怀疑这是一个错误。任何人都知道如何让 UI 测试在我使它可见后看到 UILabel?

附言我也尝试过使用等待循环等待标 checkout 现(使用 expectationForPredicate(...),但是 UILabel 没有出现。

最佳答案

问题原来是我没有在单元格上设置 accessibilityElements 属性。因此,可访问性在试图弄清楚每个文件中的内容时遇到了问题。

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

关于ios - Xcode UI测试 : label disappears from hierarchy and won't come back,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35591293/

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