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 Xcode 7 project highlights working code with a simple Test Host. This was last updated for Xcode 7 GM.
XCTAssert(app.staticTexts["Welcome"].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..."
Set up an expectation to use with XCTest
. The predicate will wait until the element's -exist
property is true.
let goLabel = app.staticTexts["Go!"]
XCTAssertFalse(goLabel.exists)
let exists = NSPredicate(format: "exists == true")
expectation(for: exists, evaluatedWithObject: goLabel, handler: nil)
app.buttons["Ready, set..."].tap()
waitForExpectations(timeout: 5, handler: nil)
XCTAssert(goLabel.exists)
Identify buttons by their accessibility label.
app.buttons["Add"].tap()
First make sure the text field has focus by tapping on it.
let textField = app.textFields["Username"]
textField.tap()
textField.typeText("joemasilotti")
app.alerts["Alert Title"].buttons["Button Title"].tap()
app.sheets["Sheet Title"].buttons["Button Title"].tap()
Present a location services authorization dialog to the user and dismiss it with the following code.
Before presenting the alert add a UI Interuption 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
This will slide the value of the slider to 70%.
app.sliders.element.adjust(toNormalizedSliderPosition: 0.7)
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")
app.links["Tweet this"].tap()
XCTAssert(app.navigationBars["Details"].exists)
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)
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, 6))
start.press(forDuration: 0, thenDragTo: finish)
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)