scalable and collaborative ios ui test automation in swift

19
Scalable and Collaborative UI Test Automation in Swift Jason Hagglund Staff Software Eng - Test, The Climate Corporation https://github.com/TheMetalCode Twitter - @jhagglund Where are we going? Why bother with UI test automation? XCTest API and UI Elements Identifying UI Elements (Storyboard and Programmatically) Stop Relying on the Recorder and Automate Predictively Write Better UI Automation Code with Screen Objects Extras Questions 2

Upload: josiah-renaudin

Post on 14-Jan-2017

268 views

Category:

Software


1 download

TRANSCRIPT

Page 1: Scalable and Collaborative iOS UI Test Automation in Swift

Scalable and Collaborative UI Test Automation in Swift

Jason Hagglund

Staff Software Eng - Test, The Climate Corporation

https://github.com/TheMetalCode

Twitter - @jhagglund

Where are we going?

● Why bother with UI test automation?

● XCTest API and UI Elements

● Identifying UI Elements (Storyboard and Programmatically)

● Stop Relying on the Recorder and Automate Predictively

● Write Better UI Automation Code with Screen Objects

● Extras

● Questions

2

Page 2: Scalable and Collaborative iOS UI Test Automation in Swift

Why bother with UI test automation?

Because this talk would be really lame if UI tests weren’t important or

useful.

It’s All About the Pyramid

4

http://martinfowler.com/bliki/TestPyramid.html

Page 3: Scalable and Collaborative iOS UI Test Automation in Swift

Why UI Test Automation Efforts Sometimes Fail

if effortToMaintain > valueAdded {

notWorthIt();

}

5

The iOS UI and the XCTest APILooking at the structure of what we’re testing and the code that gives us

the access to the UI.

Page 4: Scalable and Collaborative iOS UI Test Automation in Swift

Our Toolbox for iOS UI Automation

● XCode 7+

● XCTest (supports UI testing as of

XCode 7, iOS 9+)

● Swift

7

● On the web: http://masilotti.com/xctest-

documentation/index.html

● Build it locally: https://github.

com/joemasilotti/XCTest-Documentation

Regarding XCTest Documentation...

8

Page 5: Scalable and Collaborative iOS UI Test Automation in Swift

What is XCTest?

9

A build target that targets your application’s build target. Say that 10x fast.

Your XCTest Bread and Butter

● XCUIApplication

○ This class models the application itself and has two functions:

launch and terminate.

● XCUIElement

○ This class models a UI element contained within the

application.

● XCUIElementQuery

○ This class models a search for an element.

10

Page 6: Scalable and Collaborative iOS UI Test Automation in Swift

UI Test Recorder: An Exploration Tool

11

Identify UI Elements from the Storyboard

The least painful way to uniquely tag our UI elements for automation

purposes

Page 7: Scalable and Collaborative iOS UI Test Automation in Swift

13

Identity Inspector and Accessibility Are Your Friends Accessibility Identifier > Accessibility Label

14

● Lest we forget: the primary purpose of Accessibility-

enabled elements is to allow your app to be used in

accessibility mode on the device.

● Accessibility Labels are what the end user would

actually see/hear in order to interact with your app.

● Accessibility Identifiers don’t leave a mark.

Page 8: Scalable and Collaborative iOS UI Test Automation in Swift

Accessibility Identifier > Accessibility Label

Identify UI Elements Programmatically

Our first encounter with the power of Swift Protocols

Page 9: Scalable and Collaborative iOS UI Test Automation in Swift

About Those Pesky Dynamically Generated Elements...

● Protocol (similar to an Interface in Java): a collection of

properties, methods, etc., that encapsulate common

behavior that you want otherwise unrelated classes to

share.

● UIKit elements conform to the

UIAccessibilityIdentification protocol, which has exactly

one contract requirement: accessibilityIdentifier.

17

Let’s ID Some Table Cells Programmatically

18

Page 10: Scalable and Collaborative iOS UI Test Automation in Swift

Stop Recording and Write Predictive UI Tests

The recorder is an exploration tool, not a testing tool.

A Revelation from Morpheus

20

Page 11: Scalable and Collaborative iOS UI Test Automation in Swift

No Really, It’s This Easy (In Many Cases)

var someButton = app.buttons[“accessibilityIdentifier”]

“app” = instance of XCUIApplication

“buttons” = just one of many XCUIElementTypeQueryProvider methods (hey

look, another Protocol!)

“accessibilityIdentifier” = the accessibility identifier you have configured for

that element

21

Why This Matters

● Kill more bugs faster! (enables TDD-like

practices)

● Plus, just looks cleaner. Remember that ugly

recorder-generated code earlier?

22

Page 12: Scalable and Collaborative iOS UI Test Automation in Swift

Write Better UI Automation with Screen Objects

Your tests are software. Code like it.

Which Would You Rather Maintain?

24

Page 13: Scalable and Collaborative iOS UI Test Automation in Swift

Which Would You Rather Maintain?

25

Morpheus Says Take Pride in Your Test Automation Code!

26

Page 14: Scalable and Collaborative iOS UI Test Automation in Swift

Loadable: All Screen Objects Can Tell if They’re allLoaded()

27

Encapsulate UI Element Identification

28

Page 15: Scalable and Collaborative iOS UI Test Automation in Swift

Encapsulate Actions

29

Encapsulate Complex Assertions

30

Page 16: Scalable and Collaborative iOS UI Test Automation in Swift

Behold! Clean, Easy-to-Write UI Tests!

31

ExtrasFor more advanced test automation needs

Page 17: Scalable and Collaborative iOS UI Test Automation in Swift

Asynchronous Test Expectations

● Doable, but kinda ugly. DRY and write methods that

wait on elements once.

● https://gist.github.

com/kcharwood/0a57d7a207aba1d578da

● http://masilotti.com/xctest-helpers/

33

Code Coverage for UI Tests

34

Page 18: Scalable and Collaborative iOS UI Test Automation in Swift

Cucumber/Gherkin Goodness!

● Yes, really, somebody is finally doing this!

● Integrates with both KIF and XCTest

● https://github.com/Ahmed-Ali/Cucumberish

35

ConclusionSome stuff I hope you took away from all this that will stick.

Page 19: Scalable and Collaborative iOS UI Test Automation in Swift

Scalable and Collaborative

● Scalable: leverages the well-worn patterns and practices

of object-oriented programming to make producing UI

tests as easy and DRY as possible.

● Collaborative: a first class citizen that lives in the

application code and conforms to the same high-quality

standards.

● Devs + Testers can make awesome apps together

37

Email - [email protected]

Twitter - @jhagglund

Code - https://github.com/TheMetalCode/ios-ui-automation-demo

Thank you! Any Questions?