Testing in iOS - Part 28: Challenge: About Screen | Ray Wenderlich

Challenge: About Screen Test Your next challenge is to write a test the about screen view controller.


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/3530-testing-in-ios/lessons/28

Hi. I am seeing differences between testing the About controller in iOS 11.1 vs 11.2. In 11.1 “Not Now” shows up under staticTexts query. In 11.2 it shows up under buttons query. Can you please provide a possible solution for how to make this UI test work in both versions? Are other people seeing this difference?

You may have the simulator at a different orientation. Try switching the orientations and see what happens. Definitely let me know if you continue to run into the same issue.

@bdmoakley Here is the output of staticTexts query in 11.1

:arrow_right_hook:︎Find: Descendants matching type StaticText
Output: {
StaticText, 0x60400018e790, traits: 8589934656, {{128.3, 322.0}, {157.3, 20.3}}, label: ‘Enjoying DogYears?’
StaticText, 0x60400018e860, traits: 8589934656, {{117.0, 344.3}, {180.0, 36.0}}, label: ‘Tap a star to rate it on the App Store.’
StaticText, 0x60400018fb10, traits: 8589934656, {{72.0, 457.0}, {270.0, 20.3}}, label: ‘Not Now’
}

And here is the same staticTexts query in 11.2

:arrow_right_hook:︎Find: Descendants matching type StaticText
Output: {
StaticText, 0x608000188060, traits: 8589934656, {{128.3, 322.0}, {157.3, 20.3}}, label: ‘Enjoying DogYears?’
StaticText, 0x608000188130, traits: 8589934656, {{117.0, 344.3}, {180.0, 36.0}}, label: ‘Tap a star to rate it on the App Store.’
}

And here is the buttons query in 11.2

:arrow_right_hook:︎Find: Descendants matching type Button
Output: {
Button, 0x6080001893e0, traits: 8589934657, {{72.0, 457.0}, {270.0, 20.3}}, label: ‘Not Now’
}

I am using iPhone 8 Plus simulator in portrait mode in both cases. So the test passes in 11.1 but fails in 11.2. Do you have any insights into what is going on?

Thanks!

Any thoughts on this one, @fahim?

The element type for the “Not Now” button has indeed changed between iOS 11.1 and 11.2 though there seems to be no mention of this in the documentation - at least not a mention that I could find with a cursory search.

Having the button as a label/static text did not make much sense in the first place. So the change does make sense. And personally, I would just modify the test for iOS 11.2 since going forward, I think that it will remain a button and you will most probably be testing on the simulator for the latest version of iOS.

However, if you do test for older versions of iOS as well (for example, for bugs under previous versions), then you could always add a version check and proceed accordingly, something like this:

if #available(iOS 11.2, *) {
	element.buttons["Not Now"].tap()
} else {
	element.staticTexts["Not Now"].tap()
}
1 Like

Thank you for your assistance.

Hi, I’m having trouble with this.

   func testRateThisApp() {
        
        let tablesQuery = app.tables
        tablesQuery.staticTexts["About"].tap()
        tablesQuery.buttons["Rate Us On the App Store"].tap()
        let element = app.children(matching: .other).element(boundBy: 0)
        
        let pred = NSPredicate(format: "exists == true")
        let exp = expectation(for: pred, evaluatedWith: element, handler: nil)
        let res = XCTWaiter.wait(for: [exp], timeout: 5.0)
        XCTAssert(res == XCTWaiter.Result.completed, "Failed time out waiting for rate dialog")
        
        let title = element.staticTexts["Enjoying DogYears?"]
        XCTAssert(title.exists, "Enjoying DogYears dialog did not show!")
        element.buttons["Not Now"].tap()
        XCTAssert(title.exists, "Enjoying DogYears dialog did not go away!")
    }

I get the error message:
“XCTAssertTrue failed - Enjoying DogYears dialog did not go away!”
Without any other context. Could you please explain to me why? Thanks

@bdmoakley Can you please help with this when you get a chance? Thank you - much appreciated! :]

Hi, I am having an issue when running the testAboutRate() function on Xcode 11.1 on iPad Air (3rd Gen) simulator.

func testAboutRate() {
  tablesQuery.staticTexts["About"].tap()
  tablesQuery.buttons["Rate Us On the App Store"].tap()
  let element = app.children(matching: .other).element(boundBy: 0)
  
  let pred = NSPredicate(format: "exists == true")
  let exp = expectation(for: pred, evaluatedWith: element, handler: nil)
  let res = XCTWaiter.wait(for: [exp], timeout: 5.0)
  XCTAssert(res == XCTWaiter.Result.completed, "Failed time out waiting for rate dialog")
  
  let title = element.staticTexts["Enjoying DogYears?"]
  XCTAssert(title.exists, "Enjoying DogYears dialog did not show!")
  element.buttons["Not Now"].tap()
  XCTAssert(title.exists, "Enjoying DogYears dialog did not go away!")
}

From the source code above, the XCTWaiter keeps returning timeout as shown in the picture below


According to the log message the test seem to not be able to search for the Enjoying DogYears dialog. Even though it has already appeared on the simulator.

t =     6.69s     Checking existence of `Other`
t =     6.81s         Requesting snapshot of accessibility hierarchy for app with pid 24127
t =     6.94s         Find: Children matching type Other
t =     6.95s     Capturing element debug description
t =     7.98s Checking `Expect predicate `exists == 1` for object Other`
t =     7.98s     Checking existence of `Other`
t =     8.08s         Requesting snapshot of accessibility hierarchy for app with pid 24127
t =     8.21s         Find: Children matching type Other
t =     8.21s     Capturing element debug description
t =     9.23s Checking `Expect predicate `exists == 1` for object Other`
t =     9.23s     Checking existence of `Other`
t =     9.34s         Requesting snapshot of accessibility hierarchy for app with pid 24127
t =     9.46s         Find: Children matching type Other
t =     9.46s     Capturing element debug description
t =    10.49s Checking `Expect predicate `exists == 1` for object Other`
t =    10.49s     Checking existence of `Other`
t =    10.60s         Requesting snapshot of accessibility hierarchy for app with pid 24127
t =    10.72s         Find: Children matching type Other
t =    10.72s     Capturing element debug description
t =    10.73s Assertion Failure: DogYearsUITests.swift:103: XCTAssertTrue failed - Failed time out waiting for rate dialog
t =    10.81s Tear Down
Test Case '-[DogYearsUITests.DogYearsUITests testAboutRate]' failed (10.809 seconds).

So I am not sure what’s the proper way to fix this issue?

@bdmoakley Do you have any feedback about this? Thank you - much appreciated! :]