Menu
AWS Device Farm
Developer Guide (API Version 2015-06-23)

Working with Appium Java JUnit for Android and AWS Device Farm

Device Farm provides support for Appium Java JUnit for Android.

Device Farm also provides a sample Android application along with links to working tests in three Android automation frameworks, including Appium. You can download the Device Farm sample app for Android on GitHub.

What Is Appium Java JUnit?

Appium is an open-source tool for automating native, mobile web, and hybrid applications on platforms such as Android. For more information, see About Appium.

Version Information

Currently, Device Farm supports Appium versions 1.6.3 and 1.4.16 and Java version Java 8.

Prepare Your Android Appium Java JUnit Tests

Your Android Appium Java JUnit tests must be contained in a .zip file.

Build the Appium Java Test Package

The Appium Java test package you upload to Device Farm must be in .zip format and contain all of the tests' dependencies. The following instructions will show you how to meet these requirements during the package stage of a Maven build.

  1. Modify pom.xml to set packaging as a JAR file:

    Copy
    <groupId>com.acme</groupId> <artifactId>acme-android-appium</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging>
  2. Modify pom.xml to use maven-jar-plugin to build your tests into a JAR file.

    The following plugin will build your test source code (anything in the src/test directory) into a JAR file:

    Copy
    <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>2.6</version> <executions> <execution> <goals> <goal>test-jar</goal> </goals> </execution> </executions> </plugin>
  3. Modify pom.xml to use maven-dependency-plugin to build dependencies as JAR files.

    The following plugin will copy your dependencies into the dependency-jars directory:

    Copy
    <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <version>2.10</version> <executions> <execution> <id>copy-dependencies</id> <phase>package</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <outputDirectory>${project.build.directory}/dependency-jars/</outputDirectory> </configuration> </execution> </executions> </plugin>
  4. Save the following XML assembly to src/main/assembly/zip.xml.

    The following XML is an assembly definition that, when configured, instructs Maven to build a .zip file containing everything in the root of your build output directory and the dependency-jars directory:

    Copy
    <assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd"> <id>zip</id> <formats> <format>zip</format> </formats> <includeBaseDirectory>false</includeBaseDirectory> <fileSets> <fileSet> <directory>${project.build.directory}</directory> <outputDirectory>./</outputDirectory> <includes> <include>*.jar</include> </includes> </fileSet> <fileSet> <directory>${project.build.directory}</directory> <outputDirectory>./</outputDirectory> <includes> <include>/dependency-jars/</include> </includes> </fileSet> </fileSets> </assembly>
  5. Modify pom.xml to use maven-assembly-plugin to package tests and all dependencies into a single .zip file.

    The following plugin uses the preceding assembly to create a .zip file named zip-with-dependencies in the build output directory every time mvn package is run:

    Copy
    <plugin> <artifactId>maven-assembly-plugin</artifactId> <version>2.5.4</version> <executions> <execution> <phase>package</phase> <goals> <goal>single</goal> </goals> <configuration> <finalName>zip-with-dependencies</finalName> <appendAssemblyId>false</appendAssemblyId> <descriptors> <descriptor>src/main/assembly/zip.xml</descriptor> </descriptors> </configuration> </execution> </executions> </plugin>
  6. Build, package, and verify. For example:

    Copy
    $ mvn clean package -DskipTests=true $ tree target . |— acme-android-appium-1.0-SNAPSHOT.jar (this is the JAR containing everything built from the ./src/main directory) |— acme-android-appium-1.0-SNAPSHOT-tests.jar (this is the JAR containing everything built from the ./src/test directory) |— zip-with-dependencies.zip (this .zip file contains all of the items) `— dependency-jars (this is the directory that contains all of your dependencies, built as JAR files) |— com.some-dependency.bar-4.1.jar |— com.another-dependency.thing-1.0.jar |— joda-time-2.7.jar |— log4j-1.2.14.jar |— (and so on...)
  7. Use the Device Farm console to upload the test package.

Tip

If you receive an error saying that annotation is not supported in 1.3, add the following to pom.xml:

Copy
<plugin> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.7</source> <target>1.7</target> </configuration> </plugin>

Upload Your Android Appium Java JUnit Tests

Use the Device Farm console to upload your tests:

  1. Sign in to the Device Farm console at https://console.aws.amazon.com/devicefarm.

  2. In the list of projects, choose the option next to the project where you want to upload your tests.

    Tip

    If the list of projects is not displayed, then on the secondary navigation bar, for Projects, choose the name of the project where you want to upload your tests.

    To create a new project, follow the instructions in Create a Project.

  3. If the Create a new run button is displayed, then choose it.

  4. On the Choose your application page, choose Upload.

  5. Browse to and choose your Android app file. The file must be an .apk file.

  6. Choose Next step.

  7. On the Configure a test page, choose Appium Java JUnit, and then choose Upload.

  8. Browse to and choose the .zip file that contains your tests. The .zip file must follow the format described in Prepare Your Android Appium Java JUnit Tests.

  9. Choose the Appium version you are using from the Appium version dropdown list.

  10. Choose Next step, and then complete the remaining on-screen instructions to select devices and start the run.

Taking Screenshots in Android Appium Java JUnit Tests

You can take screenshots as part of your Android Appium Java JUnit tests.

When Device Farm runs your Appium Java JUnit test, the service sets several Java system properties that describe the configuration of the Appium server with which you're communicating. For example, Device Farm sets the appium.screenshots.dir property to a fully qualified path on the local file system where Device Farm expects Appium screenshots to be saved. The test-specific directory where the screenshots are stored is defined at runtime. The screenshots are automatically pulled into your Device Farm reports automatically. To view the screenshots, in the Device Farm console, choose the Screenshots section.

The following example shows how to use and consume the appium.screenshots.dir property to capture an Appium screenshot that is pulled into your Device Farm report.

Copy
public boolean takeScreenshot(final String name) { String screenshotDirectory = System.getProperty("appium.screenshots.dir", System.getProperty("java.io.tmpdir", "")); File screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE); return screenshot.renameTo(new File(screenshotDirectory, String.format("%s.png", name))); }

Additional Considerations for Android Appium Java JUnit Tests

Device Farm does not modify Android Appium Java JUnit tests.