Product
Socket Now Supports uv.lock Files
Socket now supports uv.lock files to ensure consistent, secure dependency resolution for Python projects and enhance supply chain security.
expect-puppeteer
Advanced tools
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.
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');
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 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 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.
Assertion library for Puppeteer.
npm install expect-puppeteer
Modify your Jest 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:
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).toMatchElement("div", { text: "some text" });
// Will match inner div
await expect(page).toMatchElement("div.inner", { text: "some text" });
Expect an element to be in the page or element, then click on it.
instance
<Page|Frame|ElementHandle> Contextselector
<string|MatchSelector> A selector or a MatchSelector to click on.options
<Object> Optional parameters
button
<"left"|"right"|"middle"> Defaults to left
.count
<number> defaults to 1. See UIEvent.detail.delay
<number> Time to wait between mousedown
and mouseup
in milliseconds. Defaults to 0.text
<string|RegExp> A text or a RegExp to match in element textContent
.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|Frame|ElementHandle> Contextselector
<string|MatchSelector> A selector or a MatchSelector to match fieldvalue
<string> Value to filloptions
<Object> Optional parameters
delay
<number> delay to pass to the puppeteer element.type
APIawait expect(page).toFill('input[name="firstName"]', "James");
Expect a form to be in the page or element, then fill its controls.
instance
<Page|Frame|ElementHandle> Contextselector
<string|MatchSelector> A selector or a MatchSelector to match formvalues
<Object> Values to filloptions
<Object> Optional parameters
delay
<number> delay to pass to the puppeteer element.type
APIawait 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|Frame|ElementHandle> Contextmatcher
<string|RegExp> 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 30000
(30 seconds). Pass 0
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).toMatchTextContent("Lorem ipsum");
// Matching using RegExp
await expect(page).toMatchTextContent(/lo.*/);
Expect an element be present in the page or element.
instance
<Page|Frame|ElementHandle> Contextselector
<string|MatchSelector> A selector or a MatchSelector 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 30000
(30 seconds). Pass 0
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 element textContent
.visible
<boolean> wait for element to be present in DOM and to be visible, i.e. to not have display: none
or visibility: hidden
CSS properties. Defaults to false
.// 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|Frame|ElementHandle> Contextselector
<string|MatchSelector> A selector or a MatchSelector to match select elementvalueOrText
<string> Value or text matching optionawait 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|Frame|ElementHandle> Contextselector
<string|MatchSelector> A selector or a MatchSelector to match input elementfilePath
<string|Array<string>> A file path or array of file pathsimport { join } from "node:path";
await expect(page).toUploadFile(
'input[type="file"]',
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 });
FAQs
Assertion toolkit for Puppeteer.
The npm package expect-puppeteer receives a total of 181,025 weekly downloads. As such, expect-puppeteer popularity was classified as popular.
We found that expect-puppeteer demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket now supports uv.lock files to ensure consistent, secure dependency resolution for Python projects and enhance supply chain security.
Research
Security News
Socket researchers have discovered multiple malicious npm packages targeting Solana private keys, abusing Gmail to exfiltrate the data and drain Solana wallets.
Security News
PEP 770 proposes adding SBOM support to Python packages to improve transparency and catch hidden non-Python dependencies that security tools often miss.