-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement
@sit-onyx/playwright-utils
package (#2269)
Relates to #1991 Implement `useMatrixScreenshotTest` and `adjustSizeToAbsolutePosition` playwright utilities.
- Loading branch information
1 parent
c88e678
commit ad309b3
Showing
63 changed files
with
1,615 additions
and
956 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
"@sit-onyx/playwright-utils": minor | ||
--- | ||
|
||
feat: implement `useMatrixScreenshotTest` and `adjustSizeToAbsolutePosition` | ||
|
||
See our [docs](https://onyx.schwarz/development/packages/playwright-utils.html) for further information. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file added
BIN
+577 KB
.../docs/playwright/snapshots/development-packages-playwright-utils-edge-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+708 KB
...cs/playwright/snapshots/development-packages-playwright-utils-firefox-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+696 KB
...ocs/playwright/snapshots/development-packages-playwright-utils-webkit-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+1.02 KB
(100%)
apps/docs/playwright/snapshots/principles-contributing-testing-edge-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+4.59 KB
(100%)
apps/docs/playwright/snapshots/principles-contributing-testing-firefox-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+1.55 KB
(100%)
apps/docs/playwright/snapshots/principles-contributing-testing-webkit-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,281 @@ | ||
--- | ||
outline: [2, 3] | ||
--- | ||
|
||
<script lang="ts" setup> | ||
import packageJson from "../../../../../packages/playwright-utils/package.json"; | ||
</script> | ||
|
||
# @sit-onyx/playwright-utils | ||
|
||
<div class="hide-external-link"> | ||
|
||
[![npm version](https://badge.fury.io/js/@sit-onyx%playwright-utils.svg)](https://www.npmjs.com/package/@sit-onyx/playwright-utils) | ||
|
||
</div> | ||
|
||
{{ packageJson.description }}. | ||
|
||
## Changelog | ||
|
||
A full changelog can be found [here](/development/packages/changelogs/playwright-utils). | ||
|
||
## Installation | ||
|
||
Install the npm package with your corresponding package manager: | ||
|
||
::: code-group | ||
|
||
```sh [pnpm] | ||
pnpm add -D @sit-onyx/playwright-utils@beta | ||
``` | ||
|
||
```sh [npm] | ||
npm install -D @sit-onyx/playwright-utils@beta | ||
``` | ||
|
||
```sh [yarn] | ||
yarn install -D @sit-onyx/playwright-utils@beta | ||
``` | ||
|
||
::: | ||
|
||
## Utilities | ||
|
||
### useMatrixScreenshotTest | ||
|
||
Creates a screenshot utility that can be used to capture matrix screenshots. | ||
Useful for capturing a single screenshot/image that contains multiple variants of a component. | ||
|
||
#### Example | ||
|
||
![Example of a screenshot matrix for the OnyxButton](../../principles/contributing/example-matrix.png) | ||
|
||
#### Usage | ||
|
||
If not already installed, make sure to install the required dependencies: | ||
|
||
::: code-group | ||
|
||
```sh [pnpm] | ||
pnpm add -D @playwright/test @playwright/experimental-ct-vue | ||
``` | ||
|
||
```sh [npm] | ||
npm install -D @playwright/test @playwright/experimental-ct-vue | ||
``` | ||
|
||
```sh [yarn] | ||
yarn install -D @playwright/test @playwright/experimental-ct-vue | ||
``` | ||
|
||
::: | ||
|
||
##### Step 1: Create matrix screenshot utility | ||
|
||
First, we need to create the matrix screenshot test utility like shown below. There you can pass global options that are applied to all matrix screenshot tests. | ||
|
||
::: code-group | ||
|
||
```ts [playwright.ts] | ||
import { useMatrixScreenshotTest } from "@sit-onyx/playwright-utils"; | ||
|
||
export const { executeMatrixScreenshotTest } = useMatrixScreenshotTest({ | ||
// optionally provide global/default options | ||
}); | ||
``` | ||
|
||
::: | ||
|
||
##### Step 2: Capture matrix screenshots | ||
|
||
Afterwards, you can capture a single matrix screenshot like this: | ||
|
||
::: code-group | ||
|
||
```tsx [MyComponent.tsx] | ||
import { executeMatrixScreenshotTest } from "./playwright"; | ||
import { test } from "@playwright/experimental-ct-vue"; | ||
|
||
test.describe("Screenshot tests", () => { | ||
executeMatrixScreenshotTest({ | ||
name: "Button (default)", | ||
columns: ["primary", "neutral", "danger"], | ||
rows: ["default", "hover", "active", "focus-visible"], | ||
component: (column) => { | ||
return <OnyxButton label="Button" color={column} />; | ||
}, | ||
hooks: { | ||
beforeEach: async (component, page, column, row) => { | ||
if (row === "hover") await component.hover(); | ||
if (row === "focus-visible") await page.keyboard.press("Tab"); | ||
if (row === "active") await page.mouse.down(); | ||
}, | ||
}, | ||
}); | ||
}); | ||
``` | ||
|
||
::: | ||
|
||
<br> | ||
|
||
#### Perform accessibility tests <Badge text="optional" type="warning" /> | ||
|
||
The matrix screenshot utility integrates nicely with features like [accessibility testing](https://playwright.dev/docs/accessibility-testing). | ||
|
||
To perform accessibility tests for all individual screenshots (column/row combinations), set it up like described below. | ||
|
||
##### Step 1: Install axe-core | ||
|
||
::: code-group | ||
|
||
```sh [pnpm] | ||
pnpm add -D @axe-core/playwright | ||
``` | ||
|
||
```sh [npm] | ||
npm install -D @axe-core/playwright | ||
``` | ||
|
||
```sh [yarn] | ||
yarn install -D @axe-core/playwright | ||
``` | ||
|
||
::: | ||
|
||
##### Step 2: Setup global hook | ||
|
||
We can now set up a global hook when calling `useMatrixScreenshotTest()` (see [Create matrix screenshot utility](#step-1-create-matrix-screenshot-utility)) that will run the accessibility test after every screenshot. | ||
|
||
Therefore, update your already existing setup like so: | ||
|
||
::: code-group | ||
|
||
```ts [playwright.ts] | ||
import { useMatrixScreenshotTest } from "@sit-onyx/playwright-utils"; | ||
|
||
/** | ||
* Creates an `AxeBuilder` with common configuration that should be used for accessibility tests. | ||
* | ||
* @see https://playwright.dev/docs/accessibility-testing#creating-a-fixture | ||
*/ | ||
export const createAxeBuilder = (page: Page) => { | ||
return new AxeBuilder({ page }).withTags(["wcag2a", "wcag2aa", "wcag21a", "wcag21aa"]); | ||
}; | ||
|
||
export const { executeMatrixScreenshotTest } = useMatrixScreenshotTest<MatrixScreenshotHookContext>( | ||
{ | ||
defaults: { | ||
hooks: { | ||
afterEach: async (component, page, column, row) => { | ||
// ARRANGE (execute accessibility tests) | ||
const axeBuilder = createAxeBuilder(page); | ||
const accessibilityScanResults = await axeBuilder.analyze(); | ||
|
||
// ASSERT | ||
expect( | ||
accessibilityScanResults.violations, | ||
`should pass accessibility checks for ${column} ${row}`, | ||
).toEqual([]); | ||
}, | ||
}, | ||
}, | ||
}, | ||
); | ||
``` | ||
|
||
::: | ||
|
||
That's it. The accessibility tests will now be performed for every single screenshot. | ||
|
||
##### Step 3: Disable rules for individual tests <Badge text="optional" type="warning" /> | ||
|
||
Sometimes, it might be necessary to disable certain accessibility rules for an individual test. | ||
We can easily support this by making use of the `context` argument that is passed to the `afterEach` hook. | ||
|
||
Adjust your global configuration like this to support disabling accessibility rules. | ||
|
||
::: code-group | ||
|
||
```ts [playwright.ts] | ||
import { useMatrixScreenshotTest } from "@sit-onyx/playwright-utils"; | ||
|
||
export type MatrixScreenshotHookContext = { | ||
/** | ||
* Rules to disable when performing the accessibility tests. | ||
* **IMPORTANT**: Should be avoided! If used, please include a comment why it is needed. | ||
* | ||
* @see https://playwright.dev/docs/accessibility-testing#disabling-individual-scan-rules | ||
*/ | ||
disabledAccessibilityRules?: string[]; | ||
}; | ||
|
||
export const { executeMatrixScreenshotTest } = useMatrixScreenshotTest<MatrixScreenshotHookContext>( | ||
{ | ||
defaults: { | ||
hooks: { | ||
afterEach: async (component, page, column, row, context) => { | ||
// ARRANGE (execute accessibility tests) | ||
const axeBuilder = createAxeBuilder(page); | ||
|
||
if (context?.disabledAccessibilityRules?.length) { | ||
axeBuilder.disableRules( | ||
DEFAULT_DISABLED_AXE_RULES.concat(context.disabledAccessibilityRules), | ||
); | ||
} | ||
|
||
const accessibilityScanResults = await axeBuilder.analyze(); | ||
// ... | ||
}, | ||
}, | ||
}, | ||
}, | ||
); | ||
``` | ||
|
||
::: | ||
|
||
For each individual test, you can now optionally disable rules by defining them in the `context`: | ||
|
||
::: code-group | ||
|
||
```tsx [MyComponent.tsx] | ||
import { executeMatrixScreenshotTest } from "./playwright"; | ||
import { test } from "@playwright/experimental-ct-vue"; | ||
|
||
test.describe("Screenshot tests", () => { | ||
executeMatrixScreenshotTest({ | ||
name: "Button (default)", | ||
context: { | ||
disabledAccessibilityRules: ["color-contrast"], | ||
}, | ||
// ... | ||
}); | ||
}); | ||
``` | ||
|
||
::: | ||
|
||
### adjustSizeToAbsolutePosition | ||
|
||
Sets the component size to fit all absolute positioned content so it is fully included in screenshots. | ||
Useful if component includes flyouts etc. that use CSS `position: absolute`. | ||
|
||
```tsx | ||
import { adjustSizeToAbsolutePosition } from "@sit-onyx/playwright-utils"; | ||
import { test, expect } from "@playwright/experimental-ct-vue"; | ||
import MyComponent from "./MyComponent.vue"; | ||
|
||
test("my example test", async ({ mount }) => { | ||
const component = await mount(<MyComponent />); | ||
|
||
// open/show absolute positioned content, e.g. a tooltip | ||
await component.getByRole("tooltip").click(); | ||
|
||
// adjust component size to include tooltip so its shown/included in the screenshot | ||
await adjustSizeToAbsolutePosition(component); | ||
|
||
await expect(component).toHaveScreenshot("screenshot.png"); | ||
}); | ||
``` |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<div align="center" style="text-align: center"> | ||
<picture> | ||
<source media="(prefers-color-scheme: dark)" type="image/svg+xml" srcset="https://raw.githubusercontent.com/SchwarzIT/onyx/main/.github/onyx-logo-light.svg"> | ||
<source media="(prefers-color-scheme: light)" type="image/svg+xml" srcset="https://raw.githubusercontent.com/SchwarzIT/onyx/main/.github/onyx-logo-dark.svg"> | ||
<img alt="onyx logo" src="https://raw.githubusercontent.com/SchwarzIT/onyx/main/.github/onyx-logo-dark.svg" width="160px"> | ||
</picture> | ||
</div> | ||
|
||
<br> | ||
|
||
# onyx Playwright utils | ||
|
||
Utilities for Vue component testing with Playwright created by [Schwarz IT](https://it.schwarz). | ||
|
||
<br /> | ||
|
||
## Documentation | ||
|
||
You can find our documentation [here](https://onyx.schwarz/development/packages/playwright-utils.html). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
{ | ||
"name": "@sit-onyx/playwright-utils", | ||
"description": "Utilities for Vue component testing with Playwright", | ||
"version": "0.0.0", | ||
"type": "module", | ||
"license": "Apache-2.0", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/schwarzit/onyx", | ||
"directory": "packages/playwright-utils" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/schwarzit/onyx/issues" | ||
}, | ||
"files": [ | ||
"src" | ||
], | ||
"exports": { | ||
".": "./src/index.ts" | ||
}, | ||
"scripts": { | ||
"build": "vue-tsc --noEmit" | ||
}, | ||
"peerDependencies": { | ||
"@playwright/experimental-ct-vue": ">= 1", | ||
"@playwright/test": ">= 1", | ||
"vue": ">= 3" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from "./screenshots/index"; | ||
export * from "./screenshots/types"; |
Oops, something went wrong.