如何使用XCTest正确地找到staticText

Sti*_*Sti 4 ui-automation ios xctest xcode-ui-testing

我正在为我的iOS应用程序实现UITests.到目前为止,我已经能够进行一些简单的测试,但是我来到一个tableView,其中有两个部分.每个部分都有一个包含静态文本的sectionHeaderView,例如."第1节"和"第2节",正常部分为头部样式.

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

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

向下滚动后,我试图打印出来app.tables.staticTexts.debugDescription,看看它可以找到,而且它仅说明了第一部分,以及一个tableFooterView我在的tableView的最底部.

在我执行的时候,我app.tables.staticTexts["SECTION 2"].exists可以在模拟器上逐字地看到"第2节"文本.但它并不存在于测试中.

为什么我的第二部分HeaderView对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
Run Code Online (Sandbox Code Playgroud)

h.w*_*ers 5

在代码中放置一个断点:

app.tables.staticTexts["SECTION 2"].exists
Run Code Online (Sandbox Code Playgroud)

当你点击断点时,在调试面板中键入它并按下回车:

po print(XCUIApplication().debugDescription)
Run Code Online (Sandbox Code Playgroud)

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


Ari*_*lSD 0

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

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

sectionHeaderView自定义子类吗?

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

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