

# Migrating Python tests to Device Farm desktop browser testing
Python

 Follow the steps in this topic to get your tests running on Python. 

**Important**  
This topic is written with the assumption you are using Python 3, Boto3, and pytest. As of January 1, 2020, Python 2 is officially no longer supported. For more information, see [Sunsetting Python 2](https://www.python.org/doc/sunset-python-2/) from the Python Software Foundation. 

**To migrate your existing tests**

1. Add the AWS SDK for Python (Boto3) to your requirements. If you're using pipenv for your requirements management, run the following:

   ```
   pipenv install boto3
   ```

   If you're using pip, add the following to your `requirements.txt`:

   ```
   boto3 >= 1.10.44
   ```

   Make sure that the version listed here is correct and then run `pip install -r requirements.txt` to install the Boto3.

1. Modify your test suite to use RemoteWebDriver. 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 boto3
      from selenium.webdriver import DesiredCapabilities
      from selenium.webdriver import Remote
      ```

   1. Instantiate a new `DeviceFarmClient` where you create your `WebDriver`

      ```
      class MyPytestTests():
        def setup_method(self, method):
          # step 2: Set up a client for boto3
          # The AWS_ACCESS_KEY and AWS_SECRET_KEY will be inferred from the command line.
          devicefarm_client = boto3.client("devicefarm")
      ```

   1. Get a signed `WebDriver` hub URL:

      ```
          testgrid_url_response = devicefarm_client.create_test_grid_url(
            projectArn= "arn:aws:devicefarm:us-west-2:111122223333:testgrid-project:123e4567-e89b-12d3-a456-426655440000",
            expiresInSeconds=300
            )
      ```

   1. Use the `DesiredCapabilities` class to specify the browser to test against:

      ```
          desired_capabilities = DesiredCapabilities.FIREFOX
          desired_capabilities["platform"] = "windows"
      ```

   1. Create your `RemoteWebDriver` in place of your existing `GeckoDriver`, `ChromeDriver`, or similar. 

      ```
          self.driver = Remote(testgrid_url_response['url'], desired_capabilities)
      ```

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

      ```
        def teardown_method(self, method):
          self.driver.quit()
      ```

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, including the following: 

   ```
   pytest -s 
   ```

For more information, see [ SDK for Python (Boto3) API Reference](https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/index.html). 