Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Xcode 7 UI Testing - Xcake Dublin, October 2015

Xcode 7 UI Testing - Xcake Dublin, October 2015

What is UI Testing?
How does it work with Xcode 7?
What is XCTest?
Comparison with Calabash

Roland Gropmair

October 13, 2015
Tweet

More Decks by Roland Gropmair

Other Decks in Technology

Transcript

  1. UI Testing • Find UI elements • Interact with the

    app the same way as user does • Validate changes through assertions
  2. XCTest • Xcode’s testing framework • Apple introduced it in

    Xcode 5 - keeps adding: • Unit Tests • Performance Tests • UI Tests • Supports Objective-C and Swift
  3. How does it work? • New target type • Executed

    in separate process • New: • XCUIApplication: proxy for tested app • XCUIElement: proxy for elements • XCUIElementQuery: resolves to collections of elements
  4. Demo • Using Shwopping http://shwopping.herokuapp.com • Test target for UI

    Test • Recording (new test; but also enhance existing test) • Manually add assertions • Debugging: • print how query gets resolved: • p print(XCUIApplication().tables.cells.textFields.debugDescription) • Accessibility Inspector ⌘F7: highlight button, … • Screenshot - quick look • Code coverage
  5. Queries - Filtering • Filter by Element type (button, table,

    …) • Filter by Identifier (label, title, …) • Predicates (value, partial matching, …) • Remember: Queries can be chained
  6. Queries - Details • Find your UI elements by combining

    Relationships and Filtering • Use .element property to get XCUIElement • Elements must be unique (use .exist to check whether elements exists) • Queries are evaluated on demand; will be re- evaluated when UI changes
  7. Example let app = XCUIApplication() let table = app.tables["Shopping List"]

    app.launch() XCTAssert(table.cells.count == 4, "wrong number of cells") table.cells.elementBoundByIndex(0).tap() app.buttons["Clear text"].tap() table.cells.elementBoundByIndex(0).textFields.elem entBoundByIndex(0).typeText("xcake") table.swipeDown()
  8. Comparison Calabash Calabash • Adds server to your iOS app

    • View hierarchy is exposed and events are generated • API, DSL (Ruby) for writing tests Xcode 7 • App runs in separate process • UI elements & queries (relationship & filtering); assert values • Events are synthesised on low level of OS • Integrated into Xcode (test target, recording, code completion, running tests, code coverage)
  9. Pros / Cons: Xcode 7 • Nice: fully integrated into

    Xcode (debugging, code coverage, …) • Access to advanced scenarios: pickers, tap links in a webView, reordering of table cells • Backgrounding / foregrounding -> test app lifecycle • Not really expressive: similar to writing unit tests with Kiwi / Spectra vs. XCTest
  10. Pros / Cons: Calabash • More expressive - you write

    tests in DSL • More flexible (e.g. backdoor feature; get view hierarchy) • But NOT integrated… • Similar to Xcode view debugging & Reveal.app: • Xcode has superior integration (auto layout) • but Reveal has great features
  11. References • WWDC https://developer.apple.com/videos/play/wwdc2015-406 • Xcake Calabash http://www.slideshare.net/roland99/ios-and- android-acceptance-testing-with-calabash-xcake-dublin •

    Automate https://krausefx.com/blog/run-xcode-7-ui-tests-from- the-command-line • Documentation http://masilotti.com/xctest-documentation • Tips & Tricks http://masilotti.com/ui-testing-cheat-sheet • App I used for demo: http://shwopping.herokuapp.com
  12. Summary • Recording is huge, but not perfect -> use

    it to learn how to write tests • Queries are evaluated on demand -> keep it DRY • Relationships & filtering -> chain • Integration into Xcode: code coverage, performance testing, screenshots