【问题标题】:How to find staticText properly with XCTest如何使用 XCTest 正确查找 staticText
【发布时间】:2017-05-27 14:49:18
【问题描述】:

我正在为我的 iOS 应用程序实现 UITests。 到目前为止,我已经能够进行一些简单的测试,但是我来到了一个 tableView,其中有两个部分。每个部分都有一个 sectionHeaderView 包含静态文本,例如。 “SECTION 1”和“SECTION 2”,采用普通 sectionHeader 样式。

当执行app.tables.staticTexts["SECTION 1"].exists 时,它返回true。这是第一部分,当视图加载时在最顶部可见。

执行相同操作时,但对于“SECTION 2”,它返回false。此部分的 sectionHeaderView 此时在视图之外,所以我认为这是问题所在,但事实证明不是.. 我尝试了app.swipeUp(),它成功地将第二部分带入了屏幕。在 swipeUp 之后,我睡了几秒钟让视图稳定下来,然后我执行了相同的检查,但它根本找不到第二个 sectionView。

向下滚动后,我尝试打印出 app.tables.staticTexts.debugDescription 以查看它可以找到什么,它只显示第一部分以及我在最底部的 tableFooterView表格视图。

当我执行app.tables.staticTexts["SECTION 2"].exists 时,我可以在模拟器上看到“SECTION 2”文本。然而它不存在于测试中。

为什么我的第二个 sectionHeaderView 对 XCTest 完全不可见?是不是我特别在这个视图上禁用了某种可访问性变量?我什么都找不到。。

编辑,输出:

    t =    32.25s     Find: Descendants matching type Table
    t =    32.26s     Find: Descendants matching type StaticText
    t =    32.26s     Find: Elements matching predicate '"SECTION 1" IN identifiers'
Found SECTION 1. Will scroll down to find Section 2.
    t =    32.26s     Swipe up Target Application 0x6080000bbf60
    t =    32.26s         Wait for app to idle
    t =    32.30s         Find the Target Application 0x6080000bbf60
    t =    32.30s             Snapshot accessibility hierarchy for my.bundle.identifier
    t =    33.09s             Wait for app to idle
    t =    33.14s         Synthesize event
    t =    33.42s         Wait for app to idle
Slept for 3 seconds. Have scrolled down. SECTION 2 in view now.
    t =    38.86s     Snapshot accessibility hierarchy for my.bundle.identifier
    t =    39.64s     Find: Descendants matching type Table
    t =    39.65s     Find: Descendants matching type StaticText
    t =    39.65s     Find: Elements matching predicate '"SECTION 2" IN identifiers'
    t =    39.66s     Assertion Failure: MyUITests.swift:347: XCTAssertTrue failed - SECTION 2 does not exist
    t =    39.66s     Tear Down

【问题讨论】:

  • 当您查找第二部分标题时,输出是否说它正在使用缓存的快照/层次结构?
  • @Oletha No.。请参阅更新后的问题以了解其内容。
  • 您解决了这个问题吗?我也面临这个问题,滚动后我的sectionheader信息完全消失了
  • @anoop4real 不,我放弃了。不过,这是一年半前的事了,所以现在也许还有其他方法可以解决这个问题。如果您找到解决方案,请告诉我。
  • 我在 11.4 中尝试过创建简单的 tableview 应用程序,正确使用标识符,一旦某个部分超出范围,然后重新出现,所有可访问性特征都会丢失。当我们使用 customheader 视图时会观察到这一点。我使用的一种解决方法是将标识符直接添加到自定义标题的 UIView 而不是其中似乎保留的标签....但仍然想知道根本原因,我认为这是 XCTest 的问题

标签: ios ui-automation xctest xcode-ui-testing


【解决方案1】:

在代码中放置断点:

app.tables.staticTexts["SECTION 2"].exists

当你点击断点时,在调试面板中输入这个并回车:

po print(XCUIApplication().debugDescription)

这将列出 XCUITest 可用的所有内容。在那里查找您的第 2 节文本。经常发生这种情况时,我拼写错误或应用程序中的文本在某处有多余的空间。当使用.staticText 时,它必须完全匹配。

【讨论】:

    【解决方案2】:

    我在表格页脚中遇到了这个问题。似乎它们被视为“其他”对象,而不是 staticTexts,因此以下代码应该可以工作:

    XCTAssert(app.otherElements["SECTION 2"].exists)
    

    感谢 h.w.powers 的调试提示:

    po print(XCUIApplication().debugDescription)
    

    【讨论】:

      【解决方案3】:

      几个问题,因为我还不能发表评论:(我最近做了很多 UI 测试,所以我正在学习一点)

      当您在向上滑动并忽略“SECTION 1”标签后尝试断言 app.tables.staticTexts["SECTION 2"].exists 时会发生什么?

      sectionHeaderView 是自定义子类吗?

      我发现为特定视图分配一个 accessibilityIdentifier 然后使用该标识符通过 XCUIElement 代理访问它们很有帮助。

      另外,我最近发现,除非您使用 UIAccessibilityContainer,否则引用父视图的可访问性特征可以否定其子视图的可访问性特征。

      【讨论】:

        【解决方案4】:

        只需将调试器放在测试函数中,运行测试

        在调试器屏幕中只需写入po app,它将显示您的应用的视图层次结构,其中包含图像、静态文本等。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-04-04
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-04-04
          • 1970-01-01
          相关资源
          最近更新 更多