Open Credo

September 19, 2013 | Software Consultancy

Cross platform test automation with Appium, WebDriver and Cucumber-JVM

This post will give an overview of mobile testing using Appium. We will integrate tests for a native Android application into an existing Cucumber-JVM based set of acceptance tests and demonstrate multi platform testing from a single set of BDD scenarios. The sample code for this can be found here.

WRITTEN BY

Oana Rusu

Cross platform test automation with Appium, WebDriver and Cucumber-JVM

Appium is an open source automation framework used for testing native Android and iOS apps. It is a Node.js server that automates native and hybrid mobile applications. You can write the tests in any language supported by Selenium, using the WebDriver API. Appium automates Android using the UIAutomator library, which is provided by Google as part of the Android SDK.

It is easy to write tests against mobile apps, especially after using Selenium and WebDriver. The same code is written for testing both the mobile site and the native Android / iOS app. The only difference is the configuration part, where you actually tell it to use the WebDriver or the RemoteWebDriver.

 

HOW IT WORKS

oanablog

Appium runs on real devices and emulators. It takes the Selenium commands from your test code and translates them into a readable format for UIAutomator, using the WebDriver JSON Wire Protocol.

 

GETTING STARTED

The sample code has a class for testing an Android app without using Cucumber, the BDD framework we use inside the company and a suite of classes / files for testing both the native app and a website using the same code, implemented with Cucumber.

You will need to do the following:

1. Download Appium.

2. Download and unzip the Android SDK. This includes the Android emulator.

3. Download and install Node.js. Appium is written in Node.js

4. After downloading sdk, you need to add the <path_to_sdk_directory> from your computer to the PATH variable.

On a Mac, do the following:

a. Start up Terminal

b. Type “cd ~/” to go to your home folder

c. Type “touch .bash_profile” to create your new file.

d. Edit .bash_profile with your favourite editor (or you can just type “open -e .bash_profile” to open it    in TextEdit.

e. Add the following in the file: “export PATH=${PATH} ANDROID_HOME=

<path_to_androidSDK_directory>/sdk/ “

“path_to_androidSDK_directory “ is the path from your computer to the unzipped androidSDK            package downloaded at point 2.

f. Type “. .bash_profile” to reload .bash_profile and update any functions you add.

In Windows, set the ANDROID_HOME = <path_to_androidSDK_directory>/sdk/  to your PATH environment variable.

5. Download Maven if you don’t already have it. To check if you already have maven installed on your computer, open a terminal window and type: “mvn -version”. If you receive an “unknown command” message, you will need to download it.

6. Download the files from the repository located here.

The repository contains the following: an html page under AddContactWebsite folder that has the same behaviour as the native mobile app, a compiled binary for the mobile app (ContactManager.apk) along with the source code under ContactManagerSample folder. We use BDD and Cucumber for our projects, including this one, but you can find a sample of code that does not use Cucumber under androidTest package. This test is written to test only the Android app.

 

RUNNING THE TEST

Now that you have the code ready, here are the steps for running it against the app:

1. Configure an Android emulator. Go to android_sdk_directory -> eclipse and open Eclipse.

a. Start the AVD Manager:

  • In Eclipse: select Window > Android Virtual Device Manager.
  • In the Android Virtual Devices panel, you’ll see a list of existing AVDs. Click New to create a new AVD. The Create New AVD dialog appears.

c. Fill in the details for the AVD:

  • Give it a name (one of your choice that you can remember later), a device (choose one from the list), a platform target (choose probably the only available option).
  • Click OK.

Your AVD is now ready and you can either close the AVD Manager, create more AVDs, or launch an emulator with the AVD by selecting the device that you just created and clicking Start.

2. If you closed the AVD Manager without clicking on Start, you can start the emulator by opening the terminal window, navigate to  <ANDROID_SDK_DIRECTORY>/sdk/tools and enter the “./emulator -avd <emulator_name>” command.

3. To make sure that the emulator is seen as running, in the terminal window go to <ANDROID_SDK_DIRECTORY>/sdk/ platform-tools and run:“ ./adb devices “ command. This command should list the emulator that you just started.

If you started the emulator, but the “ ./adb devices “ command returns no result, try running:

“ adb kill-server “

“ adb start-server “

Sometimes there are issues connecting to the emulator, so you would need to restart adb by performing the above instructions.

4. Open Appium and on the top text boxes enter the IP Address 127.0.0.1 and Port 4723 (the default setting) and click the Launch button.

If you get the following error:

“ bash: /usr/local/bin/node: No such file or directory “, you will need to download and install node.js.

5. The tests need to know where the .apk file is on your computer, so it can be loaded in the emulator. The .apk file is the compiled mobile app that you can download from the sample code. Before running the tests, you have to make some changes in the java classes TestAndroidWithoutCucumber.java and AddContactStepDefs.java. Edit this classes either by importing the entire project in Idea or Eclipse or by opening them in an editor and change the “<path>” that you see now to the actual path_till_apk_file from your computer. If you would like to read more about how to setUp an Android project, check the Appium documentation.

6. Now it’s time to run the tests. We use Cucumber as our BDD framework. As not everyone uses it, I created AndroidTestWithoutCucumber.java class which is a regular JUnit test. You have to open a command prompt window, navigate to the project directory that you downloaded from github and enter the following command: “mvn clean install -Dtest=TestAndroidWithoutCucumber”

To test the code written using Cucumber, you have two options:

a. Navigate to the project directory and run only the code that tests the mobile app by using “mvn clean install -Dtest=RunTest -Ddevice=android” command.

b. Navigate to the project directory and run only the code that tests the web app. Under AddContactWebsite folder that you downloaded you will find an index.html file. You will need to enter the path to that file in the terminal window. To run the test submit the following command: “mvn clean install -Dtest=RunTest -Ddevice=firefox -DapplicationUrl=<path_to_the_index.html_file>”

The “device” and “applicationUrl” parameters are configured in the pom.xml file of the project. Running the commands above will override them with the values entered in the terminal window. Before running the test, you can change this values in the pom.xml file and not write them in the terminal window.

If you get the following error while running your test:

“A session is either terminated or not started (Original error: Could not find adb; do you have the Android SDK installed and the tools + platform-tools folders added to your PATH?) (WARNING: The server did not provide any stacktrace information)”

Then you need to add the path to sdk folder to the PATH variable. You can do that by editing / creating the .bash_profile file and add the following : export PATH=${PATH} ANDROID_HOME=<path>/sdk/

Or

You need to check again that the path to your .apk is the correct one.

That should do it!

 

This blog is written exclusively by the OpenCredo team. We do not accept external contributions.


RETURN TO BLOG

SHARE

Twitter LinkedIn Facebook Email

SIMILAR POSTS

Blog