

# Migrating a Java test suite to Device Farm
<a name="testing-frameworks-java"></a>

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](https://docs.aws.amazon.com/sdk-for-java/latest/reference/). For more information about the SDK for Java 2.x, see the [AWS SDK for Java 2.x API Reference](https://docs.aws.amazon.com/sdk-for-java/latest/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](https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/) and [AWS SDK for Java 2.x on MVNRepository](https://mvnrepository.com/artifact/software.amazon.awssdk/aws-sdk-java/).

1. 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;
      ```

   1. 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();
      ```

   1. 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();
      ```

   1. 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);
      ```

   1. 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();
      ```

   1. Use `RemoteWebDriver` in place of your existing `WebDriver` implementation.

      ```
              driver = new RemoteWebDriver(testGridUrl, desired_capabilities);
      ```

   1. Make sure to close your session after the tests complete:

      ```
          @AfterAll
          public static void teardown() {
              driver.quit();      // tear down the driver and its parts. 
          }
      ```

1. 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](https://docs.aws.amazon.com//general/latest/gr/aws-security-credentials.html) and [IAM Best Practices](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html).

   ```
   AWS_ACCESS_KEY_ID=AKIAIOSFODNN7EXAMPLE
   AWS_SECRET_ACCESS_KEY=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
   ```

1. Run your tests.