Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find staticText properly with XCTest

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
like image 324
Sti Avatar asked Jan 12 '17 13:01

Sti


2 Answers

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.

like image 181
h.w.powers Avatar answered Nov 20 '22 08:11

h.w.powers


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)
like image 1
David Weiss Avatar answered Nov 20 '22 08:11

David Weiss