@percy/playwright 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ Copyright (c) Perceptual Inc.
2
+
3
+ The MIT License (MIT)
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,61 @@
1
+ # @percy/playwright
2
+ [![Version](https://img.shields.io/npm/v/@percy/playwright.svg)](https://npmjs.org/package/@percy/playwright)
3
+ ![Test](https://github.com/percy/percy-playwright/workflows/Test/badge.svg)
4
+
5
+ [Percy](https://percy.io) visual testing for Playwright.
6
+
7
+ ## Installation
8
+
9
+ ```sh-session
10
+ $ npm install --save-dev @percy/cli @percy/playwright
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ This is an example using the `percySnapshot` function. For other examples of `playwright`
16
+ usage, see the [Playwright docs](https://playwright.dev/docs/library).
17
+
18
+ ```javascript
19
+ const { chromium } = require('playwright');
20
+ const percySnapshot = require('@percy/playwright');
21
+
22
+ (async () => {
23
+ const browser = await chromium.launch();
24
+ const page = await browser.newPage();
25
+ await page.goto('http://example.com/', { waitUntil: 'networkidle2' });
26
+ await percySnapshot(page, 'Example Site');
27
+
28
+ await browser.close();
29
+ })();
30
+ ```
31
+
32
+ Running the code above directly will result in the following logs:
33
+
34
+ ```sh-session
35
+ $ node script.js
36
+ [percy] Percy is not running, disabling snapshots
37
+ ```
38
+
39
+ When running with [`percy
40
+ exec`](https://github.com/percy/cli/tree/master/packages/cli-exec#percy-exec), and your project's
41
+ `PERCY_TOKEN`, a new Percy build will be created and snapshots will be uploaded to your project.
42
+
43
+ ```sh-session
44
+ $ export PERCY_TOKEN=[your-project-token]
45
+ $ percy exec -- node script.js
46
+ [percy] Percy has started!
47
+ [percy] Created build #1: https://percy.io/[your-project]
48
+ [percy] Running "node script.js"
49
+ [percy] Snapshot taken "Example Site"
50
+ [percy] Stopping percy...
51
+ [percy] Finalized build #1: https://percy.io/[your-project]
52
+ [percy] Done!
53
+ ```
54
+
55
+ ## Configuration
56
+
57
+ `percySnapshot(page, name[, options])`
58
+
59
+ - `page` (**required**) - A `playwright` page instance
60
+ - `name` (**required**) - The snapshot name; must be unique to each snapshot
61
+ - `options` - [See per-snapshot configuration options](https://docs.percy.io/docs/cli-configuration#per-snapshot-configuration)
package/index.js ADDED
@@ -0,0 +1,42 @@
1
+ const utils = require('@percy/sdk-utils');
2
+
3
+ // Collect client and environment information
4
+ const sdkPkg = require('./package.json');
5
+ const playwrightPkg = require('playwright/package.json');
6
+ const CLIENT_INFO = `${sdkPkg.name}/${sdkPkg.version}`;
7
+ const ENV_INFO = `${playwrightPkg.name}/${playwrightPkg.version}`;
8
+
9
+ // Take a DOM snapshot and post it to the snapshot endpoint
10
+ async function percySnapshot(page, name, options) {
11
+ if (!page) throw new Error('A Playwright `page` object is required.');
12
+ if (!name) throw new Error('The `name` argument is required.');
13
+ if (!(await utils.isPercyEnabled())) return;
14
+ let log = utils.logger('playwright');
15
+
16
+ try {
17
+ // Inject the DOM serialization script
18
+ await page.evaluate(await utils.fetchPercyDOM());
19
+
20
+ // Serialize and capture the DOM
21
+ /* istanbul ignore next: no instrumenting injected code */
22
+ let domSnapshot = await page.evaluate((options) => {
23
+ /* eslint-disable-next-line no-undef */
24
+ return PercyDOM.serialize(options);
25
+ }, options);
26
+
27
+ // Post the DOM to the snapshot endpoint with snapshot options and other info
28
+ await utils.postSnapshot({
29
+ ...options,
30
+ environmentInfo: ENV_INFO,
31
+ clientInfo: CLIENT_INFO,
32
+ url: page.url(),
33
+ domSnapshot,
34
+ name
35
+ });
36
+ } catch (err) {
37
+ log.error(`Could not take DOM snapshot "${name}"`);
38
+ log.error(err);
39
+ }
40
+ }
41
+
42
+ module.exports = percySnapshot;
package/package.json ADDED
@@ -0,0 +1,51 @@
1
+ {
2
+ "name": "@percy/playwright",
3
+ "description": "Playwright client library for visual testing with Percy",
4
+ "version": "1.0.0",
5
+ "license": "MIT",
6
+ "author": "Perceptual Inc.",
7
+ "repository": "https://github.com/percy/percy-playwright",
8
+ "keywords": [
9
+ "playwright",
10
+ "percy",
11
+ "visual testing"
12
+ ],
13
+ "main": "index.js",
14
+ "types": "types/index.d.ts",
15
+ "files": [
16
+ "index.js",
17
+ "types/index.d.ts"
18
+ ],
19
+ "engines": {
20
+ "node": ">=12"
21
+ },
22
+ "scripts": {
23
+ "lint": "eslint --ignore-path .gitignore .",
24
+ "test": "cross-env NODE_ENV=test playwright test",
25
+ "test:coverage": "nyc yarn test",
26
+ "test:types": "tsd"
27
+ },
28
+ "publishConfig": {
29
+ "access": "public"
30
+ },
31
+ "dependencies": {
32
+ "@percy/sdk-utils": "^1.0.0-beta.69"
33
+ },
34
+ "peerDependencies": {
35
+ "playwright": ">=1"
36
+ },
37
+ "devDependencies": {
38
+ "@percy/core": "^1.0.0-beta.69",
39
+ "@playwright/test": "^1.15.2",
40
+ "cross-env": "^7.0.2",
41
+ "eslint": "^7.18.0",
42
+ "eslint-config-standard": "^16.0.2",
43
+ "eslint-plugin-import": "^2.22.0",
44
+ "eslint-plugin-node": "^11.1.0",
45
+ "eslint-plugin-promise": "^5.1.0",
46
+ "eslint-plugin-standard": "^5.0.0",
47
+ "nyc": "^15.1.0",
48
+ "playwright": "^1.15.0",
49
+ "tsd": "^0.17.0"
50
+ }
51
+ }
@@ -0,0 +1,8 @@
1
+ import * as Playwright from 'playwright';
2
+ import { SnapshotOptions } from '@percy/core';
3
+
4
+ export default function percySnapshot(
5
+ page: Playwright.Page,
6
+ name: string,
7
+ options?: SnapshotOptions
8
+ ): Promise<void>;