Assertion library for Puppeteer.
npm install expect-puppeteer
Without Jest:
import expect from 'expect-puppeteer'
;(async () => {
const browser = await puppeteer.launch()
const page = await browser.newPage()
await page.goto('https://google.com')
await expect(page).toMatch('google')
await browser.close()
})()
To use with Jest, just modify your configuration:
{
"setupFilesAfterEnv": ["expect-puppeteer"]
}
Writing integration test is very hard, especially when you are testing a Single Page Applications. Data are loaded asynchronously and it is difficult to know exactly when an element will be displayed in the page.
Puppeteer API is great, but it is low level and not designed for integration testing.
This API is designed for integration testing:
- It will wait for element before running an action
- It adds additional feature like matching an element using text
Example
// Does not work if button is not in page
await page.click('button')
// Will try for 500ms to click on "button"
await page.toClick('button')
// Will click the first button with a "My button" text inside
await page.toClick('button', { text: 'My button' })
The first element to match will be selected
Example
<div class="outer">
<div class="inner">some text</div>
</div>
// Will match outer div
await expect(page).toMatch('div', { text: 'some text' })
// Will match inner div
await expect(page).toMatch('div.inner', { text: 'some text' })
Expect an element to be in the page or element, then click on it.
instance
<Page|ElementHandle> Contextselector
<string|MatchSelector> A selector or a MatchSelector to click on.options
<Object> Optional parametersbutton
<"left"|"right"|"middle"> Defaults toleft
.clickCount
<number> defaults to 1. See UIEvent.detail.delay
<number> Time to wait betweenmousedown
andmouseup
in milliseconds. Defaults to 0.text
<string|RegExp> A text or a RegExp to match in elementtextContent
.
await expect(page).toClick('button', { text: 'Home' })
await expect(page).toClick({ type: 'xpath', value: '\\a' }, { text: 'Click' })
Expect block function to trigger a dialog and returns it.
const dialog = await expect(page).toDisplayDialog(async () => {
await expect(page).toClick('button', { text: 'Show dialog' })
})
Expect a control to be in the page or element, then fill it with text.
instance
<Page|ElementHandle> Contextselector
<string> A selector to match fieldvalue
<string> Value to filloptions
<Object> Optional parametersdelay
<number> delay to pass to the puppeteerelement.type
API
await expect(page).toFill('input[name="firstName"]', 'James')
Expect a form to be in the page or element, then fill its controls.
instance
<Page|ElementHandle> Contextselector
<string> A selector to match formvalues
<Object> Values to filloptions
<Object> Optional parametersdelay
<number> delay to pass to the puppeteerelement.type
API
await expect(page).toFillForm('form[name="myForm"]', {
firstName: 'James',
lastName: 'Bond',
})
Expect a text or a string RegExp to be present in the page or element.
instance
<Page|ElementHandle> Contextmatcher
<string|RegExp> A text or a RegExp to match in pageoptions
<Object> Optional parameterspolling
<string|number> An interval at which thepageFunction
is executed, defaults toraf
. Ifpolling
is a number, then it is treated as an interval in milliseconds at which the function would be executed. Ifpolling
is a string, then it can be one of the following values:raf
- to constantly executepageFunction
inrequestAnimationFrame
callback. This is the tightest polling mode which is suitable to observe styling changes.mutation
- to executepageFunction
on every DOM mutation.
timeout
<number> maximum time to wait for in milliseconds. Defaults to30000
(30 seconds). Pass0
to disable timeout. The default value can be changed by using the page.setDefaultTimeout(timeout) method.traverseShadowRoots
<boolean> Whether shadow roots should be traversed to find a match.
// Matching using text
await expect(page).toMatch('Lorem ipsum')
// Matching using RegExp
await expect(page).toMatch(/lo.*/)
Expect an element be present in the page or element.
instance
<Page|ElementHandle> Contextselector
<string> A selector to match elementoptions
<Object> Optional parameterspolling
<string|number> An interval at which thepageFunction
is executed, defaults toraf
. Ifpolling
is a number, then it is treated as an interval in milliseconds at which the function would be executed. Ifpolling
is a string, then it can be one of the following values:raf
- to constantly executepageFunction
inrequestAnimationFrame
callback. This is the tightest polling mode which is suitable to observe styling changes.mutation
- to executepageFunction
on every DOM mutation.
timeout
<number> maximum time to wait for in milliseconds. Defaults to30000
(30 seconds). Pass0
to disable timeout. The default value can be changed by using the page.setDefaultTimeout(timeout) method.text
<string|RegExp> A text or a RegExp to match in elementtextContent
.visible
<boolean> wait for element to be present in DOM and to be visible, i.e. to not havedisplay: none
orvisibility: hidden
CSS properties. Defaults tofalse
.
// Select a row containing a text
const row = await expect(page).toMatchElement('tr', { text: 'My row' })
// Click on the third column link
await expect(row).toClick('td:nth-child(3) a')
Expect a select control to be present in the page or element, then select the specified option.
instance
<Page|ElementHandle> Contextselector
<string> A selector to match select elementvalueOrText
<string> Value or text matching option
await expect(page).toSelect('select[name="choices"]', 'Choice 1')
Expect a input file control to be present in the page or element, then fill it with a local file.
instance
<Page|ElementHandle> Contextselector
<string> A selector to match input elementfilePath
<string> A file path
import path from 'path'
await expect(page).toUploadFile(
'input[type="file"]',
path.join(__dirname, 'file.txt'),
)
An object used as parameter in order to select an element.
type
<"xpath"|"css"> The type of the selectorvalue
<string> The value of the selector
{type:'css', value:'form[name="myForm"]'}
{type:'xpath', value:'.\\a'}
To configure default options like timeout
, expect-puppeteer
exposes two methods: getDefaultOptions
and setDefaultOptions
. You can find available options in Puppeteer page.waitForFunction
documentation. Default options are set to: { timeout: 500 }
.
import { setDefaultOptions } from 'expect-puppeteer'
setDefaultOptions({ timeout: 1000 })
MIT