Gathering detailed insights and metrics for expect-playwright
Gathering detailed insights and metrics for expect-playwright
Gathering detailed insights and metrics for expect-playwright
Gathering detailed insights and metrics for expect-playwright
Expect utility matcher functions to simplify expect statements for the usage with Playwright Test or Jest Playwright.
npm install expect-playwright
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
149 Stars
113 Commits
17 Forks
8 Watching
1 Branches
9 Contributors
Updated on 02 Nov 2024
TypeScript (97.66%)
HTML (1.72%)
JavaScript (0.63%)
Cumulative downloads
Total Downloads
Last day
-4.8%
191,099
Compared to previous day
Last week
0.9%
1,055,976
Compared to previous week
Last month
10.9%
4,480,335
Compared to previous month
Last year
123%
37,164,569
Compared to previous year
⚠️ We recommend the official Playwright test runner ⚠️
The Playwright test runner includes all the matchers in this repo plus many more to make testing your projects easy. This doesn't mean, that we stop with maintaining this package.
This library provides utility matchers for Jest in combination with Playwright. All of them are exposed on the expect
object. You can use them either directly or invert them via the .not
property like shown in a example below.
1npm install -D expect-playwright
To activate with the Playwright test runner, use expect.extend()
in the config to add the expect-playwright
matchers.
1// playwright.config.ts 2import { expect } from "@playwright/test" 3import { matchers } from "expect-playwright" 4 5expect.extend(matchers) 6 7// ...
To activate it in your Jest environment you have to include it in your configuration.
1{ 2 "setupFilesAfterEnv": ["expect-playwright"] 3}
The Playwright API is great, but it is low level and not designed for integration testing. So this package tries to provide a bunch of utility functions to perform the common checks easier.
Example which should wait and compare the text content of a paragraph on the page.
1// before 2await page.waitForSelector("#foo") 3const textContent = await page.$eval("#foo", (el) => el.textContent) 4expect(textContent).stringContaining("my text") 5 6// after by using expect-playwright 7await expect(page).toMatchText("#foo", "my text")
But that's not all! Our matchers also work inside of iframes and accept an ElementHandle which targets an iframe
element or a Frame obtained by calling element.contentFrame()
. Not only that, but if you pass a promise, we will automatically resolve it for you!
1// before 2const element = await page.$("iframe") 3const frame = await element.contentFrame() 4await expect(frame).toBeChecked("#foo") 5 6// after 7await expect(page.$("iframe")).toBeChecked("#foo")
This function checks if a given element is checked.
You can do this via a selector on the whole page:
1await expect(page).toBeChecked("#my-element")
Or by passing a Playwright ElementHandle:
1const element = await page.$("#my-element") 2await expect(element).toBeChecked()
This function checks if a given element is disabled.
You can do this via a selector on the whole page:
1await expect(page).toBeDisabled("#my-element")
Or by passing a Playwright ElementHandle:
1const element = await page.$("#my-element") 2await expect(element).toBeDisabled()
This function checks if a given element is enabled.
You can do this via a selector on the whole page:
1await expect(page).toBeEnabled("#my-element")
Or by passing a Playwright ElementHandle:
1const element = await page.$("#my-element") 2await expect(element).toBeEnabled()
This function checks if a given element is focused.
You can do this via a selector on the whole page:
1await expect(page).toHaveFocus("#foobar")
Or by passing a Playwright ElementHandle:
1const element = await page.$("#foobar") 2await expect(element).toHaveFocus()
This function waits as a maximum as the timeout exceeds for a given selector once it appears on the page.
1await expect(page).toHaveSelector("#foobar")
When used with not
, toHaveSelector
will wait until the element is not visible or not attached. See the Playwright waitForSelector docs for more details.
1await expect(page).not.toHaveSelector("#foobar")
This function checks if the count of a given selector is the same as the provided value.
1await expect(page).toHaveSelectorCount(".my-element", 3)
This function checks if an element's attribute matches the provided string or regex pattern.
You can do this via a selector on the whole page:
1await expect(page).toMatchAttribute("#foo", "href", "https://playwright.dev") 2await expect(page).toMatchAttribute("#foo", "href", /playwright/)
Or by passing a Playwright ElementHandle:
1const element = await page.$("#foo") 2await expect(element).toMatchAttribute("href", "https://playwright.dev") 3await expect(element).toMatchAttribute("href", /playwright/)
This function checks if an element's computed style property matches the provided string or regex pattern.
You can do this via a selector on the whole page:
1await expect(page).toMatchComputedStyle("#my-element", "color", "rgb(0, 0, 0)") 2await expect(page).toMatchComputedStyle("#my-element", "color", /rgb/)
Or by passing a Playwright ElementHandle:
1const element = await page.$("#my-element") 2await expect(element).toMatchComputedStyle("color", "rgb(0, 0, 0)") 3await expect(element).toMatchComputedStyle("color", /rgb/)
This function checks if the textContent
of a given element matches the provided string or regex pattern.
You can do this via a selector on the whole page:
1await expect(page).toMatchText("#my-element", "Playwright") 2await expect(page).toMatchText("#my-element", /Play.+/)
Or without a selector which will use the body
element:
1await expect(page).toMatchText("Playwright") 2await expect(page).toMatchText(/Play.+/)
Or by passing a Playwright ElementHandle:
1const element = await page.$("#my-element") 2await expect(element).toMatchText("Playwright") 3await expect(element).toMatchText(/Play.+/)
This function checks if the page or frame title matches the provided string or regex pattern.
1await expect(page).toMatchTitle("My app - page 1") 2await expect(page).toMatchTitle(/My app - page \d/)
This function checks if the current page's URL matches the provided string or regex pattern.
1await expect(page).toMatchURL("https://github.com") 2await expect(page).toMatchURL(/github\.com/)
This function checks if the value
of a given element is the same as the provided string or regex pattern.
You can do this via a selector or the element directly:
1await expect(page).toMatchValue("#my-element", "Playwright") 2await expect(page).toMatchValue("#my-element", /Play.+/)
Or by passing a Playwright ElementHandle:
1const element = await page.$("#my-element") 2await expect(element).toMatchValue("Playwright") 3await expect(element).toMatchValue(/Play.+/)
1import playwright from "playwright-chromium" 2 3describe("GitHub Playwright project", () => { 4 it("should should have Playwright in the README heading", async () => { 5 const browser = await playwright.chromium.launch() 6 const page = await browser.newPage() 7 await page.goto("https://github.com/microsoft/playwright") 8 await expect(page).toMatchText("#readme h1", "Playwright") 9 // or also all of them via the not property 10 await expect(page).not.toMatchText("this-is-no-anywhere", { 11 timeout: 1 * 1000, 12 }) 13 await browser.close() 14 }) 15})
There are typings available. For that just import
1import "expect-playwright"
at the top of your test file or include it globally in your tsconfig.json
.
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 21/26 approved changesets -- score normalized to 8
Reason
dependency not pinned by hash detected -- score normalized to 3
Details
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
project is archived
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
16 existing vulnerabilities detected
Details
Score
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More