• Stars
    star
    2,147
  • Rank 21,492 (Top 0.5 %)
  • Language
    Swift
  • Created about 9 years ago
  • Updated over 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

How do I test this with UI Testing?

UI Testing Cheat Sheet

This repository is complementary code for my post, UI Testing Cheat Sheet and Examples. The post goes into more detail with example images for most examples.

The included project highlights working code with a simple Test Host. This was last updated for Swift 5 on Xcode 11.4.1.

Contents

Basic Functionality

Testing if an element exists

XCTAssert(app.staticTexts["Welcome"].exists)

Testing if text with an ellipsis exists

A full text match will find an element even if the displayed text has an ellipsis due to truncation.

let longNameCell = app.staticTexts["Adolph Blaine Charles David Earl Frederick Gerald Hubert Irvin John Kenneth Lloyd Martin Nero Oliver Paul Quincy Randolph Sherman Thomas Uncas Victor William Xerxes Yancy Wolfeschlegelsteinhausenbergerdorff, Senior"]
XCTAssert(longNameCell.exists) // displayed text is "Adolph Blaine Charles David Earl Freder..."

Waiting for an element to appear

"Waiting" is now built into XCTest.

let goLabel = app.staticTexts["Go!"]
XCTAssertFalse(goLabel.exists)

app.buttons["Ready, set..."].tap()
XCTAssert(goLabel.waitForExistence(timeout: 5))

Interacting with System Controls

Tapping buttons

Identify buttons by their accessibility label.

app.buttons["Add"].tap()

Typing text

First make sure the text field has focus by tapping on it.

let textField = app.textFields["Username"]
textField.tap()
textField.typeText("joemasilotti")

Dismissing alerts

app.alerts["Alert Title"].buttons["Button Title"].tap()

Dismissing action sheets

app.sheets["Sheet Title"].buttons["Button Title"].tap()

Handling system alerts

Present a location services authorization dialog to the user and dismiss it with the following code.

Before presenting the alert add a UI Interruption Handler. When this fires, dismiss with the "Allow" button.

addUIInterruptionMonitor(withDescription: "Location Services") { (alert) -> Bool in
  alert.buttons["Allow"].tap()
  return true
}

app.buttons["Request Location"].tap()
app.tap() // need to interact with the app again for the handler to fire

Sliding sliders

This will slide the value of the slider to 70%.

app.sliders.element.adjust(toNormalizedSliderPosition: 0.7)

Interacting with pickers

A picker with one wheel:

app.pickerWheels.element.adjust(toPickerWheelValue: "Picker Wheel Item Title")

A picker with multiple wheels. Make sure to set the accessibility delegate so the framework can identify the different wheels.

let firstPredicate = NSPredicate(format: "label BEGINSWITH 'First Picker'")
let firstPicker = app.pickerWheels.element(matching: firstPredicate)
firstPicker.adjust(toPickerWheelValue: "first value")

let secondPredicate = NSPredicate(format: "label BEGINSWITH 'Second Picker'")
let secondPicker = app.pickerWheels.element(matching: secondPredicate)
secondPicker.adjust(toPickerWheelValue: "second value")

Tapping links in web views

app.links["Tweet this"].tap()

Interactions

Verifying the current controller's title

XCTAssert(app.navigationBars["Details"].exists)

Reordering table cells

If you have a UITableViewCell with default style and set the text to "Title", the reorder control's accessibility label becomes "Reorder Title".

Using this we can drag one reorder control to another, essentially reordering the cells.

let topButton = app.buttons["Reorder Top Cell"]
let bottomButton = app.buttons["Reorder Bottom Cell"]
bottomButton.press(forDuration: 0.5, thenDragTo: topButton)

XCTAssertLessThanOrEqual(bottomButton.frame.maxY, topButton.frame.minY)

Pull to refresh

Create a XCUICoordinate from the first cell in your table and another one with a dy of six. Then drag the first coordinate to the second.

let firstCell = app.staticTexts["Adrienne"]
let start = firstCell.coordinate(withNormalizedOffset: (CGVectorMake(0, 0))
let finish = firstCell.coordinate(withNormalizedOffset: (CGVectorMake(0, 10))
start.press(forDuration: 0, thenDragTo: finish)

Pushing and popping view controllers

Test if a view controller was pushed onto the navigation stack.

app.buttons["More Info"].tap()
XCTAssert(app.navigationBars["Volleyball?"].exists)

Pop a view controller by tapping the back button in the navigation bar and assert that the title in the navigation bar has changed.

app.navigationBars.buttons.elementBoundByIndex(0).tap()
XCTAssert(app.navigationBars["Volley"].exists)

More Repositories

1

railsdevs.com

The reverse job board for Ruby on Rails developers.
Ruby
547
star
2

TestingNSURLSession

Accompaniment to a series of blog posts covering how to test NSURLSession with Swift.
Swift
48
star
3

masilotti.com

Source for masilotti.com, built with Bridgetown and Tailwind CSS.
HTML
41
star
4

HTTP-Client

A barebones Swift HTTP client with automatic JSON response parsing.
Swift
30
star
5

JAMTestHelper

A few additions to XCTest geared towards UI Testing in Xcode 7 and iOS 9.
Swift
23
star
6

ios-feature-testing

A comparison of the current iOS feature testing frameworks available.
23
star
7

Ruka

Testing the UI without UI Testing, a Swift experiment.
Swift
20
star
8

PebCiti

A simple Pebble app to show the nearest available CitiBike NYC dock.
19
star
9

TailwindCSS-SwiftUI

TailwindCSS colors for SwiftUI.
Swift
18
star
10

x-wing-ai-tailwind

A landing page built with Tailwind CSS for my iOS app, X-Wing AI.
CSS
14
star
11

CustomHeightPresentationController

A presentation wrapper to show a controller that doesn't fill the full screen.
Swift
13
star
12

try-Swift-UI-Testing-stubbing-the-network

Source code for my "UI Testing: stubbing the network" workshop at the 2020 try Swift! World conference.
Swift
8
star
13

Turbo-iOS-Demo

Swift
8
star
14

UIPickerViewTester

How to interact with a UIPickerView using UI Testing in Xcode 7.
Swift
6
star
15

portland-heritage-trees

Swift
4
star
16

git-auto-tagger

Tag commits based on a YAML file.
Ruby
4
star
17

Turbo-Native-demo

Accompanying code for my "Turbo Native in 15 minutes" video.
Swift
3
star
18

try-Swift-UI-Testing-an-introduction

Source code for my "UI Testing: an introduction" workshop at the 2020 try Swift! World conference.
Swift
2
star
19

Launch-Tester

Swift
1
star
20

turbo-auth-swift

Swift
1
star
21

joemasilotti

Joe Masilotti's GitHub profile.
1
star
22

PebCiti-iOS

iOS half of PebCiti, a simple app to show the nearest available CitiBike NYC dock on your Pebble.
Objective-C
1
star
23

turbo-auth-rails

Ruby
1
star