Fresh install in the simulator

To make sure that your UI tests start with a fresh install of the app on the simulator you can uninstall the app after building the UI tests: Open the project settings, go to the UITests target and in the section “Run Script” add the following command line:

	xcrun simctl uninstall booted com.vvse.toiletmap

Use “accessibilityIdentifier” or “accessibilityLabel”
to identify UI elements

If possible use accessibilityIdentifier or accessibilityLabel to identify a UI element. This ensures that tests still work when the label text changes or a different UI language is used.

How to specify the “accessibilityIdentifier” of an UI element in the storyboard:

In the storyboard show the identity inspector. In the section “User defined Runtime Attributes” press “+” and enter “accessibilityIdentifier” as a key path, select type “String” and enter the identifier string that you would like to use to reference this UI element in the XCUITest test case.

Setting an accessibility identifier

In the XCUITest test case you can then reference this UI element like this:

	let app = XCUIApplication()
        let mapTypesButton = app.buttons["map_types"]
        mapTypesButton.tap()

“Timestamped Event Matching Error: Failed to find matching element”

Error message: Timestamped Event Matching Error

When recording a UI test you might encounter this error when trying to swipe an object. It is most likely caused by the object being swiped not having accessibility enabled. Just enable it in the identity inspector of the storyboard editor:

Enable accessibility in the identity inspector

If you were trying to swipe on a table cell you should check all UI elements that are part of the cell and the cell itself.

Comparing localized UI texts

If you add your strings files to the UI test target you can use NSLocalizedString() to compare the value of a UILabel element with an expected localized text value. Assume that your localized string are in Localizable.strings. Open the project settings, select the UI test target and click on “Build Phases”. Expand the “Copy Bundle Resources” section and add “Localizable.strings” to the list.

[![](https://www.vvse.com/blog/blog/assets/LocalizableStrings.png "Add localizable strings to test target")](https://www.vvse.com/blog/blog/assets/LocalizableStrings.png)

When using NSLocalizationString() you have to specify the bundle of the UI Test app since the default bundle seems to refer to the test launcher app:

    let newCellValue = app.tables.cells[cellName].staticTexts["value_text"].label
    let expectedCellValueString = NSLocalizedString(expectedCellValueStringId, 
    				bundle: Bundle(for: SomeUITestClass.self), 
    				comment: "" )
    XCTAssertEqual(newCellValue, expectedCellValueString)

Running UI tests from the command line

You can you the xcodebuild command to run both unit and UI tests from command line. If you are using CocoaPods make sure to use -workspace instead of -project otherwise the Swift compiler won’t find the frameworks that are defined in the podfile.

e.g.:

xcodebuild test -workspace MayApp.xcworkspace -scheme MyApp -destination 'platform=iOS Simulator,name=iPhone 8 Plus

Video demonstrating XCUITest in action

The following video show the UI tests of a real world app on a simulated iPhone 8 Plus. It tests the UI of the free and pro version of the Toilet Map app both in English and in German.