My first encounter with AWS Device Farm

My first encounter with AWS Device Farm

Device Farm is an app testing service available in the bunch of several AWS services. This facilitates testing Android app on real, physical phones of multiple flavors.

My maiden attempt to test a native app using AWS Device Farm is interesting and successful. I tested the app using appium framework that supports cross-platform testing. I will share my experience with appium framework in my next posting.

Here is how AWS Device Farm is deployed to test an Android native app, implementing appium automation test; my code is as below.

import io.appium.java_client.android.AndroidDriver;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import library.Utility;
import org.openqa.selenium.By;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.Test;
 
public class StartApplication {
private static AndroidDriver driver;
@Test
public void test01() throws Exception{      

DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.BROWSER_NAME, "");
capabilities.setCapability("deviceName", "My New Phone");
capabilities.setCapability("platformVersion", "4.4.2");
capabilities.setCapability("platformName", "Android");

capabilities.setCapability("appPackage", "com.ionicframework.prototype1356687");
capabilities.setCapability("appActivity", "com.ionicframework.prototype1356687.MainActivity");

driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
Thread.sleep(5000);
driver.findElement(By.id("android:id/content")).click();
Thread.sleep(2000);
Utility.takeScreenshot(driver, "screen");
Thread.sleep(1000);
driver.quit();

}

}

Code for takescreenshot :

public static boolean takeScreenshot(WebDriver driver,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)));
	   }

Next step is to upload Appium test package to AWS device farm . This package should contain all the tests and dependencies and must be in zip format.

The following instructions will guide, how to meet these requirements during the package stage of a Maven build.

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

<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/testdirectory) into a JAR file:

<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 plugin below will copy your dependencies into the dependency-jarsdirectory:

<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>

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
    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 namedzip-with-dependencies in the build output directory every time mvn package is run:

<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:

$ mvn clean package –-DskipTests=true

After you run mvn clean package -DskipTests=true, you will find a zip-with-dependencies.zip file in the target folder.

Now go to aws console and login.

Step 1: Go to Device Farm service and create a project.

Step 2: Under the created project, create a new run
select application type (Native app on android or ios/ web application)

Selected native app. (prompted to select apk/ipa file to upload)

fig-1

I have uploaded apk file; It processes and details of the uploaded apk are shown as below.

fig-2

Proceed to next step by clicking on next step.

Step 3: Configure test > select test type : I selected Appium Java Test NG [because I implemented my tests using testNG framework] and upload the zip file which contains your test and dependencies jars.

fig-3

Proceed to the next step

Step 4: Select devices on which you want to test your application.

fig-4

 

Step 5: review and start run: Here we can change the selected apk/ uploaded test package/ selected devices.
fig-5

 

Click on start run. You will see the list of devices on which the tests are running as in the screen shot shown below.

 

fig-6

After completion of all tests in all the devices the screen will be seen as below.

fig-7

Click on screen shots tab to see screens on each selected device.

fig-8

You will notice the change in the  display pattern, if any, in different devices tested.

Leave a Reply