XCUITest Swift 4 iOS 11 Xcode 9.2

A sample project with lots of working examples can help you navigate XCTest and XCUITest with the new Swift 4.

This is not so much a working app as it was a project to walk the code to find out what will work. StackOverflow had lots of out dated examples on how to do these UI tests, so I took time to figure out XCUITest.   XCTest Unit tests worked fine.

GitHub project SwiftTDD : https://github.com/eSpecialized/SwiftyTDD

Accessing the Title of a Navigation Bar;
Do not set the identifier, iOS 11 sets the identifier based on the title in the navigation bar.

let navBar = self.app.navigationBars.element.identifier

Accessing a Specific Elements Buttons;
If you have several buttons on screen all with the same identifier “Add”, you must select the specific element.

let swiftytddNavigationBar = app.navigationBars["SwiftyTDD"]
swiftytddNavigationBar.buttons["Add"].tap()

Accessing Alert’s uses a Interruption Monitor;

addUIInterruptionMonitor(withDescription: "Title") { (alert) -> Bool in
alert.textFields.firstMatch.typeText("New York, NY")
alert.buttons["Add"].tap()
return true
}

To activate the interruption monitor code,  “app.tap()” after the definition and the monitor will handle it.

Accessing a label that doesn’t exist yet;

let labelStatus = app.staticTexts["Switch is on"]
XCTAssert(labelStatus.waitForExistence(timeout: 5), "Label is missing intended value")

Access a label by identifier, and then the labels text element value;
The label’s accessibility identifier is “GasType” which allows us to access that label.  Then get the .label allows us to retrieve the text part of the label to what it has changed too.

let labelGasType = app.staticTexts["GasType"]
let gasType = labelGasType.label
XCTAssert(gasType == "Premium Gas", "Only ever use Premium gas in this device")

A Picker wheel can also be changed and access the value it is currently;

app.pickerWheels.element.adjust(toPickerWheelValue: "Salem")
let pickerChngToSalem = app.pickerWheels.element.value as! String
XCTAssert(pickerChngToSalem == "Salem", "Pickers selected value is incorrect, it should be salem but is " + pickerChngToSalem)

Access an input field and type text in it, verify it changed;

let inputField1 = app.textFields["field1"]
inputField1.tap()
inputField1.typeText("Fortitude")
let outValfld1 = inputField1.value as! String
XCTAssert(outValfld1 == "Fortitude", "Text Field Didn't update to expected result")
eSpecialized's Blog

Bill Thompson

I am a mobile smartphone and embedded Linux engineer. I love to tinker with electronics and fly drones in spare time when not geeking out in a developers world!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.