Skip to main content

Writing Tests

WebdriverIO now also runs your tests through mobilecli, the tool that Mobilewright uses to drive devices. What changes is the protocol, not the API: you write tests with the same WebdriverIO API as for Appium, with $, $$, element commands and expect matchers. The Mobilewright API itself, such as locator.tap() or locator.fill(), is not supported. The engine is there to make mobile testing easier when you do not want to set up Appium, while your tests stay the same. If you know Mobilewright, Coming from Mobilewright shows where each part of its API is in WebdriverIO.

This page covers what is specific to the engine: which selectors it supports, how it taps and types, and what to do about the on-screen keyboard.

test/specs/login.e2e.ts
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 })
if (await browser.isKeyboardShown()) {
await browser.hideKeyboard()
}
await $('~button-LOGIN').click()
await expect($('~You are logged in!')).toHaveText('You are logged in!')
})
})

Selectors​

mobilecli has no element search of its own. The engine reads the UI tree of the screen and matches the selector itself. $ and $$ support these selectors today:

SelectoriOSAndroid
~login (accessibility id)The accessibility identifier, or the label of an element without one.The content-desc.
id=loginThe same as ~login.The resource-id: the whole id (com.example:id/login) or only its name (login).
XCUIElementTypeButton, android.widget.Button (class name)The element type.The class.

They match the same attributes as in Appium, so a selector that works with Appium finds the same element here. Prefer ~ with an accessibility id: it works on both platforms and survives changes to the layout.

More selectors are added in the next releases. Until then, any other selector, such as XPath, CSS, -ios predicate string, -ios class chain or -android uiautomator, fails with an invalid selector error that lists the supported ones:

The selector "//android.widget.Button" uses the strategy "xpath", which @wdio/mobilecli-driver does not support. Supported are: accessibility id, id, class name.
warning

Chained selectors like $('~list').$('~item') are not supported yet.

mobilecli leaves elements out of the UI tree that have no label, text or id and can not be used. Such an element can not be found. Give the elements that your tests use an accessibility id.

Tap​

mobilecli has no click. click() and tap() do the same thing: a tap in the middle of the element.

await $('~button-LOGIN').click()
await $('~button-LOGIN').tap()

Before the tap, the engine waits until the element is visible, enabled and no longer moving, see Waiting and timeouts.

info

An element that is not on the screen is not scrolled to. click() and tap() with options (a place in the element, a button, a duration) and browser.tap({ x, y }) are not supported yet.

Type and clear​

CommandWhat happens
$('~input-email').setValue('test@webdriver.io')Replaces what is in the field with the text.
$('~input-email').addValue('.nl')Adds the text to what is in the field.
$('~input-email').clearValue()Empties the field, and fails if the field still has a value after that.

mobilecli types into the field that has the focus, so the engine taps the element first. An element that already has the focus is not tapped again: addValue() then adds the text where the cursor is. On iOS a tap on a field that has the focus would open the edit menu and lose the text. Only string and number values are supported.

With { mask: true }, the text is replaced with **MASKED** in the logs and in the events for reporters, as with every protocol. mobilecli still gets the real text:

await $('~input-password').setValue(process.env.PASSWORD!, { mask: true })

The keyboard​

Typing opens the on-screen keyboard, and it stays open. mobilecli leaves the keyboard out of the UI tree, so the engine can not see that an element is behind it: a tap on that element hits the keyboard. Close the keyboard before you tap an element that it may cover:

if (await browser.isKeyboardShown()) {
await browser.hideKeyboard()
}
await $('~button-LOGIN').click()

hideKeyboard() takes no strategy: mobilecli decides how it closes the keyboard. It does nothing if no keyboard is open, and fails if the keyboard stays open. On a real iOS device it only works in an app that can be debugged.

Read an element​

CommandReturns
getText()The text of the element. If it has none, its label (the content-desc on Android). If it has none either, its value.
getValue()The value of the element, for example what is typed in a field. An empty string if it has none.
isDisplayed()Whether the element is on the screen and visible. false for an element that is not there.
isEnabled()Whether the element is enabled. false for an element that is not there.
info

getText() can return something else than with Appium, which returns the value before the label on iOS and never the content-desc on Android. Check getText() assertions when you move a suite from Appium.

The matchers of expect that use these commands work too:

await expect($('~You are logged in!')).toHaveText('You are logged in!')
await expect($('~button-LOGIN')).toBeDisplayed()
await expect($('~button-LOGIN')).toBeEnabled()

Page source​

browser.getPageSource() returns the UI tree as mobilecli gives it, as a JSON string. It is not the XML that Appium returns:

const { elements } = JSON.parse(await browser.getPageSource())
// [{ type: 'Button', label: 'General', name: 'com.apple.settings.general', rect: { x: 16, y: 293, width: 370, height: 52 }, children: [...] }, ...]

Use it to find the labels and ids of the elements on a screen while you write a test.

Next steps​

Welcome! How can I help?

WebdriverIO AI Copilot