





















































In this article by Manoj Hans, author of the book Appium Essentials, we will see how toautomate mobile apps on real devices. Appium provides the support for automating apps on real devices. We can test the apps in a physical device and experience the look and feel that an end user would.
(For more resources related to this topic, see here.)
Before starting with Appium, make sure that you have all the necessary software installed. Let's take a look at the prerequisites for Android and iOS:
Prerequisites for Android |
Prerequisites for iOS |
Java (Version 7 or later) |
Mac OS (Version 10.7 or later) |
The Android SDK API, Version 17 or higher |
Xcode (Version 4.6.3 or higher; 5.1 is recommended) |
A real Android device |
An iOS provisional profile |
Chrome browser on a real device |
An iOS real device |
Eclipse |
The SafariLauncher app |
TestNG |
ios-webkit-debug-proxy |
The Appium Server |
Java Version 7 |
The Appium client library (Java) |
Eclipse |
Selenium Server and WebDriver Java library |
TestNG |
The Apk Info app |
The Appium server |
|
The Appium client library (Java) |
|
Selenium Server and WebDriver Java library |
While working with the Android real device, we need to enable USB debugging under Developer options. To enable USB debugging, we have to perform the following steps:
To ensure that we are ready to automate apps on the device, perform the following steps:
You will get a list of Android devices; if not, then try to kill and start the adb server with the adb kill-server and adb start-server commands.
Now, we've come to the coding part. First, we need to set the desired capabilities and initiate an Android/iOS driver to work with Appium on a real device. Let's discuss them one by one.
We have two ways to set the desired capabilities, one with the Appium GUI and the other by initiating the desired capabilities object.
Using the desired capabilities object is preferable; otherwise, we have to change the desired capabilities in the GUI repeatedly whenever we are testing another mobile app.
Let's take a look at the Appium GUI settings for native and hybrid apps:
Perform the following steps to set the Android Settings:
If the application is already installed on the real device, then we don't need to follow steps 2-4. In this case, we have to install the Apk Info app on the device to know the package and activities of the app and set them using the desired capabilities object, which we will see in the next section. You can easily get the 'Apk info' app from the Android Play Store.
Perform the following steps to set the Android Settings for web apps:
Let's discuss how to initiate the desired capabilities object and set the capabilities.
Here we will directly dive into the code with comments. First, we need to import the following packages:
import java.io.File;
import org.openqa.selenium.remote.DesiredCapabilities;
import io.appium.java_client.remote.MobileCapabilityType;
Now, let's set the desired capabilities for the native and hybrid apps:
DesiredCapabilities caps = new DesiredCapabilities();//To create an object
File app=new File("path of the apk");//To create file object to specify the app path
caps.setCapability(MobileCapabilityType.APP,app);//If app is already installed on the device then no need to set this capability.
caps.setCapability(MobileCapabilityType.PLATFORM_VERSION, "4.4");//To set the Android version
caps.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");//To set the OS name
caps.setCapability(MobileCapabilityType.DEVICE_NAME,"Moto X");//You can change the device name as yours.
caps.setCapability(MobileCapabilityType.APP_PACKAGE, "package name of your app (you can get it from apk info app)"); //To specify the android app package
caps.setCapability(MobileCapabilityType.APP_ACTIVITY, "Launch activity of your app (you can get it from apk info app)");//To specify the activity which we want to launch
In Android mobile web apps, some of the capabilities that we used in native and hybrid apps such as APP, APP PACKAGE, and APP ACTIVITY are not needed because we launch a browser here. First, we need to import the following packages:
import java.io.File;
import org.openqa.selenium.remote.DesiredCapabilities;
import io.appium.java_client.remote.MobileCapabilityType;
Now, let's set the desired capabilities for the web apps:
DesiredCapabilities caps = new DesiredCapabilities();//To create an object
caps.setCapability(MobileCapabilityType.PLATFORM_VERSION, "4.4");//To set the android version
caps.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");//To set the OS name
caps.setCapability(MobileCapabilityType.DEVICE_NAME,"Moto X");//You can change the device name as yours
caps.setCapability(MobileCapabilityType.BROWSER_NAME,"Chrome"); //To launch the Chrome browser
We are done with the desired capability part; now, we have to initiate the Android driver to connect with the Appium Server by importing the following packages:
import io.appium.java_client.android.AndroidDriver;
import java.net.URL;
Then, initiate the Android driver as shown here:
AndroidDriver driver = new AndroidDriver (new URL("http://127.0.0.1:4723/wd/hub"), caps);//TO pass the url where Appium server is running
This will launch the app in the Android real device using the configurations specified in the desired capabilities.
Before moving on to the desired capabilities for iOS, we have to make sure that we have set up a provisional profile and installed the SafariLauncher app and ios-webkit-debug-proxy to work with the real device.
First, let's discuss the provisional profile.
This profile is used to deploy an app on a real iOS device. To do this, we need to join the iOS Developer Program (https://developer.apple.com/programs/ios/).
For this, you will have to pay USD 99. Now, visit https://developer.apple.com/library/ios/documentation/IDEs/Conceptual/AppDistributionGuide/MaintainingProfiles/MaintainingProfiles.html#//apple_ref/doc/uid/TP40012582-CH30-SW24 to generate the profile.
After this, you will also need to install provisional profile on your real device; to do this, perform the following steps:
The SafariLauncher app is used to launch the Safari browser on a real device for web app testing. You will need to build and deploy the SafariLauncher app on a real iOS device to work with the Safari browser. To do this, you need to perform the following steps:
Now, we need to install the ios-webkit-debug-proxy on Mac to establish a connection in order to access the web view. To install the proxy, you can use brew and run the command brew install ios-webkit-debug-proxy in the terminal (this will install the latest tagged version), or you can clone it from Git and install it using the following steps:
cd ios-webkit-debug-proxy ./autogen.sh ./configure make sudo make install
We are now all set to test web and hybrid apps.
In this article, we looked at how we can execute the test scripts of native, hybrid, and web mobile apps on iOS and Android real devices. Specifically, we learned how to perform actions on native mobile apps and also got to know about the desired capabilities for real devices. We ran a test with the Android Chrome browser and learned how to load the Chrome browser on an Android real device with the necessary capabilities.
We then moved on to starting the Safari browser on a real device and setting up the desired capabilities to test web applications. Lastly, we looked at how easily we can automate hybrid apps and switch from native to web view.
Further resources on this subject: