21
2017
Test Automation of Mobile Applications using Appium
Appium is a cross platform mobile test automation tool that does not need any alterations or access to the source code when test automation is been performed.
Mobile usage has surpassed the traditional desktop and laptop usage and quite naturally mobile applications have exploded in terms of numbers. Hence, following this trend, it has become ever more important to automate mobile application testing.
Today, different test automation tools like MonkeyRunner, Robotium, KIF, Calabash, MonkeyTalk, Appium and Frank are available in the market for this purpose. Most of the above mentioned tools require an extra agent to be compiled with the application code, so that these tools can interact with the mobile application when run on test mode.
However, the problem is that automation agent libraries need to be removed before they are submitted to the application specific app stores. This means that the app which you will be testing will not be identical to the same app that would be submitted to the app store.
In contrast, Appium is a cross platform mobile test automation tool that does not need any alterations or access to the source code when test automation is been performed.
What is Appium?
Appium is an open-source test automation framework that can be used with native, hybrid and mobile web applications on iOS and Android platforms.
Appium is a mobile test automation tool developed and supported by Sauce Labs. It has been around since 2012 under the direction of various individuals and organizations and it has been implemented in 3 different programming languages. It was originally developed as a way to take advantage of the UIAutomation framework for Apple iOS to run tests against native mobile applications by Dan Cueller. Appium uses the same syntax as Selenium and it shares Selenium’s ability to automate interaction with a website through a mobile browser. In addition to that, it provides a way to interact with elements that are specific to mobile applications, such as gestures. It is hosted with GitHub.
Features of Appium
1.Multiple application support
Appium allows native, hybrid and web application test automation and supports automation on simulators, emulators or on physical devices.
2.Cross platform support
Appium allows you to write tests against multiple platforms using the same API. This enables code reuse between iOS and Android test suites. At present Appium only supports Android and iOS based mobile applications, but support for Firefox mobile OS is also in the pipeline.
3.Multiple client libraries
Appium supports all the WebDriver client libraries, therefore it supports all the webdriver support languages as well. It allows writing and running automation tests in any language that can create an HTTP request such as Java, C#, PHP, Ruby, Python, Perl and Objective-C.
4.Common API
Appium extends webdriverJsonWireProtocol with some API specific for mobile purposes. It enables cross-platform mobile app testing by using a common API for both iOS and Android platform test scripts. It has added mobile specific functions like driver.zoom(),driver.pinch(), driver.currentActivity(), driver.lock() etc.
Appium Architecture
Appium is known as an HTTP server written in node.js. It has the ability to create and handle multiple WebDriver sessions for different platforms (Android and iOS).
Appium client includes:
- A collection of client libraries for different scripting languages which you will use to write your test scripts based on the Selenium WebDriver API.
Appium Server includes:
- A server component based on node.js and it exposes the WebDriver API (a superset of the WebDriver API called Mobile JSON Wire Protocol).
- A desktop application is available for both OS X and Windows. That includes everything you need to run Appium bundled in a one package and facilitates the inspection of elements in running applications.
How Appium works?
Image source: https://testingmobileapps.files.wordpress.com/2016/04/appium.png?w=663
The basic and high level work-flow of Appium is as follows:
- Test scripts written in different languages are running from the test machine.
- A test server running Appium is configured and made available on a certain network location.
- Devices, emulators or simulators are connected to the test server.
- When a test is executed from the Appium server, the code will simply perform HTTP requests to the server. Then the server will translate those meta-commands into device-specific actions sent to the device to test. Those commands are user interactions with the application like swiping on the screen, clicking an interface component, scrolling onto a particular area or performing a specific gesture.
- Thanks to Google’s JSON, Appium is able to have tests send commands to devices through the test server to achieve multi-platform testability.
The client/server pattern of the Appium allows in achieving multi-platform testability. Once a test is written, it is executed in the client/test station. To interact with iOS and Android with Selenium WebDriver, Appium uses the JSON wire protocol. It is this interaction between node.js server and Selenium client libraries which works together with the mobile application. One of the core features of Appium is that the test codes can be written in any framework or language without having to modify them.
How Appium works on Android
Image source: http://executeautomation.com/blog/wp-content/uploads/2015/07/071415_2325_HowAppiumWo1.png
In Android, Appium works as follows:
- Appium client connects with Appium Server and communicates via JSON Wire Protocol.
- Appium Server then creates an automation session for the client and also checks the desired capabilities of the client and connects with respective vendor-provided frameworks like Selendroid/UIAutomator (based on android versions).
- Vendor-provided frameworks will then communicate with Emulator/Real device for performing client operations.
- Vendor-provided frameworks will then communicate with bootstrap.jar which is running in the Emulator/Real device for performing client operations.
- Bootstrap.jar acts as a TCP server to perform actions on the AUT (Application Under Test).
- After performing actions, the server sends an HTTP response back to the client.
How Appium works on iOS
Image source: http://executeautomation.com/blog/wp-content/uploads/2015/07/071415_2325_HowAppiumWo2.png
In iOS, Appium works as follows:
- Appium client connects with Appium Server and communicates via JSON Wire Protocol.
- Appium Server then creates an automation session for the client and also checks the desired capabilities of the client and connects with respective vendor-provided framework UIAutomation.
- UIAutomation will then communicate with bootstrap.js which is running in Simulator/Real device for performing client operations.
- Bootstrap.js will perform action on the AUT Xcode.
- After performing the actions, server sends an HTTP response back to the client.
Philosophy
Appium was designed to meet mobile automation needs according to a philosophy outlined by the following four tenets:
1. Test the same app you submit to the marketplace
You shouldn’t have to recompile your app or modify it in any way in order to automate it.
2. Write your tests in any language, using any framework
You shouldn’t be locked into a specific language or framework to write and run your tests.
3. Use a standard automation specification and API
A mobile automation framework should not reinvent the wheel when it comes to automation APIs.
4. Build a large and thriving open-source community effort
A mobile automation framework should be open source, in spirit and practice as well as in name!
Appium’s Advantages
- Appium is a free and open source tool, so anyone can use it for mobile test automation.
- Appium is developed and supported by Sauce Labs and it is getting picked really fast within the WebDriver community for mobile automation.
- Appium’s backend is Selenium, so you will get all Selenium functionality. Commands used in Appium are very similar to Selenium commands.
- It supports multiple languages like Java, C#, Ruby, Python, JavaScript, PHP etc.
- Appium opens the door for cross-platform mobile testing, which means the same test would work on multiple platforms.
- It can automate Web, Hybrid and Native mobile applications.
- The beauty of Appium is that, all the complexities are under the hood of the Appium server and for an automation developer, the programming language and the whole experience would remain the same irrespective of the platform been automated. (iOS or Android).
- Unlike other tools Appium does not require you to include some extra agents in your app to make it automation friendly. It believes in the philosophy of testing the same app which would be submitted to the app store. Commonly used programming API’s can be integrated.
- It handles simulators, emulators and real devices.
Appium’s Drawbacks
- Cannot run scripts on multiple iOS simulators at the same time. In iOS we can only run one instance on Instruments per Mac OS so we can only run iOS scripts on one device per Mac machine. So, if the tests are to be executed on multiple iOS devices at the same time, then the same number of Mac machines would need to be arranged, which would be a costly affair.
- Appium Inspector is not supported on Microsoft Windows.
- Appium uses UIAutomator for Android automation which only supports Android SDK Platform, API 16 or higher. To support the older APIs they have used another open source library called Selendroid. Hence, it is definitely an overhead on the configuration side.
- Lack of image recognition: Images cannot be located with Appium’s inspector. To work with images, the tester has to work with screen coordinates, which is not considered as the best approach.
- Documentation is scarce with limited number of good tutorials available.
- Appium requires are longer configuration time for both android and iOS.
- Too many unexpected errors including disappearing node.exe.
- No direct support for Android Alert Handling as it’s not implemented yet for native apps via Alert API.
- Some of the gesture mobile gesture supports are not implemented as yet, such as double-clicking in the Java client library, but they are implemented in other client libraries.
- Appium is still in the developing stages and it cannot be recommended for big projects initially.
How to run an Appium Automated Test?
First the following pre-requisites are to be met:
- Install the Java Development Kit (JDK)
- Set up the Java environment variable path
- Install Android SDK / ADB on Windows
- Set up the Android environment variable
- Download and install NodeJs
- Install Microsoft .net Framework
- Download and Install Appium desktop client
- Enable Developer Mode options on Android phone or tablet
- Install PdaNet to connect with an Android device
Then an IDE needs to be set up the project. This can be Eclipse, Visual Studio (If coding in C#) or Xcode (for iOS devices).
You can write the test scripts in any language which is supported by the selected IDE. A sample test script would look like the following script:
packageriksAuto; importio.appium.java_client.android.AndroidDriver; importjava.io.File; importjava.net.MalformedURLException; import java.net.URL; importjava.util.concurrent.TimeUnit; importorg.openqa.selenium.By; importorg.openqa.selenium.remote.CapabilityType; importorg.openqa.selenium.remote.DesiredCapabilities; public class riksAutomation { private static AndroidDriver driver; public static void main(String[] args) throws MalformedURLException, InterruptedException { File classpathRoot = new File(System.getProperty("user.dir")); File appDir = new File(classpathRoot, "/apps"); File app = new File(appDir, "riks.apk"); DesiredCapabilities capabilities = new DesiredCapabilities(); capabilities.setCapability(CapabilityType.BROWSER_NAME, ""); capabilities.setCapability("deviceName", "Auto_Test"); capabilities.setCapability("platformVersion", "4.4"); capabilities.setCapability("platformName", "Android"); capabilities.setCapability("app", app.getAbsolutePath()); capabilities.setCapability("appPackage","lk.mazarin"); capabilities.setCapability("appActivity",".activity.MainTabActivity"); driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities); driver.manage().timeouts().implicitlyWait(200, TimeUnit.SECONDS); driver.findElement(By.name("Transactions")).click(); Thread.sleep(500); driver.findElement(By.id("lk.mazarin:id/login_username")).clear(); driver.findElement(By.id("lk.mazarin:id/login_username")).sendKeys("190301230355"); driver.findElement(By.id("lk.mazarin:id/login_password")).clear(); driver.findElement(By.id("lk.mazarin:id/login_password")).sendKeys("4ekN9MF9"); driver.findElement(By.id("lk.mazarin:id/login_button")).click(); driver.findElement(By.id("android:id/button1")).click(); driver.findElement(By.id("lk.mazarin:id/transactions_from_date")).click(); driver.findElement(By.id("android:id/decrement")).click(); driver.findElement(By.id("android:id/decrement")).click(); driver.findElement(By.id("android:id/decrement")).click(); driver.findElement(By.id("android:id/button1")).click(); driver.findElement(By.id("lk.mazarin:id/transactions_submit_button")).click(); Thread.sleep(2000); driver.findElement(By.name("Check Balance")).click(); Thread.sleep(1000); driver.findElement(By.id("lk.mazarin:id/check_balance_button")).click(); Thread.sleep(1000); driver.findElement(By.name("Block Card")).click(); Thread.sleep(1000); driver.quit(); } }
When the test script is ready, Appium Node server has to be launched and run the test.
Running Appium server
- Simply run the Appium Server by double-clicking the Appium icon (assuming the desktop shortcut is available) and notice the following:
1. Android icon is for configuration settings
2. Settings icon is for general settings
3. Play icon is for starting Node server
- Clicking on the Android icon on the above will open up the configuration settings. If Appium doesn’t have to be installed and uninstalled for every test run, simply check ‘No reset’ in settings.
Figure: General settings window
- Click ‘General Settings’ to check if the server and port values are loaded accordingly.
Figure: Android settings window
- Now launch Appium server by clicking the play icon which is shown in the first image. The Appium server will start and run. When it shows ‘Console LogLevel:debug’, the test script is ready for execution.
Figure: Appium console
The test script can be run just like any other code as per the IDE that has been used. Make sure that the device to be used (physical device or the emulator) is enabled for USB debugging.
Example: If the project is setup on Eclipse in Java,
Run > Run As > Java Application or Right Click on Eclipse code and Click Run As > Java Application.
Observe that Appium server logs all the information including errors on the console.
Locating Elements
Using the elements in the test script in Appium is much similar to Selenium Web Driver.
The elements can be located By ID, By Name, By Class Name, By Xpath etc. But to find the properties of the elements of the applications, the following method is required,
- For Native apps UiAutomatorViewer or Appium Inspector can be used.
- For Hybrid apps Google chrome remote debugger can be used, because the above tools are not working for elements in WebView.
How to use ‘uiautomatorviewer’?
- Run the mobile app under testing from the device.
- To use ‘uiautomatorviewer’, open it from the path where the android SDK is installed Android -> android-sdk -> tools -> uiautomatorviewer.bat.
- The app screen will be loaded as on the device screen and screen shots can be taken from ‘uiautomatorviewer’ which will then allow each element displayed on the screen to be clicked and the particular attributes of the element would be displayed on the right.
Figure: UI Automator Viewer
How to use Google Remote Debugger?
- Type “chrome://inspect/#devices” in the chrome browser.
Figure: Devices list in Chrome browser
- Open the app with enabled webview debugger in device listed with adb.
Figure: Getting the element attributes
- Then by clicking on the inspect link, the inspect elements in the webview can be found.
Using the above methods the attributes of the elements of the native or hybrid mobile applications can be found. Considering many aspects of mobile test automation, Appium is a tool worth trying for any organization if they are look for mobile test automation. Finding an open source tool which works for both native and hybrid applications is not an easy task, but Appium becomes a smart solution in many ways.
References:
- Appium official website – http://appium.io/introduction.html
- Appium – Sauce labs website – https://saucelabs.com/appium
- Jonathan Lipps’ talk for GTAC 2013 on Appium, an open source framework for mobile app testing (presentation).
Authors:
- Sassri Siriwardena
- Mahesh Silva