@argos-ci/puppeteer 0.0.1 → 0.0.2

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.
Files changed (4) hide show
  1. package/README.md +12 -41
  2. package/index.d.ts +11 -5
  3. package/index.js +45 -10
  4. package/package.json +1 -1
package/README.md CHANGED
@@ -1,49 +1,20 @@
1
- # @argos-ci/puppeteer
1
+ <p align="center">
2
+ <a href="https://argos-ci.com/?utm_source=github&utm_medium=logo" target="_blank">
3
+ <img src="https://raw.githubusercontent.com/argos-ci/argos/main/resources/logos/logo-github-readme.png" alt="Argos" width="300" height="61">
4
+ </a>
5
+ </p>
2
6
 
3
- [Puppeteer](https://pptr.dev/) commands and utilities for [Argos](https://argos-ci.com) visual testing.
7
+ _Argos is a visual testing solution that fits in your workflow to avoid visual regression. Takes screenshots on each commit and be notified if something changes._
8
+
9
+ # Official Argos Puppeteer integration
4
10
 
5
11
  [![npm version](https://img.shields.io/npm/v/@argos-ci/puppeteer.svg)](https://www.npmjs.com/package/@argos-ci/puppeteer)
6
12
  [![npm dm](https://img.shields.io/npm/dm/@argos-ci/puppeteer.svg)](https://www.npmjs.com/package/@argos-ci/puppeteer)
7
13
  [![npm dt](https://img.shields.io/npm/dt/@argos-ci/puppeteer.svg)](https://www.npmjs.com/package/@argos-ci/puppeteer)
8
14
 
9
- ## Installation
10
-
11
- ```sh
12
- npm install --save-dev @argos-ci/puppeteer
13
- ```
14
-
15
- ## Usage
16
-
17
- Stabilizes the UI before taking a screenshot.
18
-
19
- `argosScreenshot(page, name[, options])`
20
-
21
- - `page` - A `puppeteer` page instance
22
- - `name` - The screenshot name; must be unique
23
- - `options` - See [Page.screenshot command options](https://pptr.dev/next/api/puppeteer.page.screenshot/)
24
-
25
- ```js
26
- describe("Integration test with visual testing", () => {
27
- it("Loads the homepage", async () => {
28
- const browser = await puppeteer.launch();
29
- const page = await browser.newPage();
30
- await page.goto(TEST_URL);
31
- await argosScreenshot(page, this.test.fullTitle());
32
- });
33
- });
34
- ```
35
-
36
- ## Helper attributes
37
-
38
- The `data-visual-test` attributes allow you to control how elements behave in the Argos screenshot.
39
-
40
- It is often used to hide changing element like dates.
15
+ Visit [docs.argos-ci.com/puppeteer](https://docs.argos-ci.com/puppeteer) for guides, API and more.
41
16
 
42
- - `[data-visual-test="transparent"]` - Make the element transparent (`opacity: 0`)
43
- - `[data-visual-test="removed"]` - Remove the element (`display: none`)
44
- - `[data-visual-test="blackout"]` - Blacked out the element
17
+ ## Links
45
18
 
46
- ```html
47
- <!-- Hide a div from a screenshot -->
48
- <div id="clock" data-visual-test="transparent">...</div>
49
- ```
19
+ - [Official SDK Docs](https://docs.argos-ci.com/)
20
+ - [Discord](https://discord.gg/pK79sv85Vg)
package/index.d.ts CHANGED
@@ -1,9 +1,15 @@
1
- import type { Page, ScreenshotOptions } from "puppeteer";
1
+ import type { Page, ScreenshotOptions, ElementHandle } from "puppeteer";
2
2
 
3
- export type ArgosScreenshotOptions = Omit<
4
- ScreenshotOptions,
5
- "encoding" | "type" | "omitBackground" | "path"
6
- >;
3
+ export interface ArgosScreenshotOptions
4
+ extends Omit<
5
+ ScreenshotOptions,
6
+ "encoding" | "type" | "omitBackground" | "path"
7
+ > {
8
+ /**
9
+ * ElementHandle or string selector of the element to take a screenshot of.
10
+ */
11
+ element?: string | ElementHandle;
12
+ }
7
13
 
8
14
  /**
9
15
  * Stabilize the UI and takes a screenshot of the application under test.
package/index.js CHANGED
@@ -38,27 +38,62 @@ async function ensureNoBusy() {
38
38
  );
39
39
  }
40
40
 
41
- // Check if the fonts are loaded
42
- function waitForFontLoading() {
41
+ /**
42
+ * Wait for all fonts to be loaded.
43
+ */
44
+ function waitForFonts() {
43
45
  return document.fonts.status === "loaded";
44
46
  }
45
47
 
46
- export async function argosScreenshot(page, name, { fullPage, clip } = {}) {
48
+ /**
49
+ * Wait for all images to be loaded.
50
+ */
51
+ async function waitForImages() {
52
+ return Promise.all(
53
+ Array.from(document.images)
54
+ .filter((img) => !img.complete)
55
+ .map(
56
+ (img) =>
57
+ new Promise((resolve) => {
58
+ img.onload = img.onerror = resolve;
59
+ })
60
+ )
61
+ );
62
+ }
63
+
64
+ export async function argosScreenshot(
65
+ page,
66
+ name,
67
+ { element = page, ...options } = {}
68
+ ) {
47
69
  if (!page) throw new Error("A Puppeteer `page` object is required.");
48
70
  if (!name) throw new Error("The `name` argument is required.");
49
71
 
50
- await Promise.all([
72
+ if (typeof element === "string") {
73
+ await page.waitForSelector(element);
74
+ element = await page.$(element);
75
+ }
76
+
77
+ const directory = resolve(process.cwd(), "screenshots/argos");
78
+
79
+ const [resolvedElement] = await Promise.all([
80
+ (async () => {
81
+ if (typeof element === "string") {
82
+ await page.waitForSelector(element);
83
+ return page.$(element);
84
+ }
85
+ return element;
86
+ })(),
87
+ mkdirp(directory),
51
88
  page.addStyleTag({ content: GLOBAL_STYLES }),
52
89
  page.waitForFunction(ensureNoBusy),
53
- page.waitForFunction(waitForFontLoading),
90
+ page.waitForFunction(waitForFonts),
91
+ page.waitForFunction(waitForImages),
54
92
  ]);
55
93
 
56
- const directory = resolve(process.cwd(), "screenshots/argos");
57
- await mkdirp(directory);
58
- await page.screenshot({
94
+ await resolvedElement.screenshot({
59
95
  path: resolve(directory, `${name}.png`),
60
96
  type: "png",
61
- fullPage,
62
- clip,
97
+ ...options,
63
98
  });
64
99
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@argos-ci/puppeteer",
3
3
  "description": "Visual testing solution to avoid visual regression. Puppeteer commands and utilities for Argos visual testing.",
4
- "version": "0.0.1",
4
+ "version": "0.0.2",
5
5
  "author": "Smooth Code",
6
6
  "license": "MIT",
7
7
  "repository": "github:argos-ci/argos-puppeteer",