expect-webdriverio extends Jest's expect API with WebDriverIO-specific enhancements. It can be used standalone or within other testing environments.
It is highly recommended to use this package with the WDIO Testrunner and a compatible framework adapter, which together provide a plug-and-play experience.
Pair it with your preferred framework using the appropriate adapter:
- Mocha: Use
@wdio/mocha-framework - Cucumber: Use
@wdio/cucumber-framework - Jasmine: Use
@wdio/jasmine-framework - Jest: Works (no framework exists; requires manual configuration—see note below)
Note: When using Jest, or when running outside of the WDIO Testrunner without a compatible framework adapter, additional manual configuration may be required, such as adding types to your
tsconfig.jsonand configuring WDIO matchers, soft assertions, and the snapshot service.
Example playgrounds are available, though their tsconfig.json files may use modified configurations for development purposes.
- See Mocha, Jasmine, and Jest examples here.
When pairing with Mocha, you can use expect-webdriverio directly or combine it with chai (or any other assertion library).
- It is strongly recommended to leverage
@wdio/mocha-frameworkfor automatic configuration and a plug-and-play experience. - See Mocha playground example here
No import is required; everything is set globally.
describe('My tests', async () => {
it('should verify my browser to have the expected url', async () => {
await expect(browser).toHaveUrl('https://example.com')
})
}) Minimum types expected in tsconfig.json:
When depending on @wdio/mocha-framework
{
"compilerOptions": {
"types": [
"@wdio/mocha-framework",
"@wdio/globals/types"
]
}
}When not depending on @wdio/mocha-framework
{
"compilerOptions": {
"types": [
"@types/mocha",
"expect-webdriverio/expect-global"
]
}
}expect-webdriverio can coexist with the Chai assertion library by importing both libraries explicitly.
See also this documentation.
You can use expect-webdriverio with Jest by leveraging either @types/jest (which provides global ambient support) or @jest/globals alone.
- Note: Jest maintainers do not officially support
@types/jest. Should this package become outdated or experience issues, support may be dropped. - Note: With Jest, the matchers
toMatchSnapshotandtoMatchInlineSnapshotare overloaded. To resolve the types correctly,expect-webdriverio/jestmust be listed last. - Note: WebdriverIO does not provide a compatible framework adapter for Jest; manual configuration is required.
When paired with @types/jest, no imports are required in your test files. Global ambient types are already defined correctly, allowing you to use Jest's expect directly after some manual configuration.
- Note:
jestandts-jestare also required. - See the Jest with
@types/jestplayground example.
Since no WDIO Testrunner and framework adapter are used, additional prerequisite configuration is required.
import { expect } from "expect-webdriverio";
(globalThis as any).expect = expect;If not already set, define a file path for setupFilesAfterEnv in your Jest configuration:
setupFilesAfterEnv: ['./jest.setup.after-env.ts'],Then, add the following configuration to your jest.setup.after-env.ts file:
import { expect } from "@jest/globals";
import { wdioCustomMatchers } from "expect-webdriverio";
beforeAll(async () => {
// Extend the imported Jest expect instance with WDIO matchers
expect.extend(wdioCustomMatchers);
});import { expect } from "@jest/globals";
import { expect as wdioExpect } from "expect-webdriverio";
beforeAll(async () => {
for (const name of ["soft", "getSoftFailures", "assertSoftFailures", "clearSoftFailures"] as const) {
Object.defineProperty(expect, name, { value: wdioExpect[name] });
}
});Then, as shown below, no imports are required and we can use WDIO matchers directly on Jest's expect:
describe('My tests', async () => {
it('should verify my browser to have the expected url', async () => {
await expect(browser).toHaveUrl('https://example.com')
})
}) Expected in tsconfig.json:
{
"compilerOptions": {
"types": [
"@types/jest",
"expect-webdriverio/jest" // Must be last for overloaded matchers `toMatchSnapshot` and `toMatchInlineSnapshot`
]
}
}When using @jest/globals directly instead of global ambient types, you explicitly import Jest's utilities. To use expect-webdriverio you have two approaches:
- Note: No example playground available
import { expect } from 'expect-webdriverio'
import { describe, it, expect as jestExpect } from '@jest/globals'
describe('My tests', async () => {
it('should verify my browser to have the expected url', async () => {
await expect(browser).toHaveUrl('https://example.com')
// Jest's native expect is still usable
jestExpect(myFunction).toHaveBeenCalled()
})
}) No types are expected in tsconfig.json.
To avoid explicitly importing expect from expect-webdriverio in every test file, add the global entry point to your tsconfig.json:
{
"compilerOptions": {
"types": ["expect-webdriverio/expect-global"]
}
}Unlike @types/jest, @jest/globals does not export a global namespace that can be easily extended. While module augmentation is possible, it does not support inheriting matchers via the extends keyword. Supporting it would require manually duplicating all expect-webdriverio matcher interfaces inside the module declaration.
This limitation is a known upstream issue tracked in Jest.
On $$(), the expected value decides how the texts (or other values) of the elements are compared. These are the matchers of the expect package, so they are the same with Mocha. Results with toHaveText on $$('li'):
| Expected value | Passes when | Order | Other elements | String options (trim, ignoreCase…) |
|---|---|---|---|---|
['Coffee', 'Tea'] |
each element has the text at its index | fixed | no: same count | applied |
'Tea' |
every element has the text | - | no | applied |
expect.oneOf('Tea', 'Coffee') |
each element has one of the texts; a text can be missing | any | no | applied |
expect.arrayContaining(['Tea', 'Coffee']) |
each text is on at least one element | any | allowed | not applied |
expect.not.arrayContaining(['Milk']) |
at least one of the texts is on no element | any | allowed | not applied |
expect.arrayOf(expect.stringMatching(/^(Tea|Coffee)$/)) |
every element matches the matcher; also passes with no element | any | no | not applied |
some($$('li')) with 'Tea' |
at least one element has the text | any | allowed | applied |
A list matcher (arrayContaining, arrayOf) compares the raw values of all the elements at once. The string options do not change these values: use a nested matcher, e.g. expect.stringMatching(/tea/i). A list matcher throws an error with some(), which checks each element alone.
// <li>Coffee</li> <li>Tea</li>
await expect($$('li')).toHaveText(['Coffee', 'Tea']) // passes
await expect($$('li')).toHaveText(['Tea', 'Coffee']) // fails: the order is different
await expect($$('li')).toHaveText(expect.oneOf('Tea', 'Coffee')) // passes
await expect($$('li')).toHaveText(expect.arrayContaining(['Tea'])) // passes: Coffee is allowed
await expect($$('li')).toHaveText(expect.not.arrayContaining(['Milk'])) // passes
await expect($$('li')).toHaveText(expect.arrayOf(expect.stringMatching(/^(Tea|Coffee)$/))) // passes
await expect(some($$('li'))).toHaveText('Tea') // passes
// <li>Tea</li> <li>Tea</li>
await expect($$('li')).toHaveText(expect.arrayContaining(['Tea', 'Coffee'])) // fails: Coffee is missing
// <li> Coffee </li> <li> Tea </li>
await expect($$('li')).toHaveText(['Coffee', 'Tea']) // passes: the default trim applies
await expect($$('li')).toHaveText(expect.arrayContaining(['Coffee'])) // fails: the value is " Coffee "When paired with Jasmine, @wdio/jasmine-framework is required to ensure proper runtime configuration. The adapter registers the WDIO matchers with addAsyncMatchers and sets a hybrid global expect:
- Jasmine synchronous matchers (
toBe,toEqual, ...) stay synchronous and returnvoid. - WDIO matchers and Jasmine async matchers (
toBeResolved, ...) go toexpectAsyncand return a promise: alwaysawaitthem.
Jasmine collects the failures until the spec ends (built-in soft assertions), so the expect-webdriverio SoftAssertion service is neither required nor supported.
@wdio/jasmine-framework v10 has the types of its hybrid expect.
describe('My tests', async () => {
it('should verify my browser to have the expected url', async () => {
await expect(browser).toHaveUrl('https://example.com')
// Jasmine synchronous matcher
expect(true).toBe(true)
})
})Expected in tsconfig.json:
{
"compilerOptions": {
"types": [
"@types/jasmine",
"@wdio/globals/types",
"@wdio/jasmine-framework"
]
}
}expectAsync also has the WDIO matchers, e.g. await expectAsync(browser).toHaveUrl('https://example.com'), typed since @wdio/jasmine-framework 10.0.2.
Since @wdio/jasmine-framework 10.0.2, expect.extend() adds custom matchers to the global expect. Call it before the tests run: at the top level of a spec file, or in the before hook of the configuration, not in a beforeAll(), because the framework reads the matchers before the first beforeAll() of the specs. The matchers become Jasmine async matchers, so await them. The types of the global expect have extend() since @wdio/jasmine-framework 10.1.0 (webdriverio#15982). With 10.0.2, call it on import { expect } from '@wdio/globals', which has the type and adds the matchers to the same global expect. See Custom Matchers and the playground spec.
Note: When using Jasmine, Jest's expect matchers are not leveraged, meaning standard Jest-specific assertion matchers are unavailable.
With Jasmine 6 alone, register the WDIO matchers on expectAsync yourself, and add expect-webdriverio/jasmine to types. Do not use expect-webdriverio/jasmine with @wdio/jasmine-framework, which has its own types.
describe('My tests', async () => {
it('should verify my browser to have the expected url', async () => {
await expectAsync(browser).toHaveUrl('https://example.com')
// Standard Jasmine async matchers work as expected
await expectAsync(Promise.resolve(true)).toBeResolvedTo(true)
})
})Expected in tsconfig.json:
{
"compilerOptions": {
"types": [
"@types/jasmine",
"expect-webdriverio/jasmine"
]
}
}The expect export from expect-webdriverio remains available under Jasmine if you prefer an explicit import strategy. See the playground example.
import { expect as wdioExpect } from 'expect-webdriverio'
describe('My tests', async () => {
it('should verify my browser to have the expected url', async () => {
await wdioExpect(browser).toHaveUrl('https://example.com')
// Does not require await
wdioExpect(true).toBe(true)
})
}) Since
@wdio/jasmine-framework10.0.2, the types of thisexpectkeep the Jest matchers, as at runtime.
jasmine.any(Type),jasmine.anything(),jasmine.is(),jasmine.stringContaining(),jasmine.stringMatching(),jasmine.truthy(),jasmine.falsy(),jasmine.empty()andjasmine.notEmpty()work in the element matchers, also in an array of expected values on$$().- On
$$(),jasmine.arrayContaining()andjasmine.arrayWithExactContents()compare the values of all the elements at once, see Matching a subset of element values. - Network matchers support
jasmine.objectContaining(). - WDIO asymmetric matchers also work properly.
import { expect as wdioExpect } from 'expect-webdriverio'
describe('My tests', async () => {
it('should verify my browser to have the expected url', async () => {
// Working Jasmine asymmetric matchers
await expectAsync(browser).toHaveUrl(jasmine.stringContaining('WebdriverIO'))
await expectAsync(browser).toHaveUrl(jasmine.stringMatching('/WebdriverIO/'))
await expectAsync(browser).toHaveUrl(jasmine.any(String))
await expectAsync(browser).toHaveUrl(jasmine.anything())
// Working WDIO asymmetric matcher
await expectAsync(browser).toHaveUrl(wdioExpect.stringContaining('WebdriverIO'))
})
})On $$(), Jasmine's list matchers compare the values of all the elements at once. Results with toHaveText on $$('li'):
| Expected value | Passes when | Order | Other elements | String options (trim, ignoreCase…) |
|---|---|---|---|---|
['Coffee', 'Tea'] |
each element has the text at its index | fixed | no: same count | applied |
jasmine.arrayContaining(['Tea', 'Coffee']) |
each text is on at least one element | any | allowed | not applied |
jasmine.arrayWithExactContents(['Tea', 'Coffee']) |
same count, and each text is on at least one element | any | no | not applied |
jasmine.arrayWithExactContents() does not count a text that it has 2 times: ['Tea', 'Tea'] passes on Coffee, Tea, because the count is the same and Tea is in the list. This is how Jasmine compares. For an exact list with repeated texts, use an array in the order of the elements.
The other list matchers of Jest, e.g. expect.arrayOf(), are on the expect of expect-webdriverio.
// <li>Coffee</li> <li>Tea</li>
await expect($$('li')).toHaveText(['Coffee', 'Tea']) // passes
await expect($$('li')).toHaveText(['Tea', 'Coffee']) // fails: the order is different
await expect($$('li')).toHaveText(jasmine.arrayWithExactContents(['Tea', 'Coffee'])) // passes: any order
await expect($$('li')).toHaveText(jasmine.arrayContaining(['Tea'])) // passes: Coffee is allowed
await expect($$('li')).toHaveText(jasmine.arrayWithExactContents(['Tea'])) // fails: Coffee is not expected
await expect($$('li')).toHaveText(jasmine.arrayWithExactContents(['Tea', 'Tea'])) // passes: see above
// <li> Coffee </li> <li> Tea </li>
await expect($$('li')).toHaveText(jasmine.arrayWithExactContents(['Tea', 'Coffee'])) // fails: the values are " Coffee " and " Tea "More details to come. In short, when paired with @wdio/cucumber-framework, you can use WebDriverIO's expect library seamlessly within your Cucumber step definitions and Gherkin-based tests.
Browser Runner (@wdio/browser-runner) allows you to leverage component frameworks like React, Preact, Vue.js, Svelte, and SolidJS. It relies on the Jest's standard expect library and only registers core expect-webdriverio matchers. Because it runs within a browser environment rather than fully embedding expect-webdriverio, global features like DefaultOption and SoftAssertion are currently unsupported.
The asymmetric matcher expect.oneOf and the modifier some have been adapted to work in Browser Runner:
expect.oneOf: Works standard out of the box.expect.some: Reimplemented to integrate with Jest's native expect runner and exposed directly viaexpect.some. Note: Importingsomefromexpect-webdriverio/apiwill not work in Browser Runner mode. Always access it throughexpect.some
it('some elements are displayed', async () => {
// `expect.some` uses global export. Ignore TypeScript errors on `expect.some` until proper typings are provided.
await expect(expect.some($$('p=Times clicked: 1'))).toBeDisplayed()
}) $$()is supported, awaited or not, and filtered (Element[]), including by the snapshot matchers.- Multi-remote is not supported: the Browser Runner runs tests in a single browser session, and stops with
No "browserName" defined in capability objectwhen given multi-remote capabilities. Use a Node.js runner framework instead, such as Mocha, Jasmine or Cucumber.
A framework adapter, a lint rule or a runner that must know the matchers before expect exists can import their names from expect-webdriverio/api, instead of a copy of the list:
import { wdioCustomMatcherNames, asymmetricMatcherNames, inverseAsymmetricMatcherNames } from 'expect-webdriverio/api'
wdioCustomMatcherNames // ['toBeChecked', ..., 'toMatchSnapshot']: the names of `wdioCustomMatchers`, not the matchers of the `expect` package
asymmetricMatcherNames // ['any', ..., 'oneOf', 'multiRemote']: the asymmetric matchers of the `expect` package and of expect-webdriverio
inverseAsymmetricMatcherNames // ['arrayContaining', ..., 'stringMatching']: the same, on `expect.not`Each matcher returns a promise, except toMatchSnapshot and toMatchInlineSnapshot when the received value is not an element, a list of elements or a promise. The lists do not have the matchers that you add with expect.extend(), or the some modifier.
expect.extend() also adds each matcher to expect and expect.not as an asymmetric matcher, and the asymmetric lists do not have these. The asymmetric form of the matchers of wdioCustomMatcherNames does not work, so do not add them to the asymmetric lists: the matchers are async (expect.toHaveTitle('x').asymmetricMatch(browser) gives undefined), except toMatchSnapshot and toMatchInlineSnapshot, which compare or write a snapshot of the current test, not an equality. A sync matcher that you add with expect.extend() works as an asymmetric matcher.