Title: Appium Automation for Mac OS X Created By: Prithivirajan M Abstract This document aims at providing the necessary information required for setting up mobile testing environment in Mac OS X for testing the Android applications and ios applications using the test automation framework Appium. Following are the steps which covers the basic setup for running the Android applications (.apk) and ios applications (.app) in Emulator (Android only), Simulator (ios only) and also in respective real devices. Introduction One should familiarise with the following terms prior to setting up the environment and running the automation tests in handheld devices or in emulator and simulator. Test Automation: Test automation is use of a program to control the execution of tests, comparison of actual outcomes to predicted outcomes, setting up of test pre-conditions and other test control and test reporting functions. The objective of automated testing is to simplify as much of the testing effort as possible with a minimum set of scripts. A test automation will address the limitations of traditional manual testing and record/playback automation and also will ensure Re-usability, Maintainability, Flexibility, Reliability, Robustness and Portability of the scripts. Appium: Appium is an open source automation testing tool written in Node.js. It is cross platform, hence it allows to write tests in multiple platforms (ios, Android and FirefoxOS). Using Appium native apps, web apps and hybrid apps can be tested in emulators, simulators and real devices. It drives the apps using the Webdriver JSON wire protocol. Native App: These are the apps are the typical apps which we download from either App Store or Google Play, they get installed within your device s applications and you launch them by tapping their icon. Web App: In one line, web apps are the mobile version of websites. Web apps loads within a mobile browser, like Safari or Chrome, like every other website.
Hybrid App: This is the app run on device rather than a browser and are built using the web technologies like HTML5, CSS and Javascript. Emulator & Simulator: The virtual devices which duplicates the features and functionalities in the aim to replicate the behaviours of Actual (Real) devices. Emulators are the virtual devices for the Android and FirefoxOS, while Simulators are for the ios. API Level: API Level is an integer value that uniquely identifies the API revision of Android platform. Appium supports from API level 9 through the latest level. Below table below specifies the API Level supported by each version of the Android platform. Platform Version API Level Version Code Android 5.0 21 LOLLIPOP Android 4.4W 20 KITKAT_WATCH Android 4.4 19 KITKAT Android 4.3 18 JELLY_BEAN_MR2 Android 4.2, 4.2.2 17 JELLY_BEAN_MR1 Android 4.1, 4.1.1 16 JELLY_BEAN Android 4.0.3, 4.0.4 15 ICE_CREAM_SANDWICH_MR1 Android 4.0, 4.0.1, 4.0.2 14 ICE_CREAM_SANDWICH Android 3.2 13 HONEYCOMB_MR2 Android 3.1.x 12 HONEYCOMB_MR1 Android 3.0.x 11 HONEYCOMB Android 2.3.4, 2.3.3 10 GINGERBREAD_MR1 Android 2.3.2, 2.3.1, 2.3 9 GINGERBREAD Android 2.2.x 8 FROYO Android 2.1.x 7 ECLAIR_MR1 Android 2.0.1 6 ECLAIR_0_1 Requirements Android 2.0 5 ECLAIR Android 1.6 4 DONUT Android 1.5 3 CUPCAKE Android 1.1 2 BASE_1_1 Android 1.0 1 BASE Mac OS X 10.7 or higher ADT Bundle for Mac
Eclipse or similar IDEs Appium (recommended latest version) Java 7 or higher Xcode (recommended latest version) Getting Started Emulator 1. Step up ANROID_HOME and PATH variables in.bash_profile as follows : Open terminal cd / touch.bash_profile open.bash_profile export ANDROID_HOME=$HOME/.../sdk/ PATH=$PATH:$HOME/.../build-tools PATH=$PATH:$HOME/.../platform-tools PATH=$PATH:$HOME/.../tools Note: PATH variable is very crucial, must mention "$PATH:" before the value or else it will lead to the loss of other PATH values stored in it. 2. Creating AVD. AVD Manager can be launched through Eclipse. Open Eclipse Window -> Android Virtual Device Manager "Android Virtual Device (AVD) Manager" window will open. Click Create button. "Create new Android Virtual Device (AVD)" window will open. Select appropriate options and click OK. Now the new device will appear on Android Virtual Device Manager. Select the AVD and click Start. In "Launch options" window click Launch. 3. Launching Appium. Open Appium Select Android. In Android options, select the necessary fields. App Path - Path of the app (.apk file) Launch AVD - Select the emulator (Check if running on Emulator)
Automation name - Appium (API Level 17 or higher), Selendroid (API level 16 and below). In Appium options, select Server Address (default- 127.0.0.1) and port (default - 4723). Click Launch button. 4. Coding Open Eclipse This below code will launch the AVD through Appium. java-client jar contains the AppiumDriver class. DesiredCapabilities capabilities = new DesiredCapabilities(); capabilities.setcapability("devicename", "Android"); capabilities.setcapability("platformname", "Android"); driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/ hub"),capabilities); Real Device (Android) 1. Connect the device via USB and set the mobile to "Developer mode" Android version 4.4 or higher: Settings -> About phone -> Version Infos -> Tap build info for 7 times. Android version below 4.3 : Settings -> Developer options -> USB Debugging 2. Step up ANROID_HOME and PATH variables in.bash_profile as follows : 3. Launching Appium. Uncheck the Launch AVD, if checked. Check Device Name and provide the name of device you connected. Get the device name by using below command in Terminal adb devices Select Automation name, Server address and click Launch. 4. Code for Emulator will work for Real device also, no need to modify anything. Simulator 1. Unlike AVD, simulators cannot be created. Simulator should be downloaded by the help of Xcode.
Open Xcode -> Preferences -> Download To launch simulator, Open Xcode -> Open Developer Tools -> ios Simulator Simulator will be launched, to change the devices select Open Simulator -> Hardware -> Device To change the display size of simulator select Open Simulator -> Window -> Scale 2. Launching Appium Open Appium Select ios. In ios options, select the necessary fields. App Path - Path of the app (.app file) Force device - Select the device (Check if running on Simulator) In Appium options, select Server Address (default- 127.0.0.1) and port (default - 4723). Click Launch button. 3. Coding Open Eclipse This below code will launch the Simulator through Appium. DesiredCapabilities capabilities = new DesiredCapabilities(); capabilities.setcapability("devicename","iphone 5"); capabilities.setcapability("platformname", "ios"); driver = new IOSDriver(new URL("http://127.0.0.1:4723/wd/ hub"),capabilities); Real Device (ios) 1. Connect the device via USB. A valid ios Development distribution certificate is required for the device and the app should be signed. For more information on signings, certificates and profiles, please refer Apple documentation. 2. Install ideviceinstaller, below code will install it. brew install --HEAD ideviceinstaller
3. Launch Appium. Uncheck the Force Device, if checked. Check UDID and provide the name of device you connected. Once the device is connected, UDID can be seen in itunes and also in Organiser (Xcode -> Window -> Organiser) Select Server address and click Launch. 4. Coding Open Eclipse Below is the code for launching application in real device DesiredCapabilities capabilities = new DesiredCapabilities(); capabilities.setcapability("devicename","ipad"); capabilities.setcapability("platformname", "ios"); driver = new IOSDriver (new URL("http://127.0.0.1:4723/wd/ hub"),capabilities); Inspecting Elements Inspection of elements are required in order to perform any action on those particular elements. Elements can be inspected through the "Inspector" available in Appium. Some other external tolls available exclusively for inspecting elements (e.g Spark Inspector), which can be downloaded from internet. Xcode also provides a inspector called "Accessibility Inspector" to inspect the elements which can be invoked my following below steps. Open Xcode -> Xcode -> Developer Tools -> Accessibility Inspector Note: If the inspectors are not showing the properties of elements, which means "Enable access for assistive devices and applications" option is disable. Enabling the option can be achieved by below steps. Mac (Apple icon) -> System Preferences -> Security & Privacy -> Privacy -> Accessibility -> Enable Launching and Killing Appium through code Launch Appium
Below java code will launch the Appium CommandLine command = new CommandLine("/Applications/Appium.app/ Contents/Resources/node/bin/node"); command.addargument("/applications/appium.app/contents/resources/ node_modules/appium/bin/appium.js", false); command.addargument("--address", false); command.addargument("127.0.0.1"); command.addargument("--port", false); command.addargument("4723"); command.addargument("--no-reset", false); DefaultExecuteResultHandler resulthandler = new DefaultExecuteResultHandler(); DefaultExecutor executor = new DefaultExecutor(); executor.setexitvalue(1); executor.execute(command, resulthandler); Kill Appium To kill the appium follow the below commands in Terminal lsof -i :4732 lsof - List of open files port - will be the port in which appium is launched Above command will provide the details of given port, in that PID number will be displayed. Use the PID number for the below command. Appium server will be terminated. kill -9 PID number Key Points & Solved Errors Key Points Application's element hierarchy cannot be seen through the inspectors if the developer built the application without enabling the Accessibility option App path given in code (i.e. in capabilities) will always override the app path present in Appium options When selecting CPU/ABI option while creating AVD, its always good to select ARM. Intel Atom chosen AVD takes long time to load or may not load at all in Mac OS X Selecting smaller screen size and lower API Level will make the AVD load faster Prior to java-client-2.0.0 jars, it enables to instantiate AppiumDriver, while in later versions of java-client AppiumDriver was changed into abstract
class. IOSDriver and AndroidDriver can be used with it Best option to connect a device is by selection it as a Media Device (MTP) Shortcut for Home button in Emulator and Simulator: fn + left arrow key (Android) and cmd + shift + H (ios) Solved Errors "Error: Command Failed: error: cannot bind to socket" - When the port is already in use, this error will happen. As mentioned earlier, using the "lsof" and "kill" commands we can make the respective ports free "Error: Unable to install" - when the application was not build using Development Distribution Certificate. This can be done by using Xcode, for that we need source (.xcodeproj) of the application "Error: Unable to install" - Even when the application is build using Development Distribution Certificate, this error will occur when ideviceinstaller is not installed "error: Could not find a device to launch" - platformname and devicename parameters mismatch Have to provide compatible version of operating system for respective devices "Instruments Trace Error : Failed to start trace. Instruments exited with code 253" - The application (.app) is not compiled for simulator or device may be locked and password protected "Timed out waiting for screenshot file" - Sporadic error which occur while capturing the screenshot. Handle this error by building a retry loop while capturing the screen, in next few attempts it will work. References http://appium.io/slate/en/tutorial/ios.html?java# https://github.com/appium/appium https://docs.saucelabs.com/tutorials/appium https://discuss.appium.io/