How to automate IOS app with Appium?
By: Deepali Kalagate
Tags: iOS, iOS App Development, Appium

How to automate IOS app with Appium?

What is Appium?

Appium is an Open source, Cross Platform test automation tool for mobile apps

Maintained by Dan Cuellar, Jonathan Lipps and a number of other contributors

Supports automation of Native, Hybrid and Mobile Web apps

Based on Webdriver JSON wire protocol

Based on Client-Server Architecture

Appium Server written in Node.js

Part 1: Environment Setup

Step 1: Install Java Development Kit (JDK)

To install JDK 1.8, open your terminal and type the following:

$ brew tap adoptopenjdk/openjdk $ brew cask install adoptopenjdk8

Now that JDK 1.8 is installed, we have to set the environment variable JAVA_HOME.

Open the file ~/.bash_profile (or ~/.zshrc if you use zsh), add the following: export JAVA_HOME=`/usr/libexec/java_home -v 1.8` export PATH=$JAVA_HOME/bin:$PATH

Step 2: Install Xcode and Simulators

You may likely already have Xcode installed on your Mac. But if you don’t, open the App Store app and install Xcode.

Once Xcode is installed, it comes with a list of simulators to use. To check the list of simulators and their IOS versions, go to Xcode -> Window -> Devices and Simulators

If you wish to use simulators with a different IOS version, go to Xcode -> Preferences -> Component, and install the desired version of simulator.

Step 3: Install Appium

We need to run Appium server to connect to IOS device and send UI tests commands over to instruct the device to perform actions.

Appium can be simply installed via NPM. Open your terminal and type the following:

$ npm install -g appium

Step 4: Install IntelliJ IDE (Free Community Edition)

We will use IntelliJ to create our UI tests maven project. IntelliJ is an Integrated Development Environment (IDE) developed by JetBrains. Its Community Edition is free to use and powerful enough that provides rich features to write, compile and run Java code.

Download and install IntelliJ (Community Edition) from here: https://www.jetbrains.com/idea/download

Part 2: Write and Run automated UI tests

With the above set up, we should have a complete environment for running automated UI tests. Now let’s look at how to write our UI tests and run them.

Step 1: Create a simple IOS app using React Native

Before we can write and run our UI tests, we need an IOS app that we can interact with to run automated UI tests. If you already have an IOS app you want to run UI tests against, you can skip to next step.

In this step, we will create a simple IOS app that we will run UI tests on, using React Native. React Native is a popular open-source mobile application framework. It can be used to develop cross-platform applications for IOS, Android, and Web.

Follow the instructions below to create a simple React Native app:

1. First, we need to install react-native-cli:

$ npm install -g react-native-cli

2. Then we can create a simple React Native app using react-native command. ToDoApp is an example name for the React Native app. Feel free to change it to a name of your choice.

$ react-native init ToDoApp

3. At last let’s run the app and open the app in IOS simulator.

In the current terminal window, start the react-native server:

$ cd ./ToDoApp $ react-native start

Open another terminal window, and cd into the ToDoApp directory, run the following to run the IOS app:

$ react-native run-ios

It will start building the IOS app bundle and automatically open up an IOS simulator to install and launch the IOS app.

4. In your terminal output, you should see Build Succeed and it’s then Installing and Launching the app as below:

▸ Build Succeeded

info Installing

“/Users/anonymous/Library/Developer/Xcode/DerivedData/AppiumUITestsExampleApp-ctzpwtembojftqdlmhpsholpwycs/Build/Products/Debug-iphonesimulator/AppiumUITestsExampleApp.app”

info Launching “org.reactjs.native.example.AppiumUITestsExampleApp”

Take note of where it’s installing the app bundle from

/Users/anonymous/Library/Developer/Xcode/DerivedData/AppiumUITestsExampleApp-ctzpwtembojftqdlmhpsholpwycs/Build/Products/Debug-iphonesimulator/ToDoApp.ipa(your app bundle file path might be different).

We will need to use this app bundle file path later to run automated UI tests.

By this step, you should be able to see an IOS simulator running the example React Native app like below:

Step 2: Create UI tests gradle project

Maven is a popular build automation tool used for building and managing Java projects. We will create our UI tests project as a maven project.

1. Open IntelliJ, go to File -> New -> Project, which opens up a dialog window to create a new project.

2. Select gradle on the left panel, and set Project SDK on the top to be 1.8 (the JDK we installed earlier), and click Next.

3. On the next panel, for Name field, type AppiumIOSUITestsTutorial . Set Location to be the directory you want to save this project. Leave the rest and click Finish.

Step 3: Install package dependencies

Next, we need install a list of package dependencies that are needed to write, compile and run Appium UI test.

1. Open the file build.gradle, and add the below

Step 4: Write simple UI automation tests

Now that we have an IOS app and a UI tests maven project set up, we can start writing simple UI automation tests to run against the IOS app.

1. In IntelliJ, create a file BaseTest.java under src/main/java directory.

2. In BaseTest.java file, paste the following code:

Let’s take a closer look at what’s going on here:

http://127.0.0.1:4723/wd/hub is the localhost url where Appium server will be running. Our UI tests will establish a connection to the Appium server url once running.

Then we need to set a list of capabilities, which we use to tell Appium how to set up our UI tests. See more about Appium desired capabilities.

AUTOMATION_NAME tells Appium the type of automation engine we want to use. XCUITest is the driver for IOS tests.

DEVICE_NAME specifies a name for the device. It’s more of a display name to distinguish itself.

PLATFORM_NAME specifics the mobile platform.

PLATFORM_VERSION specifics the mobile OS version.

APP specifies the path to the app we wish to use. It could be an absolute local file path or remote http url. Do remember to modify MobileCapabilityType.APP to be your own IOS app bundle file path that was created in Step 1.

Last we initialize an IOSDriver using the Appium server url and capabilities. We will be using this IOS driver to interact with our device and app.

Step 5: Run automated UI tests on IOS simulator

With above UI tests written, we are now ready to run our automated UI tests against the IOS app we created on IOS simulator.

1. First, let’s launch the Appium server. Open your terminal and type the following:

$ appium

This will start the Appium server in the foreground, and the server is listening and waiting on any connection from UI tests.

2. Next, we will run the UI tests from IntelliJ. Right click on the file AppUITests.java, select Run ‘AppUITests’, and it should start running the TestNG tests in the file.

3. In the terminal, you should see Appium server is establishing a connection with the automated UI tests, and as instructed opening up an IOS simulator, installing our IOS app bundle, and executing the automated UI tests against the installed IOS app.

4. Meanwhile, you will see an IOS simulator being opened up and our AppiumUITestsExampleAppIOS app being installed and launched a few times. Our automated UI tests run in IntelliJ will also finish successfully.

Congratulations!!! You have successfully created a React Native IOS app and automated UI tests to interact with the app on IOS simulator.