Guides

Appium with JavaScript and WebdriverIO

A working WebdriverIO setup for an Android build on SpawnDroid, with the config, capabilities, test and CI command.

WebdriverIO speaks the W3C WebDriver protocol directly, so a SpawnDroid session needs no Appium-specific package beyond the WebdriverIO test runner. Only the endpoint and capabilities change from a local run.

Install

npm install --save-dev webdriverio @wdio/cli @wdio/local-runner @wdio/mocha-framework

The config

wdio.conf.js
const url = new URL(process.env.SPAWNDROID_WD_URL)

export const config = {
  runner: "local",
  specs: ["./test/**/*.test.js"],
  maxInstances: 1,
  capabilities: [
    {
      platformName: "Android",
      "appium:automationName": "UiAutomator2",
      "appium:udid": process.env.SPAWNDROID_UDID,
      "appium:appPackage": process.env.SPAWNDROID_APP_PACKAGE,
      "appium:autoGrantPermissions": true,
    },
  ],
  protocol: "https",
  hostname: url.hostname,
  port: 443,
  path: url.pathname,
  logLevel: "warn",
  framework: "mocha",
  reporters: ["spec"],
  mochaOpts: { timeout: 120_000 },
}

path is the session-specific /ci/sess_…/token/wd/hub path, not the stable hub path. Keep maxInstances: 1 unless you start one session per worker; a single session cannot be shared by two workers.

The test

test/checkout.test.js
import { browser, $, expect } from "@wdio/globals"

describe("checkout", () => {
  it("adds an item to the cart", async () => {
    await $("~Add to cart").click()
    await expect($("id=com.acme.checkout:id/total")).toBeDisplayed()
  })
})

~ is the accessibility id selector. id= takes the full Android resource id. Use browser.waitUntil or the element's waitForDisplayed instead of a fixed sleep when the next screen is asynchronous.

Run it

export SPAWNDROID_WD_URL='https://stream.spawndroid.io/ci/sess_…/token/wd/hub'
export SPAWNDROID_UDID='emulator-5554'
export SPAWNDROID_APP_PACKAGE='com.acme.checkout'
npx wdio run wdio.conf.js

WebdriverIO closes the session at the end of the run. For CI, the teardown result must be reported to SpawnDroid separately; the GitHub Action does that for you.

In CI

- uses: kl3jvi/spawndroid/actions/android-test@v1
  with:
    api-key: ${{ secrets.SPAWNDROID_API_KEY }}
    app-path: app/build/outputs/apk/debug/app-debug.apk
    command: npx wdio run wdio.conf.js

The action exports SPAWNDROID_WD_URL, SPAWNDROID_UDID and SPAWNDROID_APP_PACKAGE, so the same config works locally and in CI.

Run it on SpawnDroid

Start free with 200 device-minutes a month, or see the cloud Appium guide.

Was this page helpful?

On this page