

# Migrating a Ruby test suite to Device Farm desktop browser testing
<a name="testing-frameworks-ruby"></a>

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

**To migrate your existing tests**

1. Install the appropriate gems:

   ```
   gem install selenium-webdriver
   gem install aws-sdk-devicefarm
   ```

   If you are using Bundler, add the following to your Gemfile:

   ```
   gem 'selenium-webdriver'
   gem 'aws-sdk-devicefarm'
   ```

   and update your installed gems with 

   ```
   bundle install
   ```

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:

      ```
      require 'aws-sdk-devicefarm'
      require 'selenium-webdriver'
      ```

   1. Instantiate a new `DeviceFarm::Client` where you create your `WebDriver`

      ```
      devicefarm = Aws::DeviceFarm::Client.new(region: 'us-west-2')
      ```

   1. Get a signed `WebDriver` hub URL:

      ```
      test_grid_url_response = devicefarm.create_test_grid_url(
          project_arn: "arn:aws:devicefarm:us-west-2:{{111122223333}}:testgrid-project:{{123e4567-e89b-12d3-a456-426655440000}}", 
          expires_in_seconds: 300)
      remote_url = test_grid_url_response.url
      ```

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

      ```
      capabilities = Selenium::WebDriver::Remote::Capabilities.firefox
      ```

   1. Create your `RemoteWebDriver` in place of your existing `GeckoDriver`, `ChromeDriver`, or similar. To avoid timeouts, create a new HTTP client with an extended timeout:

      ```
      client = Selenium::WebDriver::Remote::Http::Default.new
      client.read_timeout = 180 # seconds - make sure to set a timeout here of at least 3 minutes (default is 60 seconds)
      driver = Selenium::WebDriver.for :remote, http_client: client, url: remote_url, desired_capabilities: capabilities
      ```
**Note**  
If you do not set an increased timeout, the session creation will time out before your session is ready.

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

      ```
      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. They're on Device Farm\!

For more information, see [AWS SDK for Ruby Developer Guide](https://docs.aws.amazon.com/sdk-for-ruby/latest/developer-guide/). 