In this guide we will explore the possibility and benefits of automating native iOS apps using XCUITest. We will provide various example code snippets on how to automate the Settings, Messages, Photos and other native iOS apps using code.
XCUITest is a testing framework provided by Apple, built to create automated UI testing of iOS applications. You can write code in both Swift or Objective-C. It offers various functionalities to locate elements located in the UI, and ways to interact with these elements.
If you don't have an iPhone or iPad device, you can still use XCUITest to run on iOS simulators. Or you can use TestingBot's remote iOS devices for iOS app automation.
What are the various Touch Actions in Appium?
To create a new XCTest project in Xcode, please follow thes steps below:
1. Open Xcode
Launch Xcode on your Mac computer. You can find Xcode in the Applications folder or by searching for Xcode with Spotlight.
2. Create a New Project
Click on Create a new Xcode project or go to File, New, Project.
3: Choose a Project Template & configure
Select iOS from the available templates on the left side of the window. Then choose App and click Next.
Enter the product name, organization name, and organization identifier in the necessary input fields. You can also select the language and user interface (Swift and Storyboard, SwiftUI, or Objective-C). Then, click Next.
Finally, click Save to save the project.
4. Create the XCUItest Target
Now you can add an XCUItest target to your project. Go to File, New, Target. Select iOS from the available templates. Then, choose Test.
Enter a name for your XCUItest target, ensure it is associated with your main app target.
Once the XCUITest target is created, you will see it listed in the project navigator under the targets section.
UI tests created with XCUITest consist of the following classes:
-
XCUIElement
Every element that you locate in your test extends this class. It offers methods such as
tap
,doubleTap
and more. -
XCUIApplication
This class represents an application on iOS, available to interact with.
-
XCUIElementQuery
Find elements in any app by using a query, for example by using a predicate, or an accessibility identifier.
Automating the Settings Application on iOS
The settings app on iOS, with bundle identifier com.apple.Preferences, is responsible for displaying and editing the settings of an iPhone or iPad.
There are various reasons why you might want to automate the Settings app, for example you might want to:
- Change the Wifi settings
- Change the brightness of your screen
- Change to dark mode or light mode
- Toggle the Airplane mode of your device
Change the Wifi settings
To make sure Wi-fi is enabled on your device, you can open the Settings app programmatically and check for a staticText
element which has the text "Wi-Fi". Then, you can look for a switch
where the label contains the word WiFi.
With XCUITest you can check the value of the switch, if it is set to 0, you can enable WiFi by tapping the switch, as shown in the example below.
let settingsApp = XCUIApplication(bundleIdentifier: "com.apple.Preferences")
settingsApp.launch()
// Make sure wifi is on
if settingsApp.tables.cells.staticTexts["Wi-Fi"].exists {
settingsApp.tables.cells.staticTexts["Wi-Fi"].tap()
let wifiSwitch = settingsApp.switches.containing(NSPredicate(format: "label CONTAINS[c] 'Wi'")).element(boundBy: 0)
if wifiSwitch.value as! String == "0" {
wifiSwitch.tap()
}
}
Change the brightness of your screen
If you'd like to change the display brightness of your iOS device automatically, you can use the XCUITest snippet below.
In the example below, we find the correct setting button by looking for Display & Brightness, then we try to find the slider (UISlider) with a label that contains the word brightness.
We then change the slider's value by using toNormalizedSliderPosition
.
let settingsApp = XCUIApplication(bundleIdentifier: "com.apple.Preferences")
settingsApp.launch()
settingsApp.descendants(matching: .any)["Display & Brightness"].firstMatch.tap()
let brightnessSlider = settingsApp.sliders.containing(NSPredicate(format: "label CONTAINS[c] %@", "Brightness"))
if brightnessSlider.count > 0 {
brightnessSlider.firstMatch.adjust(toNormalizedSliderPosition: 0)
}
Change to dark mode or light mode
There are two options when you'd like to switch from dark mode to light mode on iOS. You can choose to use Appium (WebDriverAgent), or you can use XCUITest. With Appium, you can use mobile: setAppearance
.
In the example below, we'll show you how to make sure your iOS device is always set to light mode with XCUITest.
let settingsApp = XCUIApplication(bundleIdentifier: "com.apple.Preferences")
settingsApp.launch()
settingsApp.descendants(matching: .any)["Developer"].firstMatch.tap()
let value = settingsApp.descendants(matching: .any)["Dark Appearance"].firstMatch.value! as! String
if value == "1" {
settingsApp.descendants(matching: .any)["Dark Appearance"].firstMatch.tap()
}
Toggle the Airplane mode of your device
Enabling or disabling Airplane mode on iOS can be automated with XCUITest. In the example below, we'll enable the first UISwitch that we can find in the Settings app, if it is not already set to enabled.
Using XCUITest to automate the iOS Messages App
With XCUITest you can automate the native iOS messages app (com.apple.MobileSMS
).
You can automatically read, write or delete text messages and iMessages, with the power of XCUITest automation. By using Use XCUITest's typeText()
, you can add keyboard input and type a new message.
You can also use a test script to automate the receiving of messages. For example, write a XCUITest script that polls for new iOS messages and push these to a webhook for further processing.
iOS Photos App automation with XCUITest
The iOS photos app (com.apple.mobileslideshow
), is responsible for managing the media (photos, videos) on your device. All the pictures and videos taken by the iOS device, as well as images and videos received, will be placed in the Photos App of your iPhone or iPad.
Depending on whether the iCloud Photo Library is enabled, all photos and videos generated with your iCloud account will be available in the Photos app.
let photosApp = XCUIApplication(bundleIdentifier: "com.apple.mobileslideshow")
photosApp.launch()
if photosApp.buttons["Library"].exists {
photosApp.buttons["Library"].tap()
} else if photosApp.buttons["Photos"].exists {
photosApp.buttons["Photos"].tap()
}
photosApp.buttons["All Photos"].tap()
let photos = photosApp.images.containing(NSPredicate(format: "label CONTAINS[c] %@", "Photo"))
photos.firstMatch.tap()
The example above will open the Photos app, and tap the first photo in the list. This is a simple example, you could of course expand this code to automatically share the tapped photo with another app. Or remove/edit the photo.
You can create slideshows programmatically, create smart albums, manage Panorama photos or prune the Recently Deleted section.