Setup and run a Browser via Selenium in AWS Device Farm

— 3 min read

Ever want to learn how easy it is to set up a selenium browser testing via AWS device farm via browser.

You have to have an AWS account, setup AWS device farm, set up a maven project in eclipse, by downloading the amazon sdk + selenium.

Here is how: Create a project in AWS device Farm

Then copy the arn: and store it somewhere: you need this arn in your eclipse project later in the tutorial

add User in aws console

Create user AWSDeviceFarmuser with programmatic access

put the user in the AwsDeviceFarmGroup(optional) and then add the policy to the group/user

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "devicefarm:*"
      ],
      "Resource": [
        "*"
      ]
    }
  ]
}

Review if everything is setup properly

copy the access key and secret key

download amazon sdk 2.0 and make sure to store your user and api key

Then create a eclipse maven project, make sure your compiler is set to java 1.8 or higher in order to work with the AWS device farm java libraries

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.learnautomatedtesting</groupId>
  <artifactId>AWSDeviceFarmTest</artifactId>
  <version>0.0.1-SNAPSHOT</version>

  <name>AWSDeviceFarmTest</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
        <dependency>
   <groupId>software.amazon.awssdk</groupId>
   <artifactId>devicefarm</artifactId>
   <version>2.11.4</version>
</dependency>
<dependency>
   <groupId>software.amazon.awssdk</groupId>
   <artifactId>regions</artifactId>
   <version>2.11.4</version>
</dependency>
<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>4.0.0-alpha-4</version>
</dependency>
<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-chrome-driver</artifactId>
    <version>4.0.0-alpha-4</version>
</dependency>
<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-server</artifactId>
    <version>4.0.0-alpha-1</version>
</dependency>
  </dependencies>

  <build>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean\_Lifecycle -->
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin\_bindings\_for\_jar\_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-jar-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
        <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site\_Lifecycle -->
        <plugin>
          <artifactId>maven-site-plugin</artifactId>
          <version>3.7.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-project-info-reports-plugin</artifactId>
          <version>3.0.0</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

Setup the unit testcode for this test

package com.learnautomatedtesting;

import static org.junit.Assert.assertTrue;

import java.net.MalformedURLException;
import java.net.URL;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import software.amazon.awssdk.auth.credentials.EnvironmentVariableCredentialsProvider;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.devicefarm.\*;
import software.amazon.awssdk.services.devicefarm.model.\*;

import org.openqa.selenium.Platform;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.remote.BrowserType;

	// Import the AWS SDK for Java 2.x Device Farm client:
	
	// in your tests ...
   
public class AppTest {
	  // ... When you set up your test suite
	  private static RemoteWebDriver driver;

	  @Test
	  public void setUp() {
		  DesiredCapabilities cap = new DesiredCapabilities();
		  cap.setCapability("browserName", "chrome");
		  cap.setCapability("version", "");
		//  cap.setCapability("platform", Platform.WIN10);
		  cap.setCapability("chrome.driver","78.0");
	     
		 
		  String myProjectARN = "arn:aws:devicefarm:us-west-2:xxxxxxxxxxx:testgrid-project:xxxxxxxxxxxxxxxxxxxxxxxx";
	    
	    DeviceFarmClient client  = DeviceFarmClient.builder().region(Region.US\_WEST\_2).build();
	    		
	    	//	.credentialsProvider(EnvironmentVariableCredentialsProvider.create())
	    		
	    	
	    CreateTestGridUrlRequest request = CreateTestGridUrlRequest.builder()
	      .expiresInSeconds(300)
	       .projectArn(myProjectARN)
	      .build();
	    
	  	    CreateTestGridUrlResponse response = client.createTestGridUrl(request);
	    URL testGridUrl = null;
		try {
			testGridUrl = new URL(response.url());
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    // You can now pass this URL into RemoteWebDriver.
	    WebDriver driver = new RemoteWebDriver(testGridUrl,cap);
		driver.manage().window().maximize();
    	driver.get("http://www.learnautomatedtesting.com");
    	
    	driver.navigate().to("https://www.learnautomatedtesting.com/0-selenium-tutorials/");
    	
     // 	driver.findElementByCssSelector("\[name$=q\]").sendKeys(strvalue);
    	
	    driver.quit();
    	
	  }
	  
	
	}

About the code: run device farm always on us-west-2, not another region as it is supported only in that AWS region for now. Make sure to setup your own preferable capabilities. Run the unit testcode and check in aws logs if it is was successfully ran

SUCCESSSS!