Parallel Testing with Selenium Webdriver

Reading Time : 6min read
Illustration of Parallel Testing with Selenium Webdriver

I. Introduction

Illustration of Kobiton's parallel testing

Parallel testing is one of the main features of the Kobiton Cloud service, giving users the ability to run concurrent tests. Instead of waiting for each test to execute individually, users can run multiple tests in parallel across various browser, device, and OS combinations. In this way, parallel testing will help you reduce the run time of your test suite, resulting in faster build times and quicker release cycles.

In this article, we will teach you how to run your own tests using Selenium/WebDriver and a testing library such as selenium-webdriver for Node. We will also examine how to integrate your local testing environment with commercial tools like Kobiton.

II. Prerequisites

  brew install node

III. Create an Automated Test Case for Android/ iOS web

Test Scenario

In this tutorial, we will create a test case for the following scenario:

  1. Go to Google on Chrome browser using an Android device
  2. Enter “Kobiton” into the Search field
  3. Click Search
  4. Check if the title contains the keyword “Kobiton”

Installation

In this tutorial, we will use a sample test script provided in the Kobiton Sample Repository. Let’s download a sample script here.

  1. Open the Terminal application and enter the folder to which you would like to copy the repository. Ex. “/Documents”
  cd ~/Documents/
  1. Clone the repository to the current folder by running the following command:
  git clone https://github.com/kobiton/samples.git
  1. Navigate to the “samples/javascript” folder (this is the test project folder).
  cd samples/javascript
  1. Then run the following command to install all libraries in the sample:
  npm install

Inside the project folder, we can see multiple-devices-test.js with the following contents:

import 'babel-polyfill'
import {assert} from 'chai'
import Promise from 'bluebird'
import parallel from 'mocha.parallel'
import {Builder, By, Key, until} from 'selenium-webdriver'
const kobitonServerUrl = 'https://api.kobiton.com/wd/hub'
const username = 'your Kobiton username'
const apiKey = 'your Kobiton api key'
let desiredCapabilities = [{
  sessionName:        'Automation test session on first device',
  sessionDescription: 'This is an example for Android web',
  deviceOrientation:  'portrait',
  captureScreenshots: true,
  browserName:        'chrome',
  deviceGroup:        'KOBITON',
  deviceName:         'Galaxy S5',
  platformName:       'Android'
  }, {
  sessionName:        'Automation test session on second device',
  sessionDescription: 'This is an example for iOS web',
  deviceOrientation:  'portrait',
  captureScreenshots: true,
  browserName:        'safari',
  deviceGroup:        'KOBITON',
  deviceName:         'iPhone X',
  platformName:       'iOS'
}]
let driver
const buildDriver = (_caps) => {
  const caps = Object.assign({}, _caps)
  caps.username = username
  caps.accessKey = apiKey
  return new Builder()
    .usingServer(kobitonServerUrl)
    .withCapabilities(caps)
    .build()
}
parallel('Parallel Tests',() => {
  it('should return the title that contains Kobiton on first device', async () => {
    const driver = buildDriver(desiredCapabilities[0])
    await run(driver)
  })
  it('should return the title that contains Kobiton on second device', async () => {
    const driver = buildDriver(desiredCapabilities[1])
    await run(driver)
  })
  async function run(driver) {
    try {
      await driver.get('https://www.google.com')
      await driver.findElement(By.name('q')).sendKeys('Kobiton', Key.RETURN)
      await driver.wait(until.titleContains('Kobiton'), 2000)
      let msg = await driver.getTitle()
      assert.include(msg, 'Kobiton')
    }
    finally {
      if (driver != null) {
        await driver.quit()
      }
    }
  }
})

Now you can use this code to execute your tests with Kobiton Cloud service.

Libraries

This sample test script uses several test frameworks and libraries, including Mocha, Chai, and selenium-webdriver.

Mocha – a JavaScript test framework for Node.js and the browser.

Chai – a BDD / TDD assertion library for Node and the browser.

Selenium WebDriver – Selenium is a browser automation library. Most often used for testing web-applications, Selenium may be used for any task that requires automating interaction with the browser.

IV. Configure Test Scripts for Kobiton

We have two ways to get your username and API Key to replace the username and API Key in the multiple-devices-test.js file.

Method #1:

  • Go to your Profile page to retrieve your account username
  • Then, go to the Settings page to retrieve your API key

Method #2:

  • After logging in, you should be on the Devices page. Hover over the device you want to test and select “Show automation settings.”
  • Select NodeJS as the language.
  • Replace the username and apiKey in the multiple-devices-test.js file.
  • In the example, the username and apiKey are set as follows:
  const username = 'joeybrown'
  const apiKey = ‘7c3fa781-xxxx-xxxx-xxxx-294509985d05'

In this scenario, I chose one Android device (Galaxy S5) and one iOS device (iPhone X) to run tests in parallel. We need to edit these desiredCapabilities to indicate the target device you want to test.

image of automation settings for Galaxy S5
automation settings for iPhone X image

Edit the desiredCapabilities to indicate your target test device.

desiredCapabilities = [{
  sessionName:        'Automation test session on first device',
  sessionDescription: 'This is an example for Android web',
  deviceOrientation:  'portrait',
  captureScreenshots: true,
  browserName:        'chrome',
  deviceGroup:        'KOBITON',
  deviceName:         'Galaxy S5',
  platformName:       'Android'
  }, {
  sessionName:        'Automation test session on second device',
  sessionDescription: 'This is an example for iOS web',
  deviceOrientation:  'portrait',
  captureScreenshots: true,
  browserName:        'safari',
  deviceGroup:        'KOBITON',
  deviceName:         'iPhone X',
  platformName:       'iOS'
}]

V. Execute the Test

Once you have everything set up, you can execute the test simply by running the following command:

npm run multiple-devices-test

VI. Test Results

After the script is successfully executed in the Terminal, you can access https://portal.kobiton.com/sessions to examine the test results. You should now see two completed test sessions with near-identical timestamps, indicating that they were run in parallel to one another.

image of kobiton test results

VII. Conclusions

As you can see, running tests on remote servers isn’t much more difficult than running them locally. You just need to create your driver instance (as usual), but with a few more specific features, including the capabilities of the device you want to test on, the address of the server, and the user credentials you need (if any) to access it.

In addition, doing parallel testing with Selenium Webdriver has many benefits, such as a decrease in the execution time of the test suite and tighter feedback loops provided by your tests.

If you have a choice, always execute your parallel tests in the cloud. The benefits that you receive, i.e. logs (device log & appium log), videos and screenshots, all make you more efficient by helping you debug faster, allowing you to spend more time actually covering new functionality.

If you’re new to test automation or happy with inefficiency, you can execute tests one at a time. But if you consider yourself somewhat an expert looking to save time and energy, automate whatever tests you want and have them run in parallel. You can thank us later for the tip.

If you haven’t already, get started with Kobiton today by signing up for a free trial, and see just how productive your test automation operation can become.

Interested in Learning More?

Subscribe today to stay informed and get regular updates from Kobiton

Ready to accelerate delivery of
your mobile apps?

Request a Demo