xcode 7 ui testing - xcake dublin, october 2015
TRANSCRIPT
UI Testing
• Find UI elements
• Interact with the app the same way as user does
• Validate changes through assertions
Why would I do it?
XCTest• Xcode’s testing framework
• Apple introduced it in Xcode 5 - keeps adding:
• Unit Tests
• Performance Tests
• UI Tests
• Supports Objective-C and Swift
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
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
Queries - Filtering
• Filter by Element type (button, table, …)
• Filter by Identifier (label, title, …)
• Predicates (value, partial matching, …)
• Remember: Queries can be chained
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
Examplelet 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.elementBoundByIndex(0).typeText("xcake") table.swipeDown()
Wait for Element
Comparison CalabashCalabash
• 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)
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
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
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
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