Professional Documents
Culture Documents
© Pepgo Limited, 71-75 Shelton Street, Covent Garden, London, WC2H 9JQ, United Kingdom
Contents
About this document .............................................................................................................................. 3
Appium .................................................................................................................................................... 3
Architecture ............................................................................................................................................ 3
Enable developer options on an Android device: ................................................................................... 4
Install Android Studio and SDK (in Microsoft Windows) ........................................................................ 5
Install Appium: ........................................................................................................................................ 5
Run Appium (example) ........................................................................................................................... 5
Create an Android Virtual Device ........................................................................................................... 7
Installing an app on a Virtual Device....................................................................................................... 8
Finding element properties .................................................................................................................... 9
Using “Appium Desktop” .................................................................................................................... 9
Using “UI Automator Viewer” ............................................................................................................. 9
Using Appium Inspector .................................................................................................................... 10
Remote debugging Android hybrid applications with Google Chrome ............................................ 11
A full Appium Java example .................................................................................................................. 11
What the PasswdSafe example does ................................................................................................ 11
Using the PasswdSafe example......................................................................................................... 12
The “pom.xml” file ............................................................................................................................ 13
The “AppiumTest.java” file ............................................................................................................... 13
The “AndroidSetup.java” file ............................................................................................................ 14
The “LandingPage.java” file .............................................................................................................. 15
The “BasePage.java” file ................................................................................................................... 16
About this document
This document explains the test automation tool Appium for mobile devices. Appium is powered by
Selenium WebDriver, and the same Appium program code can be used for automating both Google
Android and Apple iOS platforms.
This document contains Java samples developed on the Microsoft Windows operating system for
Google Android as target platform. The samples in this document use an Android native app, but
they can easily be adapted to Android hybrid applications, or Android mobile web applications.
The examples can also easily be adapted for development on the Apple macOS operating system,
targeting the Apple iOS mobile platform for mobile devices such as the Apple iPhone.
Appium
Appium is an open source test automation tool for mobile applications. It allows you to test all the
three types of mobile applications: native, hybrid and mobile web.
It also allows you to run the automated tests on actual devices, emulators and simulators.
Today when almost every mobile app is available for at least two platforms (iOS and Android), you
need a tool, which allows testing cross platforms. Having two different frameworks for the same app
would increase the cost of testing and time to maintain it as well.
The basic philosophy of Appium is that you should be able to reuse code between iOS and Android,
and that’s why the API’s are the same across iOS and Android. Another important thing to highlight
is that unlike Calabash, Appium doesn’t modify your app or need you to even recompile the app, or
have access to its source code.
Appium lets you choose the language you want to write your test in. It doesn’t dictate the language
or framework to be used.
Architecture
When you download Appium, you are basically downloading the server. The server is written in
Node.js and implements Selenium WebDriver. It allows you to use Selenium WebDriver clients to
drive your tests. Your mobile app acts precisely like a web app, where the DOM is represented by
the View hierarchy.
So this server basically exposes a REST API, which performs the following actions:
1. On stock Android go to: “Settings > About phone > Build number”.
2. Once you’ve found the build number section of the settings, tap on the section 7 times. After
two taps, a small pop up notification should appear saying "you are now X steps away from
being a developer" with a number that counts down with every additional tap. After the 7th
tap, the developer options will be unlocked and available. They can usually be found in the
main settings menu.
3. Switch on "Stay awake".
4. Switch on "USB debugging".
To check if the (real) device can be used, type from command prompt: "adb devices" (after you
have correctly installed the Android SDK, as described in the next paragraph). In Microsoft Windows,
the output should look like this:
C:\>adb devices
List of devices attached
00c600c600c600c6 device
If the device is not recognised, then check if the USB driver is correctly installed in Microsoft
Windows: http://developer.android.com/studio/run/oem-usb.html
Install Android Studio and SDK (in Microsoft Windows)
Android Studio contains the Android SDK. It is recommended to install Android Studio, although you
can also just install the Android SDK (the links to the SDK are provided at the bottom of the page
under “Get just the command line tools”).
1. Go to https://developer.android.com/studio/index.html
2. Download Android Studio
3. Confirm the license agreement
4. Launch the downloaded “*.exe” file
5. Follow the setup wizard to install Android Studio and the SDK
Install Appium:
Appium can be downloaded from http://appium.io/downloads.html
The Android app file (“*.apk”) must be set in the “Application Path” of the Appium Android
Settings. However, please note that some versions of Appium have problems with paths that contain
spaces, so avoid that if possible.
Newer versions of Appium will find the Package and (starting) activity automatically, but if not, then
theses can also be found from command line with the command:
Note that “aapt” is part of the Android SDK “build-tools” folder (in the relevant Android SDK
version subfolder). So if the command is not recognised, then you might have to add the folder to
your Microsoft Windows “path” environment variable.
Under “Device Name”, you will have to set the (real or virtual) device name that you know from
running the “adb devices” command.
http://localhost:4723/wd/hub/status
You first need to open the Android Virtual Device Manager. From a command prompt, you can do
this with the command “android avd”.
Then you need to create an Android Virtual Device. The ideal settings for the device depend on the
host machine. Here are a few performance tips for Microsoft Windows:
1. If you run on a host machine with an Intel processor that supports Intel’s virtualisation (VT-x)
technology, then use “Intel Atom (x86)” as CPU/ABI. Please note that you therefore need to
install HAXM ("Intel Hardware Accelerated Execution Manager") through the Android SDK
Manager.
2. A lower screen resolution is generally faster, for example use “WVGA800”.
3. If you tick the “Snapshot” option, then you will be able to save a snapshot of the machine
and therefore be much faster on the next start/execution.
You can start virtual devices right from the Android Virtual Device Manager by selecting the device
and clicking the “Start…” button.
As you already know, you can check the name(s) of the running virtual and real devices from
command line:
“adb devices”
The first step is to run the Virtual Device, on which you want to install the app. You can start a
Virtual Device from the AVD Manager by clicking the “Start…” button, or from the command line:
emulator -avd <AVD_Name>
This launches the Virtual Device and the command is running in the command prompt window.
Launch another command prompt window and run the following command (the parameter “–e” is
for specifically addressing the Emulator, for addressing a real device on a USB connection, the
corresponding parameter is “–d”).
adb –e install <path_to_apk_file>
Finding element properties
Using “Appium Desktop”
Appium Desktop is an open source app for Mac, Windows, and Linux that presents the power of the
Appium automation server in a beautiful and flexible UI. It is a combination of a few Appium-related
tools: Being a graphical interface for the Appium server, it is an excellent tool for finding locators
required for Page Objects.
https://github.com/appium/appium-desktop/releases
Appium Desktop integrates with the Android Debug Bridge (ADB), which is useful for querying
devices to get hold of package names.
https://developer.android.com/studio/command-line/adb
While writing your tests, this tool is essential, as it exposes the Id and other attributes of an element,
which are required for writing scripts.
Clicking on the devices icon (second from the left, next to the open folder icon) takes a dump of a UI
XML snapshot of the screen shown on the device.
The left side of the tool shows how the device looks like. The right side is divided in two parts:
1. The upper half shows the UI XML snapshot and the nodes structure.
2. The lower half shows the details of the selected node with all the attributes.
You can explore the properties of UI elements by clicking on them. Usually, the most helpful
property for scripting is the “resource-id”, but it is not always available. When it is not available, it is
often best to use the class name in combination with another property (such as text) to generate an
Xpath expression that uniquely identifies the UI element.
PasswdSafe supports viewing and editing of Password Safe data files. You can therefore exchange
your data files between your (Microsoft Windows) PC and your Android device.
The PasswdSafe app can be installed on your Android device from Google play.
The app and the source code can also be downloaded from
http://sourceforge.net/projects/passwdsafe/ .
package com.pepgo.appiumpasswdsafe.scenarios;
import org.junit.BeforeClass;
import org.junit.AfterClass;
import org.junit.Test;
import org.openqa.selenium.By;
import com.pepgo.appiumpasswdsafe.pages.LandingPage;
@BeforeClass
public static void setUp() throws Exception {
prepareAndroidForAppium();
}
@AfterClass
public static void tearDown() throws Exception {
driver.quit();
}
@Test
public void createAndDeletePasswdSafeFile_Test() throws Exception {
// Close start pop-up window
driver.findElement(By.id("android:id/button1")).click();
package com.pepgo.appiumpasswdsafe.scenarios;
import io.appium.java_client.android.AndroidDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
//other caps
capabilities.setCapability("app", app.getAbsolutePath());
driver = new AndroidDriver(new URL("http://localhost:4723/wd/hub"), capabilities);
}
}
import io.appium.java_client.android.AndroidDriver;
import org.openqa.selenium.By;
import static org.junit.Assert.*;
// Enter password
driver.findElement(editTextPassword).click();
enterTextInPasswordField(strPassword);
// Click OK
driver.findElement(By.id("android:id/button1")).click();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
// Enter password
driver.findElement(enterTextPassword).click();
enterTextInPasswordField(strPassword);
// Click widget button
driver.findElement(By.className("android.widget.ImageButton")).click();
// Click OK
driver.findElement(By.id("android:id/button1")).click();
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import io.appium.java_client.android.AndroidDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.RemoteWebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.util.HashMap;
// Accepts only numbers (key codes 7-16) and lower and upper case letters (key codes 29-54)
// Key code 66 is <Enter>
public void enterTextInPasswordField(String strTextToEnter) {
int intCounter, intKey;
for (intCounter = 0; intCounter < strTextToEnter.length(); intCounter++) {
intKey = (int) strTextToEnter.charAt(intCounter);
if (intKey > 47 && intKey < 58) {
intKey = intKey - 41;
driver.pressKeyCode(intKey);
}
else if (intKey > 64 && intKey < 91) {
intKey = intKey - 36;
driver.pressKeyCode(intKey, 1);
}
else if (intKey > 96 && intKey < 123) {
intKey = intKey - 68;
driver.pressKeyCode(intKey);
}
}
driver.pressKeyCode(66);
}
}