Testing mobile web applications on Android using Appium

Appium drives the automation of Android applications using a UI Automator bundled with Andorid SDK. The process is pretty much the same as testing on iOS.

Appium works as an HTTP server and receives the commands from test scripts over the JSON wire protocol. Appium sends these commands to the UI Automator so that these can be executed on the app launched in an emulator or real device. While doing so, Appium translates the JSON commands into UI Automator Java commands that are understood by Android SDK. This process is shown in the following diagram:

Testing mobile web applications on Android using Appium

When a command is executed on the app in the emulator or device, the target app sends the response back to the UI Automator, which is sent back to the Appium. It translates the UI Automator responses into Selenium WebDriver JSON wire protocol responses, and sends them back to the test script.

Getting Ready

Testing apps on Android is similar to testing on iOS. For Android, we will use a real device instead of an emulator (a simulator is called an emulator in the Android community). We will use the same application to test in Chrome for Android.

For this example, I am using the Samsung Galaxy S4 Android handset. We need to install the Google Chrome browser on the device. You can get Google Chrome at Google's Play store if case it is not pre-installed on your device. Next, we need to connect the device to the machine on which Appium server is running.

Let's run the following command to get a list of emulator or devices connected to the machine:

./adb devices

The Android Debug Bridge (ADB) is a command line tool available in the Android SDK that lets you communicate with an emulator instance or an actual Android device connected to your computer.

The ./adb devices command will display a list of all the Android devices that are connected to the host. In this example, we connected to a real device, which is listed as seen in the following screenshot:

Getting Ready

How to do it...

Let's change the test that we created for iOS and modify it for Android. We will change the desired capabilities, as shown in the following code example, to run a test on Android:

package com.secookbook.examples.chapter14.android;

import java.net.URL;

import io.appium.java_client.android.AndroidDriver;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.By;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.junit.*;

import static org.junit.Assert.*;

public class MobileBmiCalculatorTest {

  private WebDriver driver;

  @Before
  public void setUp() throws Exception {
    // Set the desired capabilities for Android Device
    DesiredCapabilities caps = DesiredCapabilities.android();
    caps.setCapability("deviceOrientation", "portrait");
    caps.setCapability("platformVersion", "4.4");
    caps.setCapability("platformName", "Android");
    caps.setCapability("browserName", "Chrome");

    // Create an instance of AndroidDriver for testing on Android platform
    // connect to the local Appium server running on a different machine
    // We will use WebElement type for testing the Web application
    driver = new AndroidDriver<WebElement>(new URL("http://192.168.0.101:4723/wd/hub"), caps);

    // Open the BMI Calculator Mobile Application
    driver.get("http://cookbook.seleniumacademy.com/mobilebmicalculator.html");
  }

  @Test
  public void testMobileBmiCalculator() throws Exception {
    // Get the height field and set the value
    WebElement height = driver.findElement(By.name("heightCMS"));
    height.sendKeys("181");

    // Get the weight field and set the value
    WebElement weight = driver.findElement(By.name("weightKg"));
    weight.sendKeys("80");

    // Click on Calculate button
    WebElement calculateButton = driver.findElement(By.id("Calculate"));
    calculateButton.click();

    // Check the Bmi Result
    WebElement bmi = driver.findElement(By.name("bmi"));
    assertEquals("24.4", bmi.getAttribute("value"));

    // Check the Category Result
    WebElement bmi_category = driver.findElement(By.name("bmi_category"));
    assertEquals("Normal", bmi_category.getAttribute("value"));
  }

  @After
  public void tearDown() throws Exception {
    // Close the browser
    driver.quit();
  }
}

How it works...

In the preceding example, we assigned the platformName capability value to Android, which will be used by Appium to run tests on Android.

As we want to run the tests in Chrome for Android, we have mentioned Chrome in the browser capability section of the code. The other important change we made was using the AndroidDriver class from the Appium Java client libraries.

Appium will use the first device from the list of devices that adb returns, as shown in the following screenshot. It will use the desired capabilities that we mentioned and will launch the Chrome browser on the device and start executing the test script commands, as shown in the following screenshot:

How it works...

The following screenshot shows the test running on a real device:

How it works...
..................Content has been hidden....................

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