Migrating a Java test suite to Device Farm - Device Farm desktop browser testing

Migrating a Java test suite to Device Farm

Follow the steps in this topic to migrate your Java test suite to using the desktop browser testing feature with RemoteWebDriver.

Important

This topic is written with the assumption you are using the AWS SDK for Java 2.x and JUnit 5. If you are using the 1.x release of the SDK for Java, see the AWS SDK for Java API Reference. For more information about the SDK for Java 2.x, see the AWS SDK for Java 2.x API Reference. If you are using another testing framework (for example, TestNG), @BeforeAll and @AfterAll annotated methods are run at the start and end, respectively, of a test class.

To migrate your existing tests
  1. Add the AWS SDK for Java 2.x to your Maven pom.xml:

    <dependency> <groupId>software.amazon.awssdk</groupId> <artifactId>aws-sdk-java</artifactId> <version>2.10.60</version> </dependency>

    If you're using another build manager such as Gradle, see AWS SDK for Java 2.x Developer Guide and AWS SDK for Java 2.x on MVNRepository.

  2. Wherever you initialize a WebDriver instance, configure a RemoteWebDriver instance using the endpoint generated by the Device Farm API.

    1. Import the Device Farm classes:

      import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.devicefarm.DeviceFarmClient; import software.amazon.awssdk.services.devicefarm.model.CreateTestGridUrlRequest; import software.amazon.awssdk.services.devicefarm.model.CreateTestGridUrlResponse;
    2. Instantiate a new DeviceFarmClient where you create your WebDriver:

      public class myTestSuite { /** Set up the test suite. */ @BeforeAll public void setUp() { DeviceFarmClient client = DeviceFarmClient.builder().region(Region.US_WEST_2).build();
    3. Create a request for a signed WebDriver hub URL:

      CreateTestGridUrlRequest request = CreateTestGridUrlRequest.builder() .expiresInSeconds(300) // 5 minutes .projectArn("arn:aws:devicefarm:us-west-2:111122223333:testgrid-project:1111111-2222-3333-4444-555555555") .build();
    4. Get a response:

      URL testGridUrl = null; try { CreateTestGridUrlResponse response = client.createTestGridUrl(request); testGridUrl = new URL(response.url()); } catch (Exception e) { e.printStackTrace(); } Assertions.assertNotNull(testGridUrl);
    5. Create a DesiredCapabilities object to hold the capabilities you want or use a preconfigured set:

      DesiredCapabilities desired_capabilities = new DesiredCapabilities(); desired_capabilities.setCapability("browserName","firefox"); desired_capabilities.setCapability("browserVersion", "latest"); desired_capabilities.setCapability("platform", "windows"); // Or DesiredCapabilities desired_capabilities = DesiredCapabilities.firefox();
    6. Use RemoteWebDriver in place of your existing WebDriver implementation.

      driver = new RemoteWebDriver(testGridUrl, desired_capabilities);
    7. Make sure to close your session after the tests complete:

      @AfterAll public static void teardown() { driver.quit(); // tear down the driver and its parts. }
  3. Modify your environment to include your AWS access and secret keys. The steps vary depending on your configuration, but involve setting two environment variables:

    Important

    We recommend that you follow the standard security advice of granting least privilege—that is, granting only the permissions required to perform a task—when you configure the AWS SDK and AWS CLI with credentials. For more information, see AWS Security Credentials and IAM Best Practices.

    AWS_ACCESS_KEY_ID=AKIAIOSFODNN7EXAMPLE AWS_SECRET_ACCESS_KEY=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
  4. Run your tests.