May 20, 2020 Jonny Steiner

How to Carry out Web Testing on Android with Appium

Introduction
Appium
Simple Web test creation and execution
Executing the Test
Prerequisites
Start Appium Server
Create Run Configuration in IntelliJ
Execute the Run Configuration

Introduction

With the advent of Smartphones, Enterprises have to cater their applications to Mobile devices in addition to web testing for desktop Computers.

Typically, Native applications are built for Mobile devices, but this is not enough. The reason is there are Customers who do not wish to install native applications due to reasons like,

  • Space restrictions.
  • A quick review of the product does not require installation of Native applications.
  • Avoid Native application notification affecting the speed of the mobile device.

Hence it is important that Web Applications which are lighter and require no additional installations to be also receptive to Mobile browsers. This also means that the testing now requires for supportability of these applications in Mobile browsers. Android

This article focuses on Web Testing on Mobile devices using the Android Operating System

There are several tools available which support Web Testing in Android and few of them are,

  1. Appium
  2. Selendroid
  3. Espresso Web
  4. Robitium

Appium

The table below summarizes how Appium compares with other tools on Automation testing.

FeaturesAppiumSelendroidEspresso WebRobotium
Cross-PlatformiOS and AndroidAndroidAndroidAndroid
Web TestingSupportsSupportsSupportsWebView only i.e. only Hybrid Apps
Developer CommunityLargeSmallSmallSmall
Supporting ToolsProvides tools like Element Inspector, Recording, etc.Provides tools like Element Inspector, Recording, etc.It does not provide comprehensive tools.It does not provide comprehensive tools.
Language Support for Test developementCaters to most of the popular languagesLimited support.Limited support.Limited support.
PerformanceSlowSlowFast Slow

We can see that Appium has more advantages than its peers with broader language support, larger developer community, and cross-platform being important ones.

The rest of the article will focus on a short tutorial on how to develop a simple Web test in Appium.

Simple Web test creation and execution

Creating the Web Test using Appium

We will use Java as a programming language and IntelliJ IDE in these instructions.

Step 1. Create a Gradle Project in IntelliJ

Specify the name of the Gradle project.

In the rest of the screens, choose defaults and click next and in the final step finish to create the project.

Step 2. Specify the dependencies in build.gradle file.

Open the build.gradle file and replace existing ‘dependencies’ directives following lines.

dependencies {
   compile group: 'org.testng', name: 'testng', version: '+'
   compile group: 'io.appium', name: 'java-client', version: '+'
}

Save the project and click ‘Import changes’ alert popped up by IntelliJ.

This will download all the dependencies.

Step 3. Create a Java file and Implement the Test.

Select the project’s Java folder and create the Java class named AppiumWebTest.

Now we start creating the Test using the TestNG framework, below are important.

Initializing the driver

Appium requires the initializing of WebDriver to connect the Appium Server/Devices. In general, initialization needs to be accomplished by passing the Desired capabilities.  Desired capabilities define session attributes. Typically these form the initialization for the Test class and need to be in the initialization function of the Test framework.

Note: deviceName can be found out by executing command ADB devices. More information on this can be found in Enabling Developer Mode.

DesiredCapabilities dc = new DesiredCapabilities();
protected RemoteWebDriver driver = null;
.
.

dc.setCapability("platformName","Android");
dc.setCapability("deviceName", DEVICE_NAME);
dc.setCapability(, "Chrome");
dc.setCapability("chromedriverExecutable","CHROME_DRIVER_PATH");
driver = new AndroidDriver(new URL(APPIUM_SRV_URL), dc);

In the above capabilities, browserName and chromedriverExecutable are specifically used in Web Testing context.

CapabilityDescription
browsernameSpecifies which browser to be used.
chromedriverExecutableSpecifies path of the chrome driver to be used which is compatible with the chrome browser installed in the Mobile device.

Note: In order to automate any browser, Appium needs a browser driver. Please refer to the documentation Appium automating of mobile web apps.

Test Method

Test function in the context of Mobile Web testing involved primarily, loading the Web application, locating the UI elements, and executing the automation commands.

@Test
public void testWeb() {
   // loading web url 
   driver.get("http://www.google.com");
   // locating element.
   By qBy = By.xpath("//*[@name='q']");
   // click and sendkeys automation commands
   driver.findElement(qBy).click();
   driver.findElement(qBy).sendKeys("experitest");
   driver.findElement(qBy).sendKeys(Keys.ENTER);
}

Releasing the driver

@AfterTest
public void tearDown() throws MalformedURLException {
   driver.quit();
}

The final test will look like this,

import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidElement;
import io.appium.java_client.remote.MobileCapabilityType;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * Class for Web Testing using Appium in an Android Device.
 */
public class AppiumTest {

    DesiredCapabilities dc = new DesiredCapabilities();
    protected RemoteWebDriver driver = null;

    public static final String APPIUM_SRV_URL = "http://localhost:4723/wd/hub";
    public static final String DEVICE_NAME = "a3ae1c63";
    public static final String CHROME_DRIVER_PATH = "C:\\experitest1\\chromedriver_win32\\chromedriver.exe";

    @BeforeTest
    public void setUp() throws MalformedURLException {

        dc.setCapability("platformName","Android");
        dc.setCapability("deviceName", DEVICE_NAME);
        dc.setCapability("browserName", "Chrome");
        dc.setCapability("chromedriverExecutable","CHROME_DRIVER_PATH");
        driver = new AndroidDriver(new URL(APPIUM_SRV_URL), dc);
    }

    @Test
    public void testWeb() {
        driver.get("http://www.google.com");
        By qBy = By.xpath("//*[@name='q']");
        driver.findElement(qBy).click();
        driver.findElement(qBy).sendKeys("experitest");
        driver.findElement(qBy).sendKeys(Keys.ENTER);
    }

    @AfterTest
    public void tearDown() throws MalformedURLException {
        driver.quit();
    }

}

The example project associated with this blog is located at First Web Test using Appium.

Executing Web Testing

Prerequisites

  1. The latest version of Java, needed for Android Studio.
  2. Installation of Android Studio with SDK. This is needed since adb gets installed as part of the Android SDK. The adb utility is required to get the device list connected to the PC.
  3. Installation of the latest version of IntelliJ.
  4. Connecting the Mobile device to the PC using USB cable and enabling the developer mode/USB debugging in the android device.
  5. Installation of Appium Desktop.

Start Appium Server

Start the Appium Server by launching Appium Desktop and then clicking the Start Server button.

appium sign in web testing

Create Run Configuration in IntelliJ

To create a Run Configuration file navigate to IntelliJ Menu > Run > Edit Configurations. This will open up a dialog box. Click on +, Specify a name in the ‘Name’ field. Select the class name in the Class field and Click OK to save.

appium web testing testng

Execute the Run Configuration

appium web testing

Execute the test by executing the Run Configuration created above by navigating to Run > Run Configuration name (AppWebTestTestNG)

Guy ArieliCTO