Skip to content

UI Testing

  • Select storyboard.
  • Expand the Utilities
  • Select Identity Inspector
  • Select your element on storyboard
  • Add new Accessibility Identifier (in example addButton)

enter image description here

  • Select storyboard.
  • Expand the Utilities
  • Select Identity Inspector
  • Select your element on storyboard
  • Add attribute in User Defined Runtime Attributes
  • For Key Path type - accessibilityIdentifier
  • For Type - `String
  • For Value - new accessibility identifier for your element (in example view)

enter image description here

import XCTest
class StackOverFlowUITests: XCTestCase {
private let app = XCUIApplication()
//Views
private var view: XCUIElement!
//Buttons
private var addButton: XCUIElement!
override func setUp() {
super.setUp()
app.launch()
//Views
view = app.otherElements["view"]
//Buttons
addButton = app.buttons["addButton"]
}
func testMyApp() {
addButton.tap()
view.tap()
}
}

In [ ] add Accessibility Identifier for element.

let imageView = app.images["imageView"]
let scrollView = app.scrollViews["scrollView"]
let view = app.otherElements["view"]
let label = app.staticTexts["label"]
let stackView = app.otherElements["stackView"]
let tableView = app.tables["tableView"]
let tableViewCell = tableView.cells["tableViewCell"]
let tableViewCellButton = tableView.cells.element(boundBy: 0).buttons["button"]
let collectionView = app.collectionViews["collectionView"]
let button = app.buttons["button"]
let barButtonItem = app.buttons["barButtonItem"]
  • normal UITextField
let textField = app.textFields["textField"]
  • password UITextField
let passwordTextField = app.secureTextFields["passwordTextField"]
let textView = app.textViews["textView"]
let switch = app.switches["switch"]
let alert = app.alerts["About yourself"] // Title of presented alert

You should check “Include UI Tests” in the project creation dialog.

enter image description here

If you missed checking UI target while creating project, you could always add test target later.

Setps:

  • While project open go to File -> New -> Target
  • Find iOS UI Testing Bundle

enter image description here

In a test you can disable animations by adding in setUp:

app.launchEnvironment = ["animations": "0"]

Where app is instance of XCUIApplication.

Lunch and Terminate application while executing

Section titled “Lunch and Terminate application while executing”
override func setUp() {
super.setUp()
let app = XCUIApplication()
app.launch()
}
func testStacOverFlowApp() {
app.terminate()
}

Device can be rotate by changing orientation in XCUIDevice.shared().orientation:

XCUIDevice.shared().orientation = .landscapeLeft
XCUIDevice.shared().orientation = .portrait
  • XCUIApplication() // Proxy for an application. The information identifying the application is specified in the Xcode target settings as the “Target Application”.
  • XCUIElement() // A user interface element in an application.