Beginning iOS Unit and UI Testing - Part 5: UI | Ray Wenderlich

In this video tutorial on unit and UI testing in iOS, you’ll learn how to automate taps, swipes and more to test your app's user-facing functionality.


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/3573-beginning-ios-unit-and-ui-testing/lessons/5

EDIT:

We’ve fixed the subtitles! :]


There’s an inaccuracy in the subtitles on this video:

Around 4:16, talking about XCUIApplication, I say

“This is that proxy object we talked about. It is not actually the UIApplication. It’s NOT a singleton, but rather, a proxy that represents it.”

The “NOT a singleton” was omitted from the subtitles…

You can create multiple instances of XCUIApplication (you usually shouldn’t need to, however), and each will be a separate, unique instance (NOT a singleton).

Joshua, if you app is localized, these tests won’t work, how would you do ?

thanks

Hi, Joshua! Thanks for the series.

Has something changed in Xcode 9 maybe, but it’s weird I’ve always got my test failed in following assertion:

let aboutTitleText = app.navigationBars.staticTexts["About"]
XCTAssertTrue(aboutTitleText.exists, "Should be on the About screen")

But screenshots inside test navigator states that “About” title of navigation bar on screen indeed.

Any thoughts?

@jrg.developer Do you have any feedback on this? Thank you - much appreciated! :]

@giguerea According to Apple documentation, when you subscript XCUIElementQuery lookup happens against element’s accessibility identifier, so you can perfectly have localized title of a label for example, but leave accessibility identifier not localized.

Using UI Test Recorder figured out that “About” title of navigation bar can be accessed as otherElement. Next code worked for me.

let aboutTitleText = app.navigationBars.otherElements["About"]
XCTAssertTrue(aboutTitleText.exists, "Should be on the About screen")

Awesome tip! Thanks for sharing it. :]

It seems each new major version of Xcode brings bugs, and UI Test Navigation definitely is still being worked on…

FYI – another awesome RW author is working on updating this series for Xcode 9! There’s not a definite release date, but you can track the progress of it and all other upcoming videos here:

https://videos.raywenderlich.com/schedule

Localizations are a bit trickier… In advance, you need to decide what are you trying to accomplish with UI testing localizations:

(1) That the elements exist on screen, regardless of language?

You can use accessibilityIdentifier to lookup elements instead of checking for the text value directly.

(2) That the text is exactly correct for each language, and/or to generate screenshots?

You can create dedicated scheme(s) for UI testing certain language(s)… admittedly, if you have a lot of languages, this can be a lot of work… however, it can still be less work than having to repeatedly test each language manually.

It also seems like it may be possible to change the language at runtime, but personally, I haven’t tried this.

For more details on how to do each of the above, check out this StackOverflow post and answers:

What if the element is not meant to be accessible?
How can we access such elements? Are there any other ways? I am struggling with this issue for a long time now.

Even for elements that aren’t mean to be interacted by the user, you can still set accessibilityIdentifier on them and use such to find them in UI tests. :]