Expect
When you're writing tests, you often need to check that values meet certain conditions. expect
gives you access to a number of "matchers" that let you validate different things on the browser
, an element
or mock
object.
Default Options
These default options below are connected to the waitforTimeout
and waitforInterval
options set in the config.
Only set the options below if you want to wait for specific timeouts for your assertions.
{
wait: 2000, // ms to wait for expectation to succeed
interval: 100, // interval between attempts
}
If you like to pick different timeouts and intervals, set these options like this:
// wdio.conf.js
import { setOptions } from 'expect-webdriverio'
export const config = {
// ...
before () {
setOptions({ wait: 5000 })
},
// ...
}
Matcher Options
Every matcher can take several options that allows you to modify the assertion:
Command Options
Name | Type | Details |
---|---|---|
wait | number | time in ms to wait for expectation to succeed. Default: 3000 |
interval | number | interval between attempts. Default: 100 |
beforeAssertion | function | function to be called before assertion is made |
afterAssertion | function | function to be called after assertion is made containing assertion results |
message | string | user message to prepend before assertion error |
String Options
This option can be applied in addition to the command options when strings are being asserted.
Name | Type | Details |
---|---|---|
ignoreCase | boolean | apply toLowerCase to both actual and expected values |
trim | boolean | apply trim to actual value |
replace | Replacer | Replacer[] | replace parts of the actual value that match the string/RegExp. The replacer can be a string or a function. |
containing | boolean | expect actual value to contain expected value, otherwise strict equal. |
asString | boolean | might be helpful to force converting property value to string |
atStart | boolean | expect actual value to start with the expected value |
atEnd | boolean | expect actual value to end with the expected value |
atIndex | number | expect actual value to have the expected value at the given index |
Number Options
This option can be applied in addition to the command options when numbers are being asserted.
Name | Type | Details |
---|---|---|
eq | number | equals |
lte | number | less then equals |
gte | number | greater than or equals |
Handling HTML Entities
An HTML entity is a piece of text (“string”) that begins with an ampersand (&
) and ends with a semicolon (;
). Entities are frequently used to display reserved characters (which would otherwise be interpreted as HTML code), and invisible characters (like non-breaking spaces, e.g.
).
To find or interact with such element use unicode equivalent of the entity. e.g.:
<div data="Some Value">Some Text</div>
const myElem = await $('div[data="Some\u00a0Value"]')
await expect(myElem).toHaveAttribute('data', 'div[Some\u00a0Value')
await expect(myElem).toHaveText('Some\u00a0Text')
You can find all unicode references in the HTML spec.
Note: unicode is case-insensitive hence both \u00a0
and \u00A0
works. To find element in browser inspect, remove u
from unicode e.g.: div[data="Some\00a0Value"]
Browser Matchers
toHaveUrl
Checks if browser is on a specific page.
Usage
await browser.url('https://webdriver.io/')
await expect(browser).toHaveUrl('https://webdriver.io')
Usage
await browser.url('https://webdriver.io/')
await expect(browser).toHaveUrl(expect.stringContaining('webdriver'))
toHaveTitle
Checks if website has a specific title.
Usage
await browser.url('https://webdriver.io/')
await expect(browser).toHaveTitle('WebdriverIO · Next-gen browser and mobile automation test framework for Node.js')
await expect(browser).toHaveTitle(expect.stringContaining('WebdriverIO'))
toHaveClipboardText
Checks if the browser has a specific text stored in its clipboard.
Usage
import { Key } from 'webdriverio'
await browser.keys([Key.Ctrl, 'a'])
await browser.keys([Key.Ctrl, 'c'])
await expect(browser).toHaveClipboardText('some clipboard text')
await expect(browser).toHaveClipboardText(expect.stringContaining('clipboard text'))
Element Matchers
toBeDisplayed
Calls isDisplayed
on given element.
Usage
const elem = await $('#someElem')
await expect(elem).toBeDisplayed()
toExist
Calls isExisting
on given element.
Usage
const elem = await $('#someElem')
await expect(elem).toExist()
toBePresent
Same as toExist
.
Usage
const elem = await $('#someElem')
await expect(elem).toBePresent()
toBeExisting
Same as toExist
.
Usage
const elem = await $('#someElem')
await expect(elem).toBeExisting()
toBeFocused
Checks if element has focus. This assertion only works in a web context.
Usage
const elem = await $('#someElem')
await expect(elem).toBeFocused()
toHaveAttribute
Checks if an element has a certain attribute with a specific value.
Usage
const myInput = await $('input')
await expect(myInput).toHaveAttribute('class', 'form-control')
await expect(myInput).toHaveAttribute('class', expect.stringContaining('control'))
toHaveAttr
Same as toHaveAttribute
.
Usage
const myInput = await $('input')
await expect(myInput).toHaveAttr('class', 'form-control')
await expect(myInput).toHaveAttr('class', expect.stringContaining('control'))
toHaveElementClass
Checks if an element has a single class name. Can also be called with an array as a parameter when the element can have multiple class names.
Usage
const myInput = await $('input')
await expect(myInput).toHaveElementClass('form-control', { message: 'Not a form control!' })
await expect(myInput).toHaveElementClass(['form-control' , 'w-full'], { message: 'not full width' })
await expect(myInput).toHaveElementClass(expect.stringContaining('form'), { message: 'Not a form control!' })
toHaveElementProperty
Checks if an element has a certain property.
Usage
const elem = await $('#elem')
await expect(elem).toHaveElementProperty('height', 23)
await expect(elem).not.toHaveElementProperty('height', 0)