Introduction to Mobile Commands in WebdriverIO
Testing mobile apps and mobile web applications comes with its own challenges, especially when dealing with platform-specific differences between Android and iOS. While Appium provides the flexibility to handle these differences, it often requires you to dive deep into complex, platform-dependent docs (Android, iOS) and commands. This can make writing test scripts more time-consuming, error-prone, and difficult to maintain.
To simplify the process, WebdriverIO introduces custom mobile commands tailored specifically for mobile web and native app testing. These commands abstract away the intricacies of underlying Appium APIs, enabling you to write concise, intuitive, and platform-agnostic test scripts. By focusing on ease of use, we aim to reduce the extra load while developing Appium scripts and empower you to automate mobile apps effortlessly.
Why Custom Mobile Commands?
1. Simplifying Complex APIs
Some Appium commands, like gestures or element interactions, involve verbose and intricate syntax. For example, executing a long press action with the native Appium API requires constructing an action
chain manually:
const element = $('~Contacts')
await browser
.action( 'pointer', { parameters: { pointerType: 'touch' } })
.move({ origin: element })
.down()
.pause(1500)
.up()
.perform()
With WebdriverIO's custom commands, the same action can be performed with a single, expressive line of code:
await $('~Contacts').longPress();
This drastically reduces boilerplate code, making your scripts cleaner and easier to understand.
2. Cross-Platform Abstraction
Mobile apps often require platform-specific handling. For instance, scrolling in native apps differs significantly between Android and iOS. WebdriverIO bridges this gap by providing unified commands like scrollIntoView()
that work seamlessly across platforms, regardless of the underlying implementation.
await $('~element').scrollIntoView();
This abstraction ensures your tests are portable and do not require constant branching or conditional logic to account for OS differences.