How to find staticText properly with XCTest
Asked Answered
V

4

6

I am implementing UITests for my iOS app. So far, I've been able to do some simple testing, but I've come to a tableView where there are two sections. Each section has a sectionHeaderView containing static text, eg. "SECTION 1" and "SECTION 2", in normal sectionHeader style.

When performing app.tables.staticTexts["SECTION 1"].exists it returns true. This is the first section, visible at the very top when the view loads.

When performing the same, but for "SECTION 2", it returns false. The sectionHeaderView for this section is outside the view at this point, so I thought this was the problem, but it turns out it's not.. I tried app.swipeUp(), which successfully brings the second section into the screen. After the swipeUp i sleep for a few seconds for the view to settle, and I perform the same check, but it simply cannot find the second sectionView.

After scrolling down, I have tried to print out app.tables.staticTexts.debugDescription to see what it can find, and it only shows the first section as well as a tableFooterView I have at the very bottom of the tableView.

At the time I perform app.tables.staticTexts["SECTION 2"].exists I can literally see the "SECTION 2" text on the simulator. Yet it does not exist to the test.

Why is my second sectionHeaderView completely invisible to XCTest? Could it be that I have disabled some sort of accessibility-variable on this view in particular? I can't find anything..

Edit, output:

    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
Valladares answered 12/1, 2017 at 13:12 Comment(5)
When you look for the second section header, does the output say it's using a cached snapshot/hierarchy?Microphyte
@Microphyte No.. See updated question for what it says.Valladares
Did you resolve this issue? I am also facing this, my sectionheader info completely vanishes after I scrollDeplete
@Deplete No, I gave up. It's a year and a half ago though, so maybe there are other ways to fix this now. Let me know if you find a solution.Valladares
I tried it in 11.4 creating simple tableview app, using identifiers properly, once a section goes out of bounds and then re-appears all the accessibility traits are lost. This is observed when we use customheader views . One workaround I used was to add identifier directly to the UIView of custom header than the label inside which seem to be retained....but still wanted to know the root cause, I think its an issue with XCTestDeplete
P
7

Place a breakpoint in your code at:

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

When you hit the breakpoint type this into the debug panel and hit enter:

po print(XCUIApplication().debugDescription)

This will list out everything that is available to XCUITest. Look for your Section 2 text in there. Often times when this happens to me, I spelled it wrong or the text in the app has an extra space somewhere. When using .staticText it has to match EXACTLY.

Phenolphthalein answered 17/1, 2017 at 16:5 Comment(0)
K
1

I ran into this problem for table footers. It seems like they are treated as "other" objects, not staticTexts so the following code should work:

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

Thanks to h.w.powers for the debugging tip:

po print(XCUIApplication().debugDescription)
Kaete answered 11/8, 2017 at 20:35 Comment(0)
A
0

A couple of questions, since I can't comment yet: (I've been doing a LOT of UI testing recently, so I'm learning a little)

What happens when you try to assert app.tables.staticTexts["SECTION 2"].exists after swiping up, and disregarding the "SECTION 1" label?

Is sectionHeaderView a custom subclass?

I've found it's helpful to assign specific views an accessibilityIdentifier and then access them via the XCUIElement proxy with that identifier.

Also, something I've found recently is that unless you're using UIAccessibilityContainer, referring to accessibility traits of superviews can negate the accessibility traits of its subviews.

Ambagious answered 12/1, 2017 at 17:38 Comment(0)
S
0

Just put the debugger in the test function, run the test

enter image description here

and in debugger screen just write po app it will show the view hierarchy of your app containing images , static text etc.

Scheldt answered 5/12, 2019 at 6:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.