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.
Set options like this:
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 |
| message | string | user message to prepend before assertion error |
String Options#
This options 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 |
| containing | boolean | expect actual value to contain expected value, otherwise strict equal. |
| asString | boolean | might be helpful to force converting property value to string |
Number Options#
This options 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 |
Browser Matchers#
toHaveUrl#
Checks if browser is on a specific page.
Usage#
toHaveUrlContaining#
Checks if browser is on a page URL that contains a value.
Usage#
toHaveTitle#
Checks if website has a specific title.
Usage#
toHaveTitleContaining#
Checks if website has a specific title that contains a value.
Usage#
Element Matchers#
toBeDisplayed#
Calls isDisplayed on given element.
Usage#
toBeVisible#
Same as toBeDisplayed.
Usage#
toExist#
Calls isExisting on given element.
Usage#
toBePresent#
Same as toExist.
Usage#
toBeExisting#
Same as toExist.
Usage#
toBeFocused#
Checks if element has focus. This assertion only works in a web context.
Usage#
toHaveAttribute#
Checks if an element has a certain attribute with a specific value.
Usage#
toHaveAttr#
Same as toHaveAttribute.
Usage#
toHaveAttributeContaining#
Checks if an element has a certain attribute that contains a value.
Usage#
toHaveAttrContaining#
Same as toHaveAttributeContaining.
Usage#
toHaveElementClass#
Checks if an element has a certain class name.
Usage#
toHaveElementClassContaining#
Checks if an element has a certain class name that contains provided value.
Usage#
toHaveElementProperty#
Checks if an element has a certain property.
Usage#
toHaveValue#
Checks if an input element has a certain value.
Usage#
toHaveValueContaining#
Checks if an input element contains a certain value.
Usage#
toBeClickable#
Checks if an element can be clicked by calling isClickable on the element.
Usage#
toBeDisabled#
Checks if an element is disabled by calling isEnabled on the element.
Usage#
toBeEnabled#
Checks if an element is enabled by calling isEnabled on the element.
Usage#
toBeSelected#
Checks if an element is enabled by calling isSelected on the element.
Usage#
toBeChecked#
Same as toBeSelected.
Usage#
toHaveHref#
Checks if link element has a specific link target.
Usage#
toHaveLink#
Same as toHaveHref.
Usage#
toHaveHrefContaining#
Checks if link element contains a specific link target.
Usage#
toHaveLinkContaining#
Same as toHaveHrefContaining.
Usage#
toHaveId#
Checks if element has a specific id attribute.
Usage#
toHaveText#
Checks if element has a specific text. Can also be called with an array as parameter in the case where the element can have different texts.
Usage#
toHaveTextContaining#
Checks if element contains a specific text. Can also be called with an array as parameter in the case where the element can have different texts.
Usage#
toBeDisplayedInViewport#
Checks if an element is within the viewport by calling isDisplayedInViewport on the element.
Usage#
toBeVisibleInViewport#
Same as toBeDisplayedInViewport.
Usage#
toHaveChildren#
Checks amount of the fetched element's children by calling element.$('./*') command.
Usage#
toBeElementsArrayOfSize#
Checks amount of fetched elements using $$ command.
Usage#
Network Matchers#
toBeRequested#
Checks that mock was called
Usage#
toBeRequestedTimes#
Checks that mock was called for the expected amount of times
Usage#
toBeRequestedWith#
Checks that mock was called according to the expected options.
Most of the options supports expect/jasmine partial matchers like expect.objectContaining
Usage#
Default Matchers#
In addition to the expect-webdriverio matchers you can use builtin Jest's expect assertions or expect/expectAsync for Jasmine.