Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds

Appium Essentials

Save for later
  • 540 min read
  • 2015-04-09 00:00:00

article-image

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.)

Important initial points

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:

  1. Navigate to Settings | About Phone and tap on Build number seven times (assuming that you have Android Version 4.2 or newer); then, return to the previous screen and find Developer options, as shown in the following screenshot:

    appium-essentials-img-0

  2. Tap on Developer options and then tap on the ON switch to switch on the developer settings (You will get an alert to allow developer settings; just click on the OK button.). Make sure that the USB debugging option is checked, as shown here:

    appium-essentials-img-1

  3. After performing the preceding steps, you have to use a USB cable to connect your Android device with the desktop. Make sure you have installed the appropriate USB driver for your device before doing this. After connecting, you will get an alert on your device asking you to allow USB debugging; just tap on OK.

To ensure that we are ready to automate apps on the device, perform the following steps:

Unlock access to the largest independent learning library in Tech for FREE!
Get unlimited access to 7500+ expert-authored eBooks and video courses covering every tech area you can think of.
Renews at $19.99/month. Cancel anytime
  1. Open Command Prompt or terminal (on Mac).
  2. Type adb devices and press the Enter button.

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.

Desired capabilities for Android and initiating the Android driver

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:

appium-essentials-img-2

Perform the following steps to set the Android Settings:

  1. Click on the Android Settings icon.
  2. Select Application Path and provide the path of the app.
  3. Click on Package and choose the appropriate package from the drop-down menu.
  4. Click on Launch Activity and choose an activity from the drop-down menu.

    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.

  5. Select PlatformVersion from the dropdown.
  6. Select Device Name and type in a device name (For example, Moto X).
  7. Now, start the Appium Server.

Perform the following steps to set the Android Settings for web apps:

  1. Click on the Android Settings icon.
  2. Select PlatformVersion from the dropdown.
  3. Select Use Browser and choose Chrome from the dropdown.
  4. Select Device Name and type in a device name (For example, Moto X).
  5. Now, start the Appium Server.

appium-essentials-img-3

Let's discuss how to initiate the desired capabilities object and set the capabilities.

Desired capabilities for native and hybrid apps

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

Desired capabilities for web apps

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.

Installing provisional profile, SafariLauncher, and ios-webkit-debug-proxy

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.

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:

  1. Download the generated provisional profile.
  2. Connect the iOS device to a Mac using a USB cable.
  3. Open Xcode (version 6) and click on Devices under the Window menu, as shown here:

    appium-essentials-img-4

  4. Now, context click on the connected device and click on Show Provisional Profiles...:

    appium-essentials-img-5

  5. Click on +, select the downloaded profile, and then click on the Done button, as shown in the following screenshot:

    appium-essentials-img-6

SafariLauncher app and ios-webkit-debug-proxy

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:

  1. Download the source code from https://github.com/snevesbarros/SafariLauncher/archive/master.zip.
  2. Open Xcode and then open the SafariLauncher project.
  3. Select the device to deploy the app on and click on the build button.
  4. After this, we need to change the SafariLauncher app in Appium.dmg; to do this, perform the following steps:
    1. Right-click on Appium.dmg.
    2. Click on Show Package Contents and navigate to Contents/Resources/node_modules/appium/build/SafariLauncher.
    3. Now, extract SafariLauncher.zip.
    4. Navigate to submodules/SafariLauncher/build/Release-iphoneos and replace the SafariLauncher app with your app.
    5. Zip submodules and rename it as SafariLauncher.zip.

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:

  1. Open the terminal, type git clone https://github.com/google/ios-webkit-debug-proxy.git, and then click on the Enter button.
  2. Then, enter the following commands:
    cd ios-webkit-debug-proxy
    ./autogen.sh
    ./configure
    make
    sudo make install
    

We are now all set to test web and hybrid apps.

Summary

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.

Resources for Article:


Further resources on this subject:


Modal Close icon
Modal Close icon