Skip to main content
How to Carry out Web Testing on Android with Appium
Last Updated May 19, 2020 — Continuous Testing Expert

How to Carry out Web Testing on Android with Appium

Continuous Testing
IntroductionAppiumSimple Web test creation and executionExecuting the TestPrerequisitesStart Appium ServerCreate Run Configuration in IntelliJExecute 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.

[table id=61 /]

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.

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

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.

[java] 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); [/java]

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

[table id=62 /]

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.

[java] @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); } [/java]

Releasing the driver

[java] @AfterTest public void tearDown() throws MalformedURLException { driver.quit(); } [/java]

The final test will look like this,

[java] 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(); } } [/java]

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)

https://www.youtube.com/watch?v=0ByjNEuSP-E&t=2395s

Guy Arieli - CTO

 

More from the Blog

View more
Apr 22, 2021

Are Agile Happiness Metrics logical or Laughable?

Continuous Testing
These days it seems as though everyone is trying to live in an Agile w ...
Read More
Apr 12, 2021

These blockers most commonly stop people from learning test automation

Continuous Testing
We are often asked if it is hard to become a test automation engineer. ...
Read More
Apr 05, 2021

Build better apps with a low-code development approach

Continuous Testing
From being an option, DevOps is quickly turning into a necessity for m ...
Read More
Mar 29, 2021

Every web and mobile app tester needs these 7 surprising software testing skills

Continuous Testing
If you look at a job listing for QA or test automation professionals, ...
Read More
Contact Us