Using Device Farm desktop browser testing with Node.js - Device Farm desktop browser testing

Using Device Farm desktop browser testing with Node.js

Device Farm desktop browser testing can be used alongside the AWS SDK for JavaScript in Node.js and W3C WebDriver Specification compatible libraries, such as the official Selenium WebDriver package and Webdriver.IO.

Using the official Selenium WebDriver Node.js package

You can use Device Farm desktop browser testing with the Selenium Node.js selenium-webdriver package. The following example shows how to use the selenium-webdriver npm package to interact with Device Farm desktop browser testing.

Note

This example assumes you have the SDK for JavaScript in Node.js configured and installed. For more information, see AWS SDK for JavaScript in Node.js Getting Started Guide.

var webdriver = require('selenium-webdriver'); var AWS = require("aws-sdk"); var PROJECT_ARN = "arn:aws:devicefarm:us-west-2:111122223333:testgrid-project:123e4567-e89b-12d3-a456-426655440000"; var devicefarm = new AWS.DeviceFarm({ region: "us-west-2" }); (async () => { // Get the endpoint to create a new session const testGridUrlResult = await devicefarm.createTestGridUrl({ projectArn: PROJECT_ARN, expiresInSeconds: 600 }).promise(); console.log("Created url result:", testGridUrlResult); runExample(testGridUrlResult.url); })().catch((e) => console.error(e)); var runExample = async (urlString) => { console.log("Starting WebDriverJS remote driver") driver = await new webdriver.Builder() .usingServer(urlString) .withCapabilities({ browserName: 'chrome' }) .build(); console.log("New session created:", driver.getSession()); await driver.get('https://aws.amazon.com/'); const title = await driver.getTitle(); console.log('Title was: ' + title); console.log("Deleting session..."); await driver.quit(); }

Using Webdriver.IO

Device Farm desktop browser testing is supported in Webdriver.IO with some slight configuration changes. The following two examples show how to use Device Farm desktop browser testing with and without the Mocha test framework.

Using Webdriver.IO and Device Farm desktop browser testing without Mocha

Using Device Farm desktop browser testing with Webdriver.IO does not require the inherent use of the Mocha test suite. The following example demonstrates how to configure the Webdriver.IO API for use with Device Farm desktop browser testing and interact with a browser session.

Note

This example assumes you have the SDK for JavaScript in Node.js configured and installed. For more information, see AWS SDK for JavaScript in Node.js Getting Started Guide.

const webdriverio = require('webdriverio'); const AWS = require("aws-sdk"); var PROJECT_ARN = process.env.TEST_GRID_PROJECT var devicefarm = new AWS.DeviceFarm({ region: "us-west-2" }); (async () => { // Get a new signed WebDriver hub URL. const testGridUrlResult = await devicefarm.createTestGridUrl({ projectArn: PROJECT_ARN, expiresInSeconds: 600 }).promise(); console.log("Created url result:", testGridUrlResult); runExample(testGridUrlResult.url); })().catch((e) => console.error(e)); var runExample = async (urlString) => { var url = new URL(urlString) console.log("Starting a new remote driver") const browser = await webdriverio.remote({ logLevel: 'trace', hostname: url.host, path: url.pathname, protocol: 'https', port: 443, connectionRetryTimeout: 180000, capabilities: { browserName: 'chrome', } }) await browser.url('https://aws.amazon.com/') const title = await browser.getTitle() console.log('Title was: ' + title) await browser.deleteSession() }

Integrating Device Farm desktop browser testing into Mocha and Webdriver.IO test suites

To integrate Device Farm into your existing Mocha and Webdriver.IO test suite, you'll need to add support for the Device Farm desktop browser testing service backend. The following steps will guide you through the process:

  1. Install the SDK for JavaScript in Node.js. For more information, see AWS SDK for JavaScript in Node.js Getting Started Guide

    $ npm install aws-sdk
  2. 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
  3. Add services/devicefarm.service.js with the following:

    const AWS = require("aws-sdk") class AwsDeviceFarmService { constructor() { this.projectArn = process.env.TEST_GRID_PROJECT this.devicefarm = new AWS.DeviceFarm({ region: "us-west-2" }) } // Get a test grid URL once for all test suites / test cases during this wdio testrunner invocation onPrepare(config, capabilities) { return new Promise((resolve, reject) => this.devicefarm.createTestGridUrl({ projectArn: this.projectArn, expiresInSeconds: 300 }, (err, createTestGridUrlResult) => { if (err) { return reject(err) } console.log("CreateTestGridUrlResult:", createTestGridUrlResult) process.env.AWS_DEVICE_FARM_SERVICE_TEST_GRID_URL = createTestGridUrlResult.url resolve() })) } // Set the connection settings on the beforeSession hook. See more: https://github.com/webdriverio/webdriverio/issues/5113 beforeSession(config, capabilities, specs) { return new Promise((resolve, reject) => { // Set the default connection timeout to at least 3 minutes config.connectionRetryTimeout = 180000 // millis const testGridUrl = new URL(process.env.AWS_DEVICE_FARM_SERVICE_TEST_GRID_URL) config.protocol = 'https' config.hostname = testGridUrl.host config.path = testGridUrl.pathname config.port = 443 resolve() }) } } module.exports = AwsDeviceFarmService
  4. Set the environment variable TEST_GRID_PROJECT to the ARN of your project.

  5. Modify your wdio.conf.js

    const AwsDeviceFarmService = require('./service/devicefarm.service') exports.config = { runner: 'local', services: [ // Use the devicefarm.service.js to automatically generate a remote URL // that will be used to create all web drivers from this testrunner [AwsDeviceFarmService] ], // Define which test specs should run. specs: [ './test/specs/**/*.js' ], capabilities: [{ browserName: 'firefox', }], // Level of logging verbosity: trace | debug | info | warn | error | silent logLevel: 'info', }
  6. Your tests will now run using Device Farm desktop browser testing!