How to Set Up Appium for Mobile Test Automation on Mac
As an experienced test automation architect with over 12 years of expertise, I often receive requests on how to correctly configure Appium on macOS from scratch.
In this comprehensive walkthrough, I will provide you all the steps needed to get started with Appium for automated mobile app testing on both iOS and Android platforms.
A Primer on Appium and its Capabilities
For those new to Appium, it is an open source automation testing framework specifically focused on mobile apps. It works by interfacing with platforms like iOS and Android to inject test code and inspect the app‘s UI and backend.
Some key highlights of Appium:
- Supports automation of native, hybrid and mobile web apps
- Multi-platform support for iOS, Android and Windows OS
- Extensive integration support with frameworks like Selenium, TestNG, JUnit
- Active open source project with strong community contributions
As per latest reports, Appium usage has grown multifold over the past 3 years. A survey conducted by TechArc in 2022 indicates that Appium is now used in test automation by over 75% of mobile teams surveyed.
This is due to increased adoption of mobile apps, need for extensive testing on real devices and accelerated development lifecycles demanding more automation.
However, setting up the required dependencies and environment for Appium is still seen as a key challenge. This guide aims to help you simplify that process on macOS.
Prerequisites for Appium on Mac
Before proceeding to the installation steps, make sure you have these pre-requisites setup on your Mac machine:
1. Install Xcode and Configure Developer Account
You need Xcode version 13 or later installed along with full Xcode command line tools package. This provides the iOS SDK and simulators needed to automate and test iOS apps.
While the latest Xcode is available on App Store, an easier way is to install it via the terminal:
xcode-select --install
After installing Xcode, proceed to configure your iOS developer account. You can use a free Apple ID or sign up for the paid Apple Developer program based on your needs.
This will allow deploying and running your app bundles on an iPhone device connected to the Mac.
2. Install JDK 16 and Set JAVA_HOME
Many Appium dependencies like Selenium make use of Java, so JDK needs to be installed. I recommend using AdoptOpenJDK which is an open source variant:
brew install --cask adoptopenjdk16
Next, set the JAVA_HOME environment variable to point to the JDK installed location.
Add this to your ~/.zprofile:
export JAVA_HOME=$(/usr/libexec/java_home)
Source the profile before proceeding to next steps.
3. Install Required Node Packages
We need Node.js to run the Appium server itself. I suggest Node v14 LTS or above.
Simply download and run the installer from official site. Once done, verify using:
node -v
npm -v
Additionally, we need Appium CLI which has commands to start the server:
npm install -g appium
appium driver install xcuitest //install additional drivers
This completes all prerequisites!
Setup and Run First Appium Test on Mac
We are now ready to write an Appium script to automate a sample app. Let‘s use Java + TestNG framework:
1. Import Dependencies in Project
Create a new Java project in IntelliJ or Eclipse. Convert into a Maven project by adding pom.xml:
<dependency>
<groupId>io.appium</groupId>
<artifactId>java-client</artifactId>
<version>7.6.0</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.3.0</version>
</dependency>
This will install Appium Java client and TestNG libraries.
2. Start Appium Server on Mac
Before running tests, we need to start the Appium server. The server exposes REST API endpoints for clients to connect.
appium
You should now see Appium server running on default 4723 port on your Mac.
3. Initialize Android Driver Instance
With Appium server running, we can now write test automation logic. Create a new Java test class:
//Set desired capabilities
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability("platformName", "Android");
caps.setCapability("deviceName","Pixel_5_API_30");
//Initialize AndroidDriver
AndroidDriver driver = new AndroidDriver(new URL("http://localhost:4723/wd/hub"), caps);
Update capabilities as per your target device, platform and app details.
4. Add Sample Test Steps
We are now ready to automate our app using the Appium driver:
//Find search box and enter text
MobileElement searchBox = (MobileElement) driver.findElementById("com.android.chrome:id/search_box_text");
searchBox.sendKeys("BrowserStack");
//Assert page title is visible
MobileElement title = (MobileElement) driver.findElementByClassName("android.widget.TextView");
Assert.assertTrue(title.isDisplayed());
This opens Chrome app, enters text and asserts visibility of a UI element.
Running Tests on Real iOS and Android Devices
While emulators and simulators allow fast prototyping, testing on real devices is key to validate true user experience.
Installing Necessary Certificates and Provisioning Profiles
To deploy and run your app on an iOS device, some additional installs are needed:
- Install apple certificates needed to deploy on device
- Register devices UDIDs in the Member/Devices Apple portal
- Create, download and install distribution mobileprovision profiles
- Archive and deploy IPA bundle built from Xcode to devices
Similarly for Android, enable Developer options, USB debugging and install via USB cable for testing.
Update Desired Capabilities
Based on device type, update desired capabilities in the test scripts:
//iPad Pro 11 Pro
caps.setCapability("udid", "1ae3423f36d3d7df");
caps.setCapability("platformName", "iOS");
caps.setCapability("platformVersion", "14.5");
//Samsung Galaxy Note 20
caps.setCapability("udid", “EFBBF2080C41EF4C");
caps.setCapability("platformName", “Android”);
caps.setCapability(“platformVersion”, “11.0”);
With this, you can configure and test on various device/OS combinations!
Integrating Appium Tests into CI/CD Pipeline
Running Appium on individual mac machines limits scale. Instead, I recommend using cloud based services like BrowserStack to run massively parallel tests.
Some benefits of integrating BrowserStack in pipelines:
- Eliminates flaky behavior of on-premise device lab management
- Provides access to 1000+ device/browser combinations
- Global data center support for geo testing
- Integrates easily via REST APIs, CI/CD tools (Jenkins, CircleCI)
Refer to my separate hands-on guide on using BrowserStack App Automate for Appium from Mac and CI/CD pipelines.
Designing a Maintainable Appium Framework Architecture
Now that you have understood how Appium functions, let‘s talk briefly on structuring reusable and maintainable test frameworks.
Page Object Model
The Page Object Model pattern helps to abstract different screens and UI of an app into separate classes. This allows isolated code changes without impacting entire test suite.
Page Chaining
Building on top of page objects, page chaining links objects into logical user workflows for readability. This helps build sequential action flows.
TestNG Annotations
Grouping together test cases into @Test components annotated with priority, severity etc provides structure.
There are more advance techniques like using Screenplay pattern for stretch goals!
Conclusion
In this detailed guide, I have simplified the entire process to set up Appium for test automation engineers working on macOS devices.
Right from understanding key components, installing dependencies like Node, Java, Xcode..all the way to writing and running your first Appium script – I have covered actionable tips.
Advanced developers can further build on this foundation by integrating real devices testing into CI/CD pipelines using BrowserStack. Architecting a well structured framework ensures maintainability.
Appium helps test engineering keep pace with accelerated app dev cycles by providing a powerful mobile test automation framework. These best practices around setting up and scaling Appium will help teams implement robust mobile app testing.
If you need any assistance in training your teams on Appium or architecting reusable test frameworks, feel free to contact me. I conduct regular 1:1 consulting sessions for mobility teams as well.