Automated testing of Android applications in Java with Appium

Appium is an open source API which can be used for black-box testing of apps. It is built on top of Selenium WebDriver, which was covered in a previous post. Appium can be used to test native, hybrid and mobile web apps on Android or iOS. Several languages are supported, including Java. This blog will focus testing native Android applications in Java using Eclipse. Writing such tests requires you to have access to an .apk file of the application.

Installation

Several programs should be installed before being able to use Appium in Eclipse:
– JavaEE JDK
– Eclipse
– Android Studio, including the Android SKD
– ADT (a plugin for Eclipse)
– Microsoft .NET framework 4.5
– Selenium
– Appium itself

Information about how to install and configure these programs can be found in this video tutorial series (especially lecture 4-9).

Emulators

The tutorials which are mentioned above use a default Android Studio emulator. However, there are other emulators which perform better, such as Genymotion. If you choose such an emulator, you need to make sure that Eclipse can find it.

Also, under ´dev settings´ on the emulator, you need to check ‘USB debugging’. If ‘dev settings’ is not present, select settings> about phone> build number> click 7 times. Then the developer settings will become visible.

After having set up the emulator, you can install the .apk-file on the emulator (e.g. download it via the emulator’s browser or drag and drop the file to the emulator’s screen).

Make sure the emulator is running every time you run your test.

Starting the Appium server

Run the Appium.exe file. Click ‘General settings’ and ‘Android settings’ and make sure none of the boxes are checked (except, if you wish, the option to check for updates). Click the arrow at the top. Now the Appium server is running.

Make sure the server is running every time you run your test.

Creating a test case

Below you find an example of a test case:

import java.io.File;
import java.net.URL;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.remote.MobileCapabilityType;
import io.appium.java_client.remote.MobilePlatform;

public class YourTest {
	
	private static File app;
	private static URL url;
	private static DesiredCapabilities capabilities;
	
	private static String appDirectory = "C:/Users/<user>/Documents/<Directory_Of_The_APP>"; // the directory of the app
	private static String appAPKFile = "<Name_OF_APP>.1.2.apk"; // the app
	private static String urlAppiumDriver = "http://127.0.0.1:4723/wd/hub"; 
	private static WebDriver driver;
	
	@Before
	public void setUp() throws Exception {
		app = new File(appDirectory, appAPKFile);
		url = new URL(urlAppiumDriver);
		capabilities = new DesiredCapabilities();
		capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, MobilePlatform.ANDROID);
		capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "Android Emulator");
		capabilities.setCapability(MobileCapabilityType.APP, app.getAbsolutePath());
		driver = new AndroidDriver(url, capabilities);
		driver = DataSelectedMobile.driver;
	  }
	
	@Test
	public void testInloggenInt() throws Exception {
		//<your test>
	}
}

The above test case can be run as a regular JUnit test case (which is beyond the scope of this blog).

@Test

In the actual test (under @Test), you can use all selenium methods which are also available for writing browser application tests, e.g.:

driver.findElement(By.xpath("<YOUR XPATH>")).click();

Also, many of the Selenide methods can be used, such as :

$(By.xpath("<YOUR XPATH")).shouldBe(present, visible).click();

Using Selenide requires you to add Selenide to your project, add the relevant imports and set the Selenide driver. All of these issues were covered in a previous blog.

Finding xpaths

When testing browser applications, Firebug can be used to find the xpath of a field or button. When testing Android applications, an inspector can be used to achieve this goal. Appium does have its own inspector, but it is not very sophisticated. The inspector that comes with Android Studio, called ‘uiautomatorviewer‘ is much more useful. By default, it will be placed in the following directory: C:\Users\UserName\AppData\Local\Android\sdk\tools:
Appium1
The information about the classses, indexes and other properties which can be found in uiautomatorviewer can be used in order to create xpaths.
In addition, you can obtain information about the XML-structure of the application in your code, which can be used to create xpaths:

system.out.println(driver.getPageSource());

Using these strategies will enable you to write solid tests for Android applications.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

About Paula van Strien