Automation (Mobile) – Appium, Java, TestNG and AWS Device Farm for Android and IOS

Description:

This blog will help to automate mobile applications on various mobile testing platforms both Android and iOS. Here we are giving brief explanation about how to create mobile automation framework with various tools like Appium, TestNG and programming language Java in AWS Device farm environment for both Android and IOS.

Prerequisites

What Is AWS Device Farm?

Device Farm is an app testing service that enables you to test and interact with your Android, iOS, and Web apps on real, physical phones and tablets that are hosted by Amazon Web Services (AWS). There are two main ways to use Device Farm:

  • Automated testing of apps using a variety of available testing frameworks
  • Remote access of devices onto which you can load, run, and interact with apps in real time

Device Farm allows you to upload your own tests and perform the testing in parallel on multiple devices.

At the end report will be generated with high-level results, low-level logs, pixel-to-pixel screenshots, and performance data is updated once the tests are completed.

What Is Appium ?

Appium is an open-source tool for automating native, mobile web, and hybrid applications on platforms such as Android and IOS.

Version Information

  • For IOS, Device Farm supports Appium versions 1.6.3 (for iOS 10 and later) and 1.4.16 (for iOS 9 and earlier) and Java version Java 8.
  • For Android, Device Farm supports Appium versions 1.6.3 and 1.4.16 and Java version Java 8.

Prepare Your iOS or Android Appium Java TestNG Tests

  • Before you upload your iOS or Android, Appium, Java and TestNG tests to Device Farm for testing, make sure that your scripts are contained within 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  Add below steps to pom.xml to set packaging as a JAR file:

acme

2  Add below steps to 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:

   maven jar.PNG

3  Add below steps toxml to use maven-dependency-plugin to build dependencies as JAR files.

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

dependency jars   

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:

assembly.PNG

5  Add below steps to 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:

     maven assembly

6  Build, package, and verify. For example:

$ 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…)

      Note: .zip will be created inside target folder

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:

maven compiler

Upload Your iOS or Android Appium Java TestNG Tests

To run your iOS or Android Appium Java TestNG tests on a set of Android or iOS devices in Device Farm, you upload your tests with the Device Farm console as follows:

  1. Sign in to the Device Farm console athttps://console.aws.amazon.com/devicefarm.
  2. In the list of projects, choose the option next to the project that you want to upload your tests to.
  3. Tip: If the list of projects is not displayed, then on the secondary navigation bar, for Projects, choose the name of the project that you want to upload your tests to.
  4. To create a new project, follow the instructions in Create a Project.
  5. If theCreate a new run button is displayed, then choose it.
  6. On theChoose your application page, choose Upload.
  7. Browse to and choose your iOS or Android app file. The file must be an .ipa for iOS or an .apk for Android.
  8. Note: Make sure that your app file is built for an iOS device and not for a simulator.
  9. ChooseNext step.
  10. On theConfigure a test page, choose Appium Java TestNG, and then choose Upload.
  11. Browse to and choose the .zip file that contains your tests.
  12. Choose the Appium version you are using from theAppium version dropdown list.
  13. Choose Next step, and then complete the remaining on-screen instructions to select the devices to run your tests on and to then start the run.

Taking Screenshots in iOS or Android Appium Java TestNG Tests

You can take screenshots as part of your iOS Appium Java TestNG tests.

When Device Farm runs your Appium Java TestNG 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.

code

Appium setup code for Android

Appium Setup code for android

Appium setup code for IOS

Appium setup code for ios.PNG

Note:  Don’t provide anything to the desired capabilities. Device farm will provide the capabilities in run time, because test cases will be running parallel on all devices.

Refer sample git project here.

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 )

Google+ photo

You are commenting using your Google+ 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 )

w

Connecting to %s