SlideShare a Scribd company logo
UI Testing in Xcode
Allan Shih
August 16th, 2017
Agenda
• UI testing
– Find and interact with UI elements
– Validate UI properties and state
• UI recording
• Improving Accessibility and UI Testing
• Reference
Core Technologies
Accessibility
+
XCTest
• Test case subclasses
• Test methods
• Assertions
• Integrated with Xcode
• CI via Xcode Server and xcodebuild
• Swift and Objective-C
Accessibility
• Rich semantic data about UI
• UIKit and AppKit integration
• APIs for fine tuning
• UI tests interact with the app
the way a user does
Requirements
• UI testing depends on new OS features
– iOS 9
– OS X 10.11
• Privacy protection
– iOS devices
• Enabled for development
• Connected to a trusted host running Xcode
– OS X must grant permission to Xcode Helper
Getting Started
• Xcode target type
• APIs
• UI recording
UI Testing Target Template
Target Application
APIs
• XCUIApplication
• XCUIElement
• XCUIElementQuery
UI Recording
• Interact with your app
• Recording generates the code
– Create new tests
– Expand existing tests
DEMO
Getting started with UI testing
What Did You See?
• Using recording
– Finding UI elements
– Synthesizing user events
• Adding validation with XCTAssert
UI Testing API
Setup
Find XCUIElement
func testTapPlayWithoutSingInButton_withoutSigningIn_doGoToHome() {
let app = XCUIApplication()
let playButton = app.buttons["Play without Sign in"]
XCTAssertTrue(playButton.exists)
Press Button on Remote Control
// action
XCUIRemote.shared().press(.select)
wait(for: 1)
XCUIRemote.shared().press(.menu)
Assertion
// assertion
let loginButton = app.tabBars.buttons["Login"]
XCTAssertTrue(loginButton.exists)
UITest Example
func testTapPlayWithoutSingInButton_withoutSigningIn_doGoToHome() {
let app = XCUIApplication()
let playButton = app.buttons["Play without Sign in"]
XCTAssertTrue(playButton.hasFocus)
XCUIRemote.shared().press(.select)
wait(for: 1)
XCUIRemote.shared().press(.menu)
// assertion
let loginButton = app.tabBars.buttons["Login"]
XCTAssertTrue(loginButton.exists)
}
XCUIApplication
• Proxy for the tested application
– Tests run in a separate process
• Launch
– Always spawns a new process
– Implicitly terminates any preexisting instance
• Starting point for finding elements
XCUIElement
• Proxy for elements in application
• Types
– Button, Cell, Window, etc.
• Identifiers
– Accessibility identifier, label, title, etc.
• Most elements are found by
combining type and identifier
Element Hierarchy
Application
|- View
|- Table
|- Cell
| |- “YOUR ACCOUNT”
|
|- Cell
| |- “john.smith2@mattel.com”
|
|- Cell
| |- “KID ACCOUNTS”
Element Hierarchy
Application
|- View
|- Table
|- Cell
| |- “YOUR ACCOUNT”
|
|- Cell
| |- “john.smith2@mattel.com”
|
|- Cell
| |- “KID ACCOUNTS”
Element Hierarchy
Application
|- View
|- Table
|- Cell
| |- “YOUR ACCOUNT”
|
|- Cell
| |- “john.smith2@mattel.com”
|
|- Cell
| |- “KID ACCOUNTS”
Element Hierarchy
Application
|- View
|- Table
|- Cell
| |- “YOUR ACCOUNT”
|
|- Cell
| |- “john.smith2@mattel.com”
|
|- Cell
| |- “KID ACCOUNTS”
Element Uniqueness
• Every XCUIElement is backed by a query
• Query must resolve to exactly one match
– No matches or multiple matches cause test
failure
– Failure raised when element resolves query
• Exception
– exists property
XCUIElement Exists Example
func testTapPlayButton_signingIn_doGoToHome() {
let app = XCUIApplication()
let playButton = app.buttons["Play without Sign in"]
XCTAssertTrue(playButton.exists)
Event Synthesis
• Simulate user interaction on elements
• APIs are platform-specific
– button.click() // OS X
– button.tap() // iOS
– XCUIRemote.shared().press(.select) // tvOS
– textField.typeText(“Hello, World!”) // iOS &
OS X & tvOS
XCUIElementQuery
• Queries resolve to collections of accessible
elements
– Number of matches: count
– Specify by identifier: subscripting
– Specify by index: element(boundBy: Index)
XCUIElementQuery - Descendant
Application
|- View
|- Table
|- Cell
| |- “YOUR ACCOUNT”
|
|- Cell
| |- “john.smith2@mattel.com”
|
|- Cell
| |- “KID ACCOUNTS”
XCUIElementQuery - Children
Application
|- View
|- Table
|- Cell
| |- “YOUR ACCOUNT”
|
|- Cell
| |- “john.smith2@mattel.com”
|
|- Cell
| |- “KID ACCOUNTS”
XCUIElementQuery - Containment
Application
|- View
|- Table
|- Cell
| |- “YOUR ACCOUNT”
|
|- Cell
| |- “john.smith2@mattel.com”
|
|- Cell
| |- “KID ACCOUNTS”
XCUIElementQuery - Filtering
• Element type
– Button, table, menu, etc.
• Identifiers
– Accessibility identifier, label, title, etc.
• Predicates
– Value, partial matching, etc.
Combining Relationships and Filtering
• let allButtons = app.buttons
• let allCellsInTable = table.cells
• let allMenuItemsInMenu =
menu.menuItems
Find elements by describing their descendants
Application
|- View
|- Table
|- Cell
| |- “YOUR ACCOUNT”
|
|- Cell
| |- “KID ACCOUNTS”
let cellQuery =
app.tables.cells.containing(
.staticText,
identifier: ”YOUR ACCOUNT”
)
Getting Elements from Queries
Method Example
Subscripting table.staticTexts[“YOUR ACCOUNT”]
Index table.staticTexts.element(boundBy: 0)
Unique table.navigationBars.element
DEMO
Getting Elements from Queries
API Recap
Accessibility and UI Testing
Accessibility data makes UI testing possible
Poor Accessibility Data
let textView = XCUIApplication().windows
.children(matching: .other).element(boundBy: 2)
.children(matching: .other).element
.children(matching: .textView).element
Improving Accessibility and UI Testing
let textView = app.textViews[“content”]
DEMO
Improving Accessibility Data
Reference
• UI Testing in Xcode - WWDC 2015
• UI Testing Cheat Sheet
• iOS Unit Testing and UI Testing Tutorial - raywenderlich
• Getting started with Xcode UI testing in Swift - Medium

More Related Content

PDF
PDF
Lets make a better react form
PDF
Robot Framework :: Demo login application
PPTX
Presentation on Apache Jmeter
PPT
Testing In Django
PPTX
Writing and using Hamcrest Matchers
PDF
Swift Programming Language
PDF
Performance Testing for Mobile Apps & Sites using Apache JMeter
Lets make a better react form
Robot Framework :: Demo login application
Presentation on Apache Jmeter
Testing In Django
Writing and using Hamcrest Matchers
Swift Programming Language
Performance Testing for Mobile Apps & Sites using Apache JMeter

What's hot (20)

PPTX
Cypress test techniques cucumber bdd framework,tdd,api tests course
PPTX
Automation Testing by Selenium Web Driver
PDF
Swift Programming Language
PPTX
Integration testing for microservices with Spring Boot
PPTX
Load testing with J meter
PPTX
Automation Testing of Shadow DOM Elements with Katalon Studio
PPTX
Performance testing using Jmeter for apps which needs authentication
PDF
Statement Testing and Statement Coverage. ISTQB whitebox techniques with Test...
PPT
Selenium ppt
PPTX
Selenium Training | TestNG Framework For Selenium | Selenium Tutorial For Beg...
PDF
Mobile Performance Testing Approaches and Challenges
PPT
ODP
Beginners - Get Started With Unit Testing in .NET
PDF
Completable future
PPTX
Introduction to microbit-2
PPTX
Tricentis Tosca Libraries - Value Range
PPTX
Data driven testing
PPTX
Automation - web testing with selenium
PDF
Introduction to flutter
PDF
Cypress - Best Practices
Cypress test techniques cucumber bdd framework,tdd,api tests course
Automation Testing by Selenium Web Driver
Swift Programming Language
Integration testing for microservices with Spring Boot
Load testing with J meter
Automation Testing of Shadow DOM Elements with Katalon Studio
Performance testing using Jmeter for apps which needs authentication
Statement Testing and Statement Coverage. ISTQB whitebox techniques with Test...
Selenium ppt
Selenium Training | TestNG Framework For Selenium | Selenium Tutorial For Beg...
Mobile Performance Testing Approaches and Challenges
Beginners - Get Started With Unit Testing in .NET
Completable future
Introduction to microbit-2
Tricentis Tosca Libraries - Value Range
Data driven testing
Automation - web testing with selenium
Introduction to flutter
Cypress - Best Practices
Ad

Similar to Ui testing in xcode (20)

PDF
Automated Xcode 7 UI Testing
PPTX
open-west
PDF
UI testing in Xcode 7
PDF
X-Code UI testing architecture and tools
PPTX
iOS Automation: XCUITest + Gherkin
PPTX
Getting Started with XCTest and XCUITest for iOS App Testing
PDF
iOS UI Testing in Xcode
KEY
Automated ui testing
PDF
Xcode7 UI Automation
PDF
XCTest_ A Complete Comprehensive Guide.pdf
PDF
Xcode 7 UI Testing - Xcake Dublin, October 2015
PDF
Advanced Appium Tips & Tricks with Jonathan Lipps
PDF
Understanding XCUITest Framework Your Guide to Efficient iOS Testing.pdf
PDF
Uncovering breaking changes behind UI on mobile applications
ODP
Alexandre.iline rit 2010 java_fxui_extra
PDF
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
PDF
Automated interactive testing for i os
KEY
Effectively Using UI Automation
PDF
Make XCUITest Great Again
PDF
Cucumber meets iPhone
Automated Xcode 7 UI Testing
open-west
UI testing in Xcode 7
X-Code UI testing architecture and tools
iOS Automation: XCUITest + Gherkin
Getting Started with XCTest and XCUITest for iOS App Testing
iOS UI Testing in Xcode
Automated ui testing
Xcode7 UI Automation
XCTest_ A Complete Comprehensive Guide.pdf
Xcode 7 UI Testing - Xcake Dublin, October 2015
Advanced Appium Tips & Tricks with Jonathan Lipps
Understanding XCUITest Framework Your Guide to Efficient iOS Testing.pdf
Uncovering breaking changes behind UI on mobile applications
Alexandre.iline rit 2010 java_fxui_extra
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Automated interactive testing for i os
Effectively Using UI Automation
Make XCUITest Great Again
Cucumber meets iPhone
Ad

More from allanh0526 (18)

PPTX
PPTX
Digital authentication
PDF
Integration of slather and jenkins
PDF
How to generate code coverage reports in xcode with slather
PDF
Unit testing in xcode 8 with swift
PDF
How to work with dates and times in swift 3
PDF
Using a model view-view model architecture for iOS apps
PDF
iOS architecture patterns
PDF
ThingMaker in Swift
PDF
Automatic reference counting in Swift
PDF
Core data in Swfit
PDF
From android/java to swift (3)
PDF
From android/ java to swift (2)
PDF
From android/java to swift (1)
PDF
WebRTC
PDF
Pipeline interface
PDF
Deploying artifacts to archiva
PPT
Android httpclient
Digital authentication
Integration of slather and jenkins
How to generate code coverage reports in xcode with slather
Unit testing in xcode 8 with swift
How to work with dates and times in swift 3
Using a model view-view model architecture for iOS apps
iOS architecture patterns
ThingMaker in Swift
Automatic reference counting in Swift
Core data in Swfit
From android/java to swift (3)
From android/ java to swift (2)
From android/java to swift (1)
WebRTC
Pipeline interface
Deploying artifacts to archiva
Android httpclient

Recently uploaded (20)

PDF
Empathic Computing: Creating Shared Understanding
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Encapsulation theory and applications.pdf
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
KodekX | Application Modernization Development
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Machine learning based COVID-19 study performance prediction
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Encapsulation_ Review paper, used for researhc scholars
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PPTX
Spectroscopy.pptx food analysis technology
Empathic Computing: Creating Shared Understanding
NewMind AI Weekly Chronicles - August'25 Week I
Encapsulation theory and applications.pdf
Chapter 3 Spatial Domain Image Processing.pdf
KodekX | Application Modernization Development
Mobile App Security Testing_ A Comprehensive Guide.pdf
Machine learning based COVID-19 study performance prediction
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Reach Out and Touch Someone: Haptics and Empathic Computing
Advanced methodologies resolving dimensionality complications for autism neur...
Dropbox Q2 2025 Financial Results & Investor Presentation
Understanding_Digital_Forensics_Presentation.pptx
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
The AUB Centre for AI in Media Proposal.docx
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Encapsulation_ Review paper, used for researhc scholars
“AI and Expert System Decision Support & Business Intelligence Systems”
MYSQL Presentation for SQL database connectivity
Diabetes mellitus diagnosis method based random forest with bat algorithm
Spectroscopy.pptx food analysis technology

Ui testing in xcode