WWDC Notes

UI Testing in Xcode

Show Apple's description.
Xcode 7 introduces new UI testing features fully integrated into the IDE. Learn about the new APIs and how UI testing fits in with existing testing features in Xcode. See how to get started by recording your app, and how to efficiently craft and maintain UI tests.

Three classes

  • XCUIApplication
    • Proxy of the tested application, it launches the app (and kill any pre-existing instances of the app).
    • You use this to query for XCUI Elements
  • XCUIElement
    • Proxy for UI elements in the app. They have types (buttons, labels, ...) and identifiers (“new”, “add”, “delete”, ...).
    • You can simulate events (like taps) with these elements.
    • Every query to get a element must resolve to exactly one match (multiple or no matches will cause a failure)
  • XCUIElementQuery
    • When querying, you can get elements via subscripting, via index, and if you know it’s just a one result, use unique.

UIRecorder

  • You can create a test just by hitting the record button, then interact in the app simulator. Every action is recorded and automatically added in the test for you.
  • What you’re validating this way is that the UI elements you’re interacting with exist during the test.
  • If you want to validate the states of different elements (like if a button has been tapped), you need to add more logic in the test yourself.
  • You can even set a breakpoint in one test, hit the record button and add interactions to an already existing test.
  • After running the tests, you can see the report to actually see each test step by step. You can even get a screenshot of that step in the reporter:

Accessibility Inspector

After running this Xcode tool, you can press ⌘ + F7 to visualize in the simulator what element is under the cursor:

Missing anything? Corrections? Contributions are welcome 😃

Related

Written by

zntfdr

Federico Zanetello

Federico Zanetello is an iOS Engineer with strong passion for Swift, minimalism, and design. When he’s not busy automating things, he can be found writing at fivestars.blog and/or playing with the latest shiny toys.