gpt4 book ai didi

ios - 如何使用 XCTest 正确查找 staticText

转载 作者:可可西里 更新时间:2023-11-01 05:25:27 42 4
gpt4 key购买 nike

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

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

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

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

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

为什么我的第二个 sectionHeaderView 对 XCTest 完全不可见?会不会是我在这个 View 上特别禁用了某种可访问性变量?我找不到任何东西..

编辑,输出:

    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

最佳答案

在您的代码中放置一个断点:

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

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

po print(XCUIApplication().debugDescription)

这将列出 XCUITest 可用的所有内容。在那里寻找您的第 2 节文本。很多时候,当我遇到这种情况时,我拼错了,或者应用程序中的文本在某处有额外的空间。使用 .staticText 时,它必须完全匹配。

关于ios - 如何使用 XCTest 正确查找 staticText,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41614147/

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