Chapter 6. Driving Appium on Real Devices

It is really good to know that we can automate 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.

In this chapter, we will learn the following topics:

  • Dialer app automation on an Android real device
  • Registration form automation on an Android real device
  • Gmail login automation with Chrome browser on an Android real device
  • Body Mass Index (BMI) calculator automation on an iOS real device
  • Mobile hybrid app automation on an iOS real device
  • Web app automation with Safari on an iOS real device

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:
    Important initial points
  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:
    Important initial points
  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:

  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:

Desired capabilities for Android and initiating the Android driver

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.

    Note

    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.
Desired capabilities for Android and initiating the Android driver

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

Desired capabilities for native and hybrid apps

We have already discussed desired capabilities in Chapter 1, Appium – Important Conceptual Background, so 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:
    Provisional profile
  4. Now, context click on the connected device and click on Show Provisional Profiles...:
    Provisional profile
  5. Click on +, select the downloaded profile, and then click on the Done button, as shown in the following screenshot:
    Provisional profile

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. Next, it's time to move on to the desired capabilities for iOS.

Desired capabilities for iOS and initiating theiOS driver

Similar to Android, we can set the desired capabilities using the Appium GUI and by initiating the desired capabilities object. Let's discuss the Appium GUI settings for native and hybrid apps. The following screen will be displayed when the iOS Settings icon is clicked:

Desired capabilities for iOS and initiating theiOS driver

Perform the following steps to set the iOS settings for native and hybrid apps:

  1. Click on the iOS Settings icon.
  2. Select App Path and provide the path of the application.
  3. Select UDID and type in the device identifier. For the device identifier, we need to perform the following steps:
    1. Connect iOS device to a Mac using a USB cable.
    2. Open Xcode (Version 6) and click on Devices from the Window menu.
    3. Select Connected device.
    4. Under Device Information, you will get an identifier. We can also get the UDID from iTunes by clicking on Serial Number.
  4. Under Device Information, you will get an identifier.
  5. Select Platform Version from the dropdown; you can also type in the platform version (for example, 8.1).
  6. Now, start the Appium Server.

Let's set the Appium GUI settings for web apps:

Desired capabilities for iOS and initiating theiOS driver

Perform the following steps to set the iOS settings for web apps:

  1. Click on the iOS Settings icon.
  2. Select Use Mobile Safari.
  3. Select UDID and type in the device identifier.
  4. Select Platform Version from the dropdown or you can also type in one (for example, 8.1).
  5. Now, start the Appium Server.

Now, let's take a look at how to initiate the desired capabilities object and set the capabilities.

Desired capabilities for native and hybrid Apps

We have already discussed the desired capabilities for iOS in Chapter 1,Appium – Important Conceptual Background, so 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 of desired capabilities
File app=new File("path of the .app");//To create a file object to specify the app path
caps.setCapability(MobileCapabilityType.APP,app); //To set the app path
caps.setCapability(MobileCapabilityType.PLATFORM_VERSION, "8.1");//To set the iOS version
caps.setCapability(MobileCapabilityType.PLATFORM_NAME, "iOS");//To set the OS name
caps.setCapability(MobileCapabilityType.DEVICE_NAME, "iPad");// Type device name
caps.setCapability("udid","Real Device Id ");// To specify the UDID of the device

Desired capabilities for web apps

In iOS 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 of desired capabilities
caps.setCapability(MobileCapabilityType.PLATFORM_VERSION, "8.1");//To set the iOS version
caps.setCapability(MobileCapabilityType.PLATFORM_NAME, "iOS");//To set the OS name
caps.setCapability(MobileCapabilityType.DEVICE_NAME,"iPad");//To type the device name
caps.setCapability("udid","Real Device Id ");//To specify the UDID of real device
caps.setCapability(MobileCapabilityType.BROWSER_NAME,"Safari"); //To launch the Safari browser

We are done with the desired capabilities part; now, we have to initiate the iOS driver to connect to the Appium Server by importing the following packages:

import io.appium.java_client.ios.IOSDriver;
import java.net.URL;

Then, initiate the iOS Driver as shown here:

IOSDriver driver = new IOSDriver (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 simulator using the configurations specified in the desired capabilities. Now, you can use the following class for scripts with TestNG:

import io.appium.java_client.ios.IOSDriver;
import io.appium.java_client.remote.MobileCapabilityType;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class TestAppIication {
 IOSDriver driver;
 @BeforeClass //It will execute one time before execute all test cases
 public void setUp() throws MalformedURLException{
  //Set up desired capabilities
  DesiredCapabilities caps = new DesiredCapabilities();
  File app=new File("path of the .app");
  caps.setCapability(MobileCapabilityType.APP,app);
  caps.setCapability(MobileCapabilityType.PLATFORM_VERSION, "8.1");
  caps.setCapability(MobileCapabilityType.PLATFORM_NAME, "iOS");
  caps.setCapability(MobileCapabilityType.DEVICE_NAME,"iPad");
  caps.setCapability("udid","Real Device Id ");
  caps.setCapability(MobileCapabilityType.BROWSER_NAME, "Safari");// In case of web-apps
  driver = new IOSDriver (new URL("http://127.0.0.1:4723/wd/hub"), caps);
  driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
 }
 @Test
 public void testExample(){
  //We will put test scripts here
 }
@AfterClass
 public void tearDown(){
  driver.closeApp();
  //driver.quit(); //in case of web apps
}
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.218.153.50