Mobilewright Engine
The Mobilewright engine runs your WebdriverIO tests on Android and iOS through mobilecli, the device automation tool that Mobilewright is built on. You keep writing tests with the WebdriverIO API ($, click, setValue, expect) and the testrunner. Only the automation protocol in wdio.conf.ts changes. The engine is an alternative to Appium for native apps and comes in the @wdio/mobilecli-driver package.
The Mobilewright engine is in beta:
- It runs on local emulators, simulators and devices only. Support for device clouds is in progress.
- It finds elements by accessibility id, id and class name. More selectors are added in the next releases.
- It taps, types, reads text and closes the keyboard. Gestures, screenshots and webviews are not supported yet.
See Supported commands for the full list.
One test suite, two protocols​
WebdriverIO keeps your tests apart from the protocol that runs them. The automationProtocol option picks the driver:
automationProtocol | Runs on |
|---|---|
webdriver (default) | WebDriver BiDi and WebDriver: browsers, and mobile apps through Appium |
@wdio/mobilecli-driver | The Mobilewright engine: mobile apps through mobilecli |
A spec that uses the supported commands runs on both without a change. The selectors match the same attributes as in Appium: ~login is the accessibility id on iOS and the content-desc on Android in both. To move a suite to the Mobilewright engine, or to try it next to Appium, you add a second config file. See Run the same tests with Appium.
When to use it​
The Mobilewright engine is a good fit when:
- You want a short setup. mobilecli is one tool, installed with npm. You do not install an Appium server or an Appium driver per platform.
- You want every action to wait for its element. Before a tap or typing, the engine waits until the element is visible, enabled and no longer moving. A read waits until the element is visible. On top of that, WebdriverIO waits for the element to exist, as it does with every protocol. Your tests need no waits of their own.
- Your tests read a lot from one screen. Finds and reads on a screen that did not change share one read of the screen. Four
getText()calls in a row cost one read. - You know Mobilewright. Every command makes the same mobilecli calls as its Mobilewright counterpart and waits by the same rules. See Coming from Mobilewright.
Use Appium when you need a device cloud, gestures, XPath, screenshots, webviews or any other command that is not supported yet. Appium stays the default for mobile testing in WebdriverIO.
Quick start​
Prerequisites​
mobilecli is a separate tool with its own license (FSL-1.1-ALv2). WebdriverIO does not install it or depend on it. Install it, check that it sees your device, and start its server:
npm install -g mobilecli
mobilecli devices # lists the devices that mobilecli sees
mobilecli server start # listens on http://localhost:12000
The emulator, simulator or device must be running: the engine does not boot it. A real iOS device also needs the mobilecli agent, which you install once with mobilecli agent install --device <id>.
Install​
npm install --save-dev @wdio/cli @wdio/local-runner @wdio/mocha-framework @wdio/spec-reporter @wdio/mobilecli-driver tsx
{
"compilerOptions": {
"types": ["node", "@wdio/globals/types", "@wdio/mocha-framework"]
}
}
Configure​
This example uses the WebdriverIO demo app. Download android.wdio.native.app.v2.2.0.apk into an apps folder in your project, and start an Android emulator.
import path from 'node:path'
export const config: WebdriverIO.Config = {
runner: 'local',
automationProtocol: '@wdio/mobilecli-driver',
// where `mobilecli server start` runs
hostname: 'localhost',
port: 12000,
specs: ['./test/specs/**/*.ts'],
// one device, so one test file at a time
maxInstances: 1,
capabilities: [{
platformName: 'Android',
'mobilecli:app': path.resolve(import.meta.dirname, 'apps', 'android.wdio.native.app.v2.2.0.apk')
}],
framework: 'mocha',
reporters: ['spec'],
mochaOpts: {
ui: 'bdd',
timeout: 60000
}
}
The session installs the app, starts it and waits until it is in the foreground. With more than one device online, add 'mobilecli:udid' with an id from mobilecli devices. All capabilities and settings are on the Configuration page.
Write a test​
import { browser, expect, $ } from '@wdio/globals'
describe('Login', () => {
it('logs in with valid credentials', async () => {
await $('~Login').click()
await $('~input-email').setValue('test@webdriver.io')
await $('~input-password').setValue('Test1234!', { mask: true })
// the keyboard covers the login button
if (await browser.isKeyboardShown()) {
await browser.hideKeyboard()
}
await $('~button-LOGIN').click()
await expect($('~You are logged in!')).toHaveText('You are logged in!')
})
})
Run​
npx wdio run ./wdio.conf.ts
Run the same tests with Appium​
Keep the specs and add a second config file for Appium. Only the protocol, the port and the capabilities change:
import path from 'node:path'
export const config: WebdriverIO.Config = {
runner: 'local',
// automationProtocol defaults to 'webdriver'
port: 4723,
services: ['appium'],
specs: ['./test/specs/**/*.ts'],
maxInstances: 1,
capabilities: [{
platformName: 'Android',
'appium:automationName': 'UiAutomator2',
'appium:app': path.resolve(import.meta.dirname, 'apps', 'android.wdio.native.app.v2.2.0.apk')
}],
framework: 'mocha',
reporters: ['spec'],
mochaOpts: {
ui: 'bdd',
timeout: 60000
}
}
npx wdio run ./wdio.conf.ts # Mobilewright engine
npx wdio run ./wdio.appium.conf.ts # Appium
The Appium config needs appium, @wdio/appium-service and the UiAutomator2 driver, see Mobile Apps. Stop one run before you start the other: Appium and mobilecli can not drive the same Android device at the same time.
Troubleshooting​
Could not reach the mobilecli server at http://localhost:12000/rpc.: the server is not running, or runs somewhere else. Start it withmobilecli server start, and checkhostnameandportin your config.multiple devices foundwithIn WebdriverIO, set the capability "mobilecli:udid" to one of them.: more than one device is online. Add'mobilecli:udid'with an id frommobilecli devices.The capability "platformName" is "iOS", but the device emulator-5554 is an Android device.: the device that mobilecli picked is of the other platform. Set'mobilecli:udid'to a device of the platform you want.The selector "//android.widget.Button" uses the strategy "xpath", which @wdio/mobilecli-driver does not support.: use an accessibility id (~), an id (id=) or a class name. See Selectors.The command "takeScreenshot" is not supported by @wdio/mobilecli-driver: the command is not built yet. See Supported commands, or run this test with Appium.The app com.wdiodemoapp was started, but it was not in the foreground within 20000ms: the app takes longer to start, which is common on a real device. RaiseappLaunchTimeoutinmobilecli:options, see Apps.- A tap does nothing, or hits the wrong element: the on-screen keyboard may cover the element. mobilecli leaves the keyboard out of the UI tree, so the engine can not see that the element is behind it. Close the keyboard with
browser.hideKeyboard()first, see The keyboard.
Next steps​
- Configuration: the server address, capabilities, apps and settings.
- Writing tests: selectors, taps, typing, the keyboard and reading elements.
- Waiting and timeouts: what each command waits for, and how long.
- Supported commands: what works today and what does not yet.
- Coming from Mobilewright: the Mobilewright API next to the WebdriverIO API.