Launch Appium tests in parallel on several devices

Reading Time: 2 minutes

It’s sometimes required to execute tests in parallel on several devices. In any case, this allows you to speed up you test campaign:

  • Execute the same test on different devices to validate your application wrt. market fragmentation
  • Execute the same test on the same type of device by changing execution parameters (network connection, user data…)
  • Execute a test campaign quickly by balancing tests on several devices of the same type
  • Allow several teams to execute their tests on a device farm

To do this, one Appium server must be run by device. We will use one port per server. In our example, we will use a nodesjs client with WebdriverIO (the node.js binding for the W3C WebDriver protocol).

Prerequisites

  • NodeJS globally installed on your system
  • Android SDK installed on your system
  • ANDROID_HOME in the system path
  • WebdriverIO installed
  • Appium installed
  • At least 2 devices or emulators connected for the test

Connection to the phones

Connect to the first phone:

adb connect <device name or IP>

Then, proceed similarly to connect the second phone.

Launching the servers

In a command line, launch the first server:

appium -p 4727 -bp 5727 -U <device name or IP>

p specifies the server port.
bp specifies the port to use to communicate on the device.
U is the device name (or, in case of a TCP connection, the IP).

In a second command line, launch the second server using different ports:

appium -p 4728 -bp 5728 -U <device name or IP>

You can check the logs in the command line outputs.

Launching test clients

It’s now time to write the node.js test to be executed. In a file test1.js, first configure the capabilities of the server.

const client = webdriverio.remote({
    host: "0.0.0.0",
    port : 4727, // Port of the first server
desiredCapabilities: {
        deviceName: <device name or IP>, // First device
        platformName: 'Android',
app: 'https://github.com/appium/sample-code/raw/master/sample-code/apps/ApiDemos/bin/ApiDemos-debug.apk'
     }
})

Then complete the file with the test to run:

;(async () => {
  try {
    await client.init()
    await sleep(1000)
    await client.click('~App')
    await client.click('~Alert Dialogs')
    await client.back()
    await client.back()
    await sleep(300)
    await sleep(2000)
    await client.end()
  } catch (err) {
    console.log(err)
  }
})()

function sleep (ms) {
  return new Promise(resolve => setTimeout(resolve, ms))
}

You can run this test to check it works correctly:

node test1.js

Then, you can copy this file to another file called test2.js, where the server and device parameters must be modified (port 4728 and deviceName).
In two separate windows, you can prepare the commands to launch the tests and execute them almost simultaneously. The tests are then executed in parallel on the devices.