Setup iOS Test Automation Using WebDriverIO, XCode and Appium [Simulator Devices]

Setup iOS Test Automation Using WebDriverIO, XCode and Appium [Simulator Devices]




iOS test automation is a valuable testing technique for ensuring the quality of your iOS apps and is the process of using automated scripts to test iOS apps. This can be done on real devices or on simulators.
There are many benefits to using iOS test automation, including:
  • Increased speed and efficiency: Automated tests can be run much faster than manual tests, which can save you a lot of time.
  • Improved accuracy: Automated tests are less error-prone than manual tests, which can help you ensure the quality of your app.
  • Reduced maintenance costs: Once you have created automated tests, you can reuse them for future releases of your app.
Setup-iOS-Test-Automation-001
There are many different ways to set up iOS test automation, but Xcode and Appium are two popular options. In this blog post, I will show you how to set up iOS test automation using Xcode and Appium. I will also show you how to run automated tests on simulator devices. As, simulator devices are a great way to test your iOS apps on a variety of devices without having to purchase them all.
Setup-iOS-Test-Automation-002

Prerequisites

1. Installing Node JS

Download and install Node JS depending on your operating system. To check the node version, use the following command on terminal:
node -v

2. Installing Java JDK and Setting up JAVA_HOME Environment Variable

Download and install Java JDK depending on your operating system . Open the terminal shell, and to return where was the SDK installed use the following command:
/usr/libexec/java_home
It should return something like this
Setup-iOS-Test-Automation-003
If you want to check the java version you can use:
/usr/libexec/java_home -V
We can set the $JAVA_HOME environment variable in either ~/.zshenv or ~/.zshrc.
a. Open the ~/.zshenv
nano ~/.zshenv
b. Add the following content
export JAVA_HOME=$(/usr/libexec/java_home)
c. Source the file and print the $JAVA_HOME.
source ~/.zshenv 
echo $JAVA_HOME
It should return something like this
Setup-iOS-Test-Automation-004

3. Setting Android_Home Environment Variable

a. Install Android studio on your device.
b. Open the zshenv file to insert the ANDROID_HOME variable:
nano ~/.zshenv
c. Add the following content
export ANDROID_HOME="/Users/[USER]/Library/Android/sdk"
export PATH=$ANDROID_HOME/platform-tools:$PATH
export PATH=$ANDROID_HOME/tools:$PATH
d. Source the file and print the $ANDROID_HOME.
source ~/.zshenv 
echo $ANDROID_HOME
It should return something like this
Setup-iOS-Test-Automation-005
e. With this configured, you can access the command Android Debug Bridge.
adb

4. iOS Setup

a. Install XCode from the macOS App Store. For this your macOS Ventura should be 13 or later.
b. Verify that you’ve successfully installed XCode Command Line Tools:
xcode-select -p
Executing this command should return something like:
Setup-iOS-Test-Automation-006
c. Install Carthage (It is a simple dependency manager for macOS and iOS, created by a group of developers from GitHub).
brew install carthage
Setup-iOS-Test-Automation-007

5. Appium Server GUI Setup

a. Install Appium GUI from github.
b. Run the following commands in the Terminal
npm install -g appium@next
c. Check the Appium version using
appium -v
Now open the app, and set the following configurations
Setup-iOS-Test-Automation-008
Setup-iOS-Test-Automation-009

6. Appium Doctor

To check if your OS meets the Appium requirements, install this node (Appium Doctor Package) package.
Install it using the command
npm install appium-doctor -g
And then use the library:
appium-doctor
Setup-iOS-Test-Automation-010

7. Appium Drivers

Now you need to download and have the iOS drivers in your system.
Run this command
appium driver install xcuitest
Check whether the driver is installed correctly by using following command:
appium driver list

8. Install idevice Installer

ideviceinstaller is an application that allows the iOS device to install, upgrade, uninstall, archive, restore and enumerate apps. It can be installed using
brew install ideviceinstaller

9. iOS Desired Capabilities [Simulator App]

Setup-iOS-Test-Automation-011
  1. Open finder and search for WebDriverAgent.xcodeproj.
  2. Open WebDriverAgent.xcodeproj in Xcode.
  3. Go to XCode and click on the settings.
  4. In the Accounts tab, click + sign
  5. Select Apple ID and click continue
  6. Enter your Apple ID and password and click on Next.
  7. Close the window after you've successfully added the Apple ID.
  8. Now click on the WebDriverAgent Project and click on the “WebDriverAgentLib”.
  9. Click on “Build Settings” and scroll down to “Packaging”.
  10. In Packaging, rename the “Product Bundle Identifier” name to something unique “hms012”. So, it becomes like com.hms012.WebDriverAgentLib.
  11. Now, click on “Signing&Capabilities”, select Automatically manage signing.
  12. In team, select the name of your team that you created at Step 6.
  13. Select Development in Signing Certificate. It’ll generate a signing certificate for you.
Now select WebDriverAgentRunner”and repeat the steps from 9 to 13.
Setup-iOS-Test-Automation-012
Setup-iOS-Test-Automation-013
Note
Setup-iOS-Test-Automation-014
If you encounter error like
Setup-iOS-Test-Automation-015
Then you need to change the string “hms012” to something else. It should be unique.
And if this error appears
Setup-iOS-Test-Automation-016
then unfortunately you’ll have to wait till said days. This problem appears when you have a free developer account.
Setup-iOS-Test-Automation-017
If you encounter this error “Xcode couldn’t find any iOS App Development provisioning profiles matching ‘com.hms012.WebDriverAgentRunner’.”
or errors like
Setup-iOS-Test-Automation-018
Then follow these steps to solve it:
  1. Check the signing and capabilities for webdriveragentrunner and webdriveragentlib, it should be signed in using a proper developer account.
  2. Try running the tests again and check if the error isn’t there anymore. In case the same error still appears you need to do the next steps mentioned below.
  3. . In such cases you can download the latest WebDriver Agent from ‘https://github.com/appium/WebDriverAgent/releases 30
  4. Then delete all files except Build folder from appium gui’s webdriver agent folder.
  5. From the downloaded folder you can copy all files and paste inside the appium gui’s webdriver agent folder.
It should work!!

10. Appium Inspector Setup

Install Appium Inspector.
  • For setup, you can use the following configuration:
    
    Setup-iOS-Test-Automation-019
  • iOS Desired Capabilities (Simulator — App)
    
    Setup-iOS-Test-Automation-020
  • For “appium:app”, you need to ask your mobile developers for the “IPA” file of your app.
  • For “appium:udid”, you can either go to XCode. Click on your simulator and copy the “Identifier” path or you can run the following command in the terminal:
xcrun xctrace list devices
It will give you the list of emulators and their Identifier path.
Finally, start the Session.
Now I will configure the environment to run tests using WebDriverIO, and set up the Appium capabilities in the project.

11. Setup WebDriverIO

  1. Run the command in the terminal to create the package.json & continue with the installation process
    npm init wdio .
  2. Using the WDIO Configuration Helper select the options you want to select. In my case I decided to use:
    
    Setup-iOS-Test-Automation-021
  3. Install all the system requirements
    npm install wdio
    npm install webdriverio --save
    npm i @wdio/cli
  4. Install Appium in your project
    npm install --save-dev appium@next
  5. Check if the drivers are still available by using the following command:
    appium driver list
  6. If drivers are not available then install them again:
    appium driver install xcuitest
  7. Install Appium Server, Appium Plugin, and then run Appium Doctor using this command:
    npx appium-installer
  8. Add your feature files at
    './[yourProject]/features/**/*.feature'
  9. Add your tests at
    './[yourProject]/specs/**/*.js'
  10. Set up the capabilities for iOS(Emulator sample) at wdio.conf.js
    capabilities: [{
           platformName: 'iOS',
           maxInstances: 1,
           'appium:deviceName': 'iPhone 14 Pro',
           'appium:platformVersion': '16.4',
           'appium:automationName': 'XCUItest',
           "appium:bundleID": "com.sampleapp.app",
           "appium:udid": "sample ID of your Emulator app",
           'appium:noReset': true,
           "appium:waitforTimeout": 30000,
           "appium:newCommandTimeout": 7200,
           "appium:app": "iosAppPath"
       }],
  11. 11. Run your scripts using
    npx wdio
You should now be able to execute your tests.
I hope you find this blog helpful!! 🙂