Using Device Farm desktop browser testing in Python - Device Farm desktop browser testing

Using Device Farm desktop browser testing in 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 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.

  2. 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
    2. 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")
    3. 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 )
    4. Use the DesiredCapabilities class to specify the browser to test against:

      desired_capabilities = DesiredCapabilities.FIREFOX desired_capabilities["platform"] = "windows"
    5. Create your RemoteWebDriver in place of your existing GeckoDriver, ChromeDriver, or similar.

      self.driver = Remote(testgrid_url_response['url'], desired_capabilities)
    6. Make sure to close your session after the tests are complete:

      def teardown_method(self, method): self.driver.quit()
  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, including the following:

    pytest -s

For more information, see SDK for Python (Boto3) API Reference.