What is expect-puppeteer?
The expect-puppeteer package is a set of utility functions that integrate Jest's expect assertions with Puppeteer's browser automation capabilities. It allows for more readable and expressive tests when working with Puppeteer.
What are expect-puppeteer's main functionalities?
toMatch
This feature allows you to assert that a specific text appears on the page. It simplifies the process of checking for text content within the page.
await expect(page).toMatch('text to match');
toClick
This feature allows you to simulate a click event on a specified element. It is useful for testing interactions with buttons, links, and other clickable elements.
await expect(page).toClick('button#submit');
toFill
This feature allows you to fill out form fields with specified values. It is useful for testing form submissions and input handling.
await expect(page).toFill('input[name="username"]', 'myUsername');
toSelect
This feature allows you to select an option from a dropdown menu. It is useful for testing interactions with select elements.
await expect(page).toSelect('select#dropdown', 'optionValue');
toUploadFile
This feature allows you to simulate file uploads by specifying the file path. It is useful for testing file input elements.
await expect(page).toUploadFile('input[type="file"]', 'path/to/file.txt');
Other packages similar to expect-puppeteer
jest-puppeteer
jest-puppeteer provides a set of utilities to run Puppeteer with Jest. It offers a more comprehensive setup for integrating Puppeteer with Jest, including custom environment setup and teardown, but it does not include the same level of syntactic sugar for assertions as expect-puppeteer.
puppeteer-testing-library
puppeteer-testing-library is a set of utilities for using the Testing Library queries with Puppeteer. It focuses on providing a more user-centric approach to querying and interacting with elements, similar to the philosophy of the Testing Library family of packages.
puppeteer-extra
puppeteer-extra is a modular plugin framework for Puppeteer. It allows you to extend Puppeteer's functionality with plugins, such as stealth mode and adblocker. While it does not provide assertion utilities, it enhances Puppeteer's capabilities in other ways.
expect-puppeteer
Assertion library for Puppeteer.
npm install expect-puppeteer
Usage
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()
})()
Use with Jest
To use with Jest, just modify your configuration:
{
"setupTestFrameworkScriptFile": "expect-puppeteer"
}
Why do I need it
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
await page.click('button')
await page.toClick('button')
await page.toClick('button', { text: 'My button' })
API
Table of Contents
expect(instance).toClick(selector[, options])
instance
<Page|ElementHandle> Contextselector
<string> A selector to click onoptions
<Object> Optional parameters
polling
<string|number> An interval at which the pageFunction
is executed, defaults to raf
. If polling
is a number, then it is treated as an interval in milliseconds at which the function would be executed. If polling
is a string, then it can be one of the following values:
raf
- to constantly execute pageFunction
in requestAnimationFrame
callback. This is the tightest polling mode which is suitable to observe styling changes.mutation
- to execute pageFunction
on every DOM mutation.
timeout
<number> maximum time to wait for in milliseconds. Defaults to 500
.text
<string> A text or a RegExp to match in element textContent
.
await expect(page).toClick('button', { text: 'Home' })
expect(page).toDisplayDialog(block)
await expect(page).toDisplayDialog(async () => {
await expect(page).toClick('button', { text: 'Show dialog' })
})
expect(instance).toFill(selector, value[, options])
instance
<Page|ElementHandle> Contextselector
<string> A selector to match fieldvalue
<string> Value to filloptions
<Object> Optional parameters
polling
<string|number> An interval at which the pageFunction
is executed, defaults to raf
. If polling
is a number, then it is treated as an interval in milliseconds at which the function would be executed. If polling
is a string, then it can be one of the following values:
raf
- to constantly execute pageFunction
in requestAnimationFrame
callback. This is the tightest polling mode which is suitable to observe styling changes.mutation
- to execute pageFunction
on every DOM mutation.
timeout
<number> maximum time to wait for in milliseconds. Defaults to 500
.
await expect(page).toFill('input[name="firstName"]', 'James')
expect(instance).toFillForm(selector, values[, options])
instance
<Page|ElementHandle> Contextselector
<string> A selector to match formvalues
<Object> Values to filloptions
<Object> Optional parameters
polling
<string|number> An interval at which the pageFunction
is executed, defaults to raf
. If polling
is a number, then it is treated as an interval in milliseconds at which the function would be executed. If polling
is a string, then it can be one of the following values:
raf
- to constantly execute pageFunction
in requestAnimationFrame
callback. This is the tightest polling mode which is suitable to observe styling changes.mutation
- to execute pageFunction
on every DOM mutation.
timeout
<number> maximum time to wait for in milliseconds. Defaults to 500
.
await expect(page).toFillForm('form[name="myForm"]', {
firstName: 'James',
lastName: 'Bond',
})
expect(instance).toMatch(matcher[, options])
instance
<Page|ElementHandle> Contextmatcher
<string> A text or a RegExp to match in pageoptions
<Object> Optional parameters
polling
<string|number> An interval at which the pageFunction
is executed, defaults to raf
. If polling
is a number, then it is treated as an interval in milliseconds at which the function would be executed. If polling
is a string, then it can be one of the following values:
raf
- to constantly execute pageFunction
in requestAnimationFrame
callback. This is the tightest polling mode which is suitable to observe styling changes.mutation
- to execute pageFunction
on every DOM mutation.
timeout
<number> maximum time to wait for in milliseconds. Defaults to 500
.
await expect(page).toMatch('Lorem ipsum')
await expect(page).toMatch('lo.*')
expect(instance).toMatchElement(selector[, options])
instance
<Page|ElementHandle> Contextselector
<string> A selector to match elementoptions
<Object> Optional parameters
polling
<string|number> An interval at which the pageFunction
is executed, defaults to raf
. If polling
is a number, then it is treated as an interval in milliseconds at which the function would be executed. If polling
is a string, then it can be one of the following values:
raf
- to constantly execute pageFunction
in requestAnimationFrame
callback. This is the tightest polling mode which is suitable to observe styling changes.mutation
- to execute pageFunction
on every DOM mutation.
timeout
<number> maximum time to wait for in milliseconds. Defaults to 500
.text
<string> A text or a RegExp to match in element textContent
.
const row = await expect(page).toMatchElement('tr', { text: 'My row' })
await expect(row).toClick('td:nth-child(2) a')
expect(instance).toSelect(selector, valueOrText[, options])
instance
<Page|ElementHandle> Contextselector
<string> A selector to match select elementvalueOrText
<string> Value or text matching optionoptions
<Object> Optional parameters
polling
<string|number> An interval at which the pageFunction
is executed, defaults to raf
. If polling
is a number, then it is treated as an interval in milliseconds at which the function would be executed. If polling
is a string, then it can be one of the following values:
raf
- to constantly execute pageFunction
in requestAnimationFrame
callback. This is the tightest polling mode which is suitable to observe styling changes.mutation
- to execute pageFunction
on every DOM mutation.
timeout
<number> maximum time to wait for in milliseconds. Defaults to 500
.
await expect(page).toSelect('select[name="choices"]', 'Choice 1')
expect(instance).toUploadFile(selector, filePath[, options])
instance
<Page|ElementHandle> Contextselector
<string> A selector to match input elementfilePath
<string> A file pathoptions
<Object> Optional parameters
polling
<string|number> An interval at which the pageFunction
is executed, defaults to raf
. If polling
is a number, then it is treated as an interval in milliseconds at which the function would be executed. If polling
is a string, then it can be one of the following values:
raf
- to constantly execute pageFunction
in requestAnimationFrame
callback. This is the tightest polling mode which is suitable to observe styling changes.mutation
- to execute pageFunction
on every DOM mutation.
timeout
<number> maximum time to wait for in milliseconds. Defaults to 500
.
import path from 'path'
await expect(page).toUploadFile(
'input[type="file"]',
path.join(__dirname, 'file.txt'),
)
License
MIT