Skip to content

Instantly share code, notes, and snippets.

@alexj70
Created April 28, 2017 13:02
Show Gist options
  • Save alexj70/b52407817a62735d74309baf89d519af to your computer and use it in GitHub Desktop.
Save alexj70/b52407817a62735d74309baf89d519af to your computer and use it in GitHub Desktop.
UI Testing

UI Testing

★ Get Application

let app = XCUIApplication()

accessibilityIdentifier by code

@IBOutlet weak var button: UIButton!

override func viewDidLoad() {
    super.viewDidLoad()
    
    button.accessibilityIdentifier = "Button"
}

★ Setting the device orientation

XCUIDevice.shared().orientation = .faceUp

★ Pressing the home button of the device

XCUIDevice.shared().press(XCUIDeviceButton.home)

★ Manipulate the volume of the device

// Raise the volume
XCUIDevice.shared().press(XCUIDeviceButton.volumeUp)

// Lower the volume
XCUIDevice.shared().press(XCUIDeviceButton.volumeDown)

UIButton

★ Get UIButton object

let button = app.buttons["identifier"]

★ Tap on UIButton

// Single tap
app.buttons["identifier"].tap()

// Double tap
app.buttons["identifier"].doubleTap()

★ UIButton's title test

let title = app.buttons["Button"].label
XCTAssertEqual(title, "button")

UINavigationItem

★ Get Navigation's BarButtonItem

let barbutton = app.navigationBars["UITestingViewController.View"].buttons["identifier"]

★ Tap of Navigation's Back button

app.navigationBars["UIView"].children(matching: .button).matching(identifier: "Back").element(boundBy: 0).tap()

UITextField

★ Compare UITextField's text

let textFieldText = app.textFields["VCTextField"].value as? String
XCTAssertEqual(textFieldText, "0")

★ Tap the TextField to display the keyboard

let textField = element.children(matching: .textField).element
textField.tap()

★ Enter characters in UITextField

// I can not input without displaying the keyboard

let textField = app.textFields["identifier"]
textField.tap()

// input text
textField.typeText("text")

UISlider

★ Change the value of UISlider

let slideridSlider = XCUIApplication().sliders["SliderID"]
slideridSlider.adjust(toNormalizedSliderPosition: 0)
slideridSlider.adjust(toNormalizedSliderPosition: 0.5)
slideridSlider.adjust(toNormalizedSliderPosition: 1)        
        

// Tap Slider
slideridSlider.tap()

UISwitch

★ switch switching when ID of Switch is not specified

app.switches["1"].tap()
app.switches["0"].tap()

★ Switch when Switch ID is specified

let switchObj = XCUIApplication().switches["Identifier"]
switchObj.tap()
switchObj.tap()
switchObj.tap()

UITableView

★ Tap the table cell

let app = XCUIApplication()
let cell = app.tables.cells.element(boundBy: 4)

// tap cell
cell.tap()

// When the ID of the cell is specified
XCUIApplication().tables.cells.matching(identifier: "CellID").element(boundBy: 4).tap()

// ID and textLabel? .text specified
XCUIApplication().tables.cells.matching(identifier: "CellID").staticTexts[textLabel?.text].tap()

// When only textLabel? .text is specified 1
XCUIApplication().tables.cells.staticTexts[textLabel?.text].tap()

// When only textLabel? .text is specified 2
XCUIApplication().tables.staticTexts[textLabel?.text].tap()

★ Test the number of UITableView cells

let app = XCUIApplication()
XCTAssertEqual(5, app.tables.cells.count)

★ XCUIElementTypeQueryProvider

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment