What is expect-webdriverio?
The expect-webdriverio package is an assertion library designed to work with WebdriverIO, providing a set of matchers to make it easier to write assertions for WebdriverIO tests.
What are expect-webdriverio's main functionalities?
Element Matchers
This feature allows you to assert the state of elements in the DOM. For example, you can check if an element is displayed.
expect($('#element')).toBeDisplayed();
Attribute Matchers
This feature allows you to assert the attributes of elements. For example, you can check if an element has a specific class.
expect($('#element')).toHaveAttribute('class', 'my-class');
Text Matchers
This feature allows you to assert the text content of elements. For example, you can check if an element contains specific text.
expect($('#element')).toHaveText('Hello World');
Value Matchers
This feature allows you to assert the value of input elements. For example, you can check if an input field has a specific value.
expect($('#input')).toHaveValue('input value');
Condition Matchers
This feature allows you to assert the state of the browser. For example, you can check if the browser's URL matches a specific value.
expect(browser).toHaveUrl('https://example.com');
Other packages similar to expect-webdriverio
chai
Chai is a BDD / TDD assertion library for node and the browser that can be delightfully paired with any javascript testing framework. It provides a variety of assertion styles and is often used with WebdriverIO, but it does not have built-in support for WebdriverIO-specific matchers.
jest
Jest is a delightful JavaScript Testing Framework with a focus on simplicity. It provides a rich set of matchers and is often used for unit testing. While it can be used with WebdriverIO, it does not have built-in support for WebdriverIO-specific matchers.
assert
Assert is a simple assertion library provided by Node.js. It is minimalistic and does not provide the rich set of matchers that expect-webdriverio offers, especially for WebdriverIO-specific assertions.
expect-webdriverio
WebdriverIO Assertion library inspired by expect
Key Features
- waits for expectation to succeed
- detailed error messages
- works in Mocha, Cucumber, Jest, Jasmine
- builtin types for TypeScript and JS autocompletion
Installation
npm install expect
(Jasmine and Jest users should skip this step)npm install expect-webdriverio
NOTE: WebdriverIO v9.0.0
or higher is required!
Usage
Using WebdriverIO Testrunner
If you run your tests through the WDIO testrunner no additional setup is needed. WebdriverIO initialises expect-webdriverio
and makes expect
available in the global scope. So you can use it directly in your tests:
const $button = await $('button')
await expect($button).toBeDisplayed()
See more Examples
Using in a standalone script
If you embed WebdriverIO in a standalone script, make sure you import expect-webdriverio
before you use it anywhere.
import { remote } from 'webdriverio'
import { expect } from 'expect-webdriverio'
;(async () => {
const browser = await remote({
capabilities: {
browserName: 'chrome'
}
})
await browser.url('https://webdriver.io')
const $button = await browser.$('button')
await expect($button).toBeDisplayed()
await browser.deleteSession()
})().catch(console.error)
API
Please see API doc
Error messages
Error messages are informative out of the box and contain:
- full element selector, like
$('form').$('input')
- actual and expected values
- highlight the difference (texts assertions)
What's next?
First of all, feel free to raise an issue with your suggestions or help with PR!
Planned
- css matcher
- size matcher
- cookie / localStorage matchers?
- text regex matchers
- multiremote support (if requested)