iOS mobile application UI can be automated by using the Apple XCUITEST testing framework integrated with XCode. It is one of the alternate approach to avoid using Appium and you can refer the framework comparison matrix using the below link;
Automation script should be prepared in XCode using the Swift language and UI Recorder option is available in XCode. Please avoid using Recorder in which auto-generated script can easily break during long run. For example, slider specific user actions cannot be automated using the recorder option available in XCode.
The biggest advantage of Appium based UI Automation is nothing but source code of iOS mobile application is not required in which .IPA or .APP file can be used to start creating automation scripts but XCUITEST required source code of the iOS mobile application. I’ve taken UICatalog open source iOS application source code from GitHub using the below URL for demo run;
https://github.com/appium/ios-uicatalog
In the same source code project, you should create a “iOS UI Testing Bundle” in XCode as follows;
- Click File –> New –> Target
- Click iOS UI Testing Bundle under Test section
- Click Next
- Leave the default values and select your developer account team name
- Click Finish
Your target project name appended with Tests should be created and available under the project explorer in XCode. Please note, Apple developer account required to start work on the real iOS devices.
Open the ‘UICatalogUITests.swift’ available in the project explorer and you could see the setup and teardown method as like Junit or TestNG. All your test case method should start with ‘test’ keyword as mentioned below;
Please refer each test method name, which will explain about the exact touch actions commenced in the iOS mobile application under automation;
import XCTest
class UICatalogUITests: XCTestCase {
override func setUp() {
super.setUp()
// Put setup code here. This method is called before the invocation of each test method in the class.
// In UI tests it is usually best to stop immediately when a failure occurs.
continueAfterFailure = false
// UI tests must launch the application that they test. Doing this in setup will make sure it happens for each test method.
XCUIApplication().launch()
// In UI tests it’s important to set the initial state – such as interface orientation – required for your tests before they run. The setUp method is a good place to do this.
}
override func tearDown() {
// Put teardown code here. This method is called after the invocation of each test method in the class.
super.tearDown()
}
func testSingleTap() {
let app = XCUIApplication()
let tablesQuery = app.tables
tablesQuery.staticTexts[“Action Sheets”].tap()
tablesQuery.staticTexts[“Okay / Cancel”].tap()
app.sheets.buttons[“OK”].tap()
app.navigationBars[“Action Sheets”].buttons[“UICatalog”].tap()
}
func testMultipleTap()
{
let app = XCUIApplication()
let elementSteppers = app.staticTexts[“Steppers”]
elementSteppers.press(forDuration: 1)
let tablesQuery = app.tables
tablesQuery.children(matching: .cell).element(boundBy: 0).buttons[“Increment”].doubleTap()
}
func testScrollUpDown()
{
let app = XCUIApplication()
let scrollElement1 = app.staticTexts[“Date Picker”]
let tablesQuery = app.tables
let scrollElement2 = tablesQuery.staticTexts[“Sliders”]
let scrollElement3 = tablesQuery.staticTexts[“Web View”]
scrollElement2.press(forDuration: 2, thenDragTo: scrollElement1)
tablesQuery.staticTexts[“Sliders”].press(forDuration: 2, thenDragTo: scrollElement3)
}
func testLongPress()
{
let app = XCUIApplication()
let elementSteppers = app.staticTexts[“Steppers”]
elementSteppers.press(forDuration: 1)
let tablesQuery = app.tables
tablesQuery.children(matching: .cell).element(boundBy: 0).buttons[“Increment”].press(forDuration: 5)
}
func testEnterText()
{
let app = XCUIApplication()
let tablesQuery = app.tables
let elementTextField = tablesQuery.staticTexts[“Text Fields”]
elementTextField.tap()
let cell = tablesQuery.children(matching: .cell).element(boundBy: 0)
cell.textFields[“Placeholder text”].tap()
cell.textFields[“Placeholder text”].typeText(“Welcome to UI Testing”)
}
func testSlider()
{
XCUIApplication().staticTexts[“Sliders”].tap()
let slider = XCUIApplication().sliders[“42%”]
XCTAssert(slider.exists)
let fiftyPercent = slider.coordinate(withNormalizedOffset: CGVector(dx: 0.5, dy: 0.5))
let ninetyPercent = slider.coordinate(withNormalizedOffset: CGVector(dx: 0.9, dy: 0.5))
fiftyPercent.press(forDuration: 0.1, thenDragTo: ninetyPercent)
sleep(5)
}
func testDatePicker()
{
let app = XCUIApplication()
app.staticTexts[“Date Picker”].tap()
let datePickersQuery = XCUIApplication().datePickers
datePickersQuery.pickerWheels[“Today”].swipeUp()
let pmPickerWheel = datePickersQuery.pickerWheels[“PM”]
pmPickerWheel.swipeDown()
}
}
Please find the below video for automated script execution in iPhone Simulator;