@argos-ci/playwright 0.0.7 → 0.0.8-alpha.182

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 CHANGED
@@ -1,7 +1,7 @@
1
- Copyright 2023 Smooth Code
1
+ Copyright 2022 Smooth Code
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
4
 
5
5
  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
6
 
7
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/dist/index.cjs CHANGED
@@ -1,6 +1,8 @@
1
- /* eslint-disable @typescript-eslint/no-explicit-any */ const argosScreenshot = async (...args)=>{
1
+ // @ts-ignore
2
+ const argosScreenshot = async (...args)=>{
2
3
  // @ts-ignore
3
- const { argosScreenshot } = await import('./index.mjs');
4
+ const { argosScreenshot } = await import('./index.mjs');
4
5
  return argosScreenshot(...args);
5
6
  };
7
+ // @ts-ignore
6
8
  exports.argosScreenshot = argosScreenshot;
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { ElementHandle, LocatorScreenshotOptions, PageScreenshotOptions, Page } from '@playwright/test';
2
-
1
+ import { Page, PageScreenshotOptions, LocatorScreenshotOptions, ElementHandle } from "@playwright/test";
2
+ import { ViewportOption } from "@argos-ci/browser";
3
3
  type LocatorOptions = Parameters<Page["locator"]>[1];
4
4
  type ScreenshotOptions<TBase extends PageScreenshotOptions | LocatorScreenshotOptions> = Omit<TBase, "encoding" | "type" | "omitBackground" | "path">;
5
5
  type ArgosScreenshotOptions = {
@@ -7,7 +7,10 @@ type ArgosScreenshotOptions = {
7
7
  * ElementHandle or string selector of the element to take a screenshot of.
8
8
  */
9
9
  element?: string | ElementHandle;
10
+ /**
11
+ * Viewports to take screenshots of.
12
+ */
13
+ viewports?: ViewportOption[];
10
14
  } & LocatorOptions & ScreenshotOptions<LocatorScreenshotOptions> & ScreenshotOptions<PageScreenshotOptions>;
11
- declare function argosScreenshot(page: Page, name: string, { element, has, hasText, ...options }?: ArgosScreenshotOptions): Promise<void>;
12
-
15
+ declare function argosScreenshot(page: Page, name: string, { element, has, hasText, viewports, ...options }?: ArgosScreenshotOptions): Promise<void>;
13
16
  export { ArgosScreenshotOptions, argosScreenshot };
package/dist/index.mjs CHANGED
@@ -1,78 +1,121 @@
1
- import { mkdir } from 'node:fs/promises';
1
+ import { mkdir, readFile } from 'node:fs/promises';
2
2
  import { resolve } from 'node:path';
3
+ import { createRequire } from 'node:module';
4
+ import { resolveViewport } from '@argos-ci/browser';
5
+ import { getScreenshotName, writeMetadata, readVersionFromPackage } from '@argos-ci/util';
3
6
 
7
+ const require = createRequire(import.meta.url);
4
8
  const screenshotFolder = "./screenshots";
5
- const GLOBAL_STYLES = `
6
- /* Hide carets */
7
- * { caret-color: transparent !important; }
8
-
9
- /* Hide scrollbars */
10
- ::-webkit-scrollbar {
11
- display: none !important;
12
- }
13
-
14
- /* Generic hide */
15
- [data-visual-test="transparent"] {
16
- color: transparent !important;
17
- font-family: monospace !important;
18
- opacity: 0 !important;
19
- }
20
-
21
- [data-visual-test="removed"] {
22
- display: none !important;
23
- }
24
- `;
25
- // Check if the fonts are loaded
26
- function waitForFontLoading() {
27
- return document.fonts.status === "loaded";
9
+ /**
10
+ * Inject Argos script into the page.
11
+ */ async function injectArgos(page) {
12
+ const fileName = require.resolve("@argos-ci/browser/global.js");
13
+ const content = await readFile(fileName, "utf-8");
14
+ await page.addScriptTag({
15
+ content
16
+ });
17
+ }
18
+ async function getPlaywrightVersion() {
19
+ const pkgPath = require.resolve("playwright/package.json");
20
+ return readVersionFromPackage(pkgPath);
28
21
  }
29
- // Check if the images are loaded
30
- function waitForImagesLoading() {
31
- const allImages = Array.from(document.images);
32
- allImages.forEach((img)=>img.loading = "eager");
33
- return allImages.every((img)=>img.complete);
22
+ async function getArgosPlaywrightVersion() {
23
+ const pkgPath = require.resolve("@argos-ci/playwright/package.json");
24
+ return readVersionFromPackage(pkgPath);
34
25
  }
35
- // Disable spellcheck to avoid red underlines
36
- function disableSpellCheck() {
37
- const query = "[contenteditable]:not([contenteditable=false]):not([spellcheck=false]), input:not([spellcheck=false]), textarea:not([spellcheck=false])";
38
- const inputs = document.querySelectorAll(query);
39
- inputs.forEach((input)=>input.setAttribute("spellcheck", "false"));
40
- return true;
26
+ function getViewportSize(page) {
27
+ const viewportSize = page.viewportSize();
28
+ if (!viewportSize) {
29
+ throw new Error("Can't take screenshots without a viewport.");
30
+ }
31
+ return viewportSize;
41
32
  }
42
- async function argosScreenshot(page, name, { element , has , hasText , ...options } = {}) {
43
- if (!page) throw new Error("A Playwright `page` object is required.");
44
- if (!name) throw new Error("The `name` argument is required.");
33
+ async function argosScreenshot(page, name, { element, has, hasText, viewports, ...options } = {}) {
34
+ if (!page) {
35
+ throw new Error("A Playwright `page` object is required.");
36
+ }
37
+ if (!name) {
38
+ throw new Error("The `name` argument is required.");
39
+ }
45
40
  const handle = typeof element === "string" ? page.locator(element, {
46
41
  has,
47
42
  hasText
48
43
  }) : element ?? page;
49
- mkdir(screenshotFolder, {
50
- recursive: true
51
- });
52
- // Inject global styles
53
- await page.addStyleTag({
54
- content: GLOBAL_STYLES
55
- });
56
- // Wait for all busy elements to be loaded
57
- await page.waitForSelector('[aria-busy="true"]', {
58
- state: "hidden"
59
- });
60
- // Code injection to improve the screenshot stability
61
44
  await Promise.all([
62
- page.waitForFunction(waitForImagesLoading),
63
- page.waitForFunction(waitForFontLoading),
64
- page.waitForFunction(disableSpellCheck)
45
+ // Create the screenshot folder if it doesn't exist
46
+ mkdir(screenshotFolder, {
47
+ recursive: true
48
+ }),
49
+ // Inject Argos script into the page
50
+ injectArgos(page)
65
51
  ]);
66
- await handle.screenshot({
67
- path: resolve(screenshotFolder, `${name}.png`),
68
- type: "png",
69
- fullPage: true,
70
- mask: [
71
- page.locator('[data-visual-test="blackout"]')
72
- ],
73
- animations: "disabled",
74
- ...options
75
- });
52
+ const originalViewportSize = getViewportSize(page);
53
+ await page.evaluate(()=>window.__ARGOS__.prepareForScreenshot());
54
+ async function collectMetadata() {
55
+ const [colorScheme, mediaType, playwrightVersion, argosPlaywrightVersion] = await Promise.all([
56
+ page.evaluate(()=>window.__ARGOS__.getColorScheme()),
57
+ page.evaluate(()=>window.__ARGOS__.getMediaType()),
58
+ getPlaywrightVersion(),
59
+ getArgosPlaywrightVersion()
60
+ ]);
61
+ const viewportSize = getViewportSize(page);
62
+ const browser = page.context().browser();
63
+ if (!browser) {
64
+ throw new Error("Can't take screenshots without a browser.");
65
+ }
66
+ const browserName = browser.browserType().name();
67
+ const browserVersion = browser.version();
68
+ const metadata = {
69
+ url: page.url(),
70
+ viewport: viewportSize,
71
+ colorScheme,
72
+ mediaType,
73
+ browser: {
74
+ name: browserName,
75
+ version: browserVersion
76
+ },
77
+ automationLibrary: {
78
+ name: "playwright",
79
+ version: playwrightVersion
80
+ },
81
+ sdk: {
82
+ name: "@argos-ci/playwright",
83
+ version: argosPlaywrightVersion
84
+ }
85
+ };
86
+ return metadata;
87
+ }
88
+ async function stabilizeAndScreenshot(name) {
89
+ await page.waitForFunction(()=>window.__ARGOS__.waitForStability());
90
+ const metadata = await collectMetadata();
91
+ const screenshotPath = resolve(screenshotFolder, `${name}.png`);
92
+ await writeMetadata(screenshotPath, metadata);
93
+ await handle.screenshot({
94
+ path: screenshotPath,
95
+ type: "png",
96
+ fullPage: handle === page,
97
+ mask: [
98
+ page.locator('[data-visual-test="blackout"]')
99
+ ],
100
+ animations: "disabled",
101
+ ...options
102
+ });
103
+ }
104
+ // If no viewports are specified, take a single screenshot
105
+ if (!viewports) {
106
+ await stabilizeAndScreenshot(name);
107
+ return;
108
+ }
109
+ // Take screenshots for each viewport
110
+ for (const viewport of viewports){
111
+ const viewportSize = resolveViewport(viewport);
112
+ await page.setViewportSize(viewportSize);
113
+ await stabilizeAndScreenshot(getScreenshotName(name, {
114
+ viewportWidth: viewportSize.width
115
+ }));
116
+ }
117
+ // Restore the original viewport size
118
+ await page.setViewportSize(originalViewportSize);
76
119
  }
77
120
 
78
121
  export { argosScreenshot };
package/package.json CHANGED
@@ -1,10 +1,14 @@
1
1
  {
2
2
  "name": "@argos-ci/playwright",
3
3
  "description": "Visual testing solution to avoid visual regression. Playwright commands and utilities for Argos visual testing.",
4
- "version": "0.0.7",
4
+ "version": "0.0.8-alpha.182+8a2e9db",
5
5
  "author": "Smooth Code",
6
6
  "license": "MIT",
7
- "repository": "github:argos-ci/argos-playwright",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "https://github.com/argos-ci/argos-javascript.git",
10
+ "directory": "packages/playwright"
11
+ },
8
12
  "keywords": [
9
13
  "playwright",
10
14
  "argos",
@@ -18,9 +22,6 @@
18
22
  "publishConfig": {
19
23
  "access": "public"
20
24
  },
21
- "engines": {
22
- "node": ">=16"
23
- },
24
25
  "type": "module",
25
26
  "types": "./dist/index.d.ts",
26
27
  "exports": {
@@ -32,30 +33,24 @@
32
33
  },
33
34
  "./package.json": "./package.json"
34
35
  },
36
+ "engines": {
37
+ "node": ">=16.0.0"
38
+ },
39
+ "dependencies": {
40
+ "@argos-ci/browser": "0.0.1-alpha.182+8a2e9db",
41
+ "@argos-ci/util": "0.0.1-alpha.182+8a2e9db"
42
+ },
43
+ "devDependencies": {
44
+ "@argos-ci/cli": "0.6.1-alpha.4+8a2e9db",
45
+ "@playwright/test": "^1.38.1",
46
+ "@types/node": "^16.0.0"
47
+ },
35
48
  "scripts": {
36
49
  "prebuild": "rm -rf dist",
37
50
  "build": "rollup -c",
38
- "test": "npx playwright test",
39
- "format": "prettier --write . --ignore-path .gitignore",
40
- "check-format": "prettier --check .",
41
- "lint": "eslint --ignore-path .gitignore .",
42
- "prepublishOnly": "npm run build",
43
- "release": "standard-version && conventional-github-releaser --preset angular"
51
+ "test": "pnpm exec playwright test",
52
+ "argos-upload": "pnpm exec argos upload screenshots --build-name \"argos-playwright-e2e-node-$NODE_VERSION-$OS\"",
53
+ "e2e": "pnpm run test && pnpm run argos-upload"
44
54
  },
45
- "devDependencies": {
46
- "@argos-ci/cli": "^0.4.4",
47
- "@playwright/test": "^1.30.0",
48
- "@swc/cli": "^0.1.62",
49
- "@swc/core": "^1.3.35",
50
- "@typescript-eslint/eslint-plugin": "^5.52.0",
51
- "@typescript-eslint/parser": "^5.52.0",
52
- "conventional-github-releaser": "^3.1.5",
53
- "eslint": "^8.34.0",
54
- "prettier": "^2.8.4",
55
- "rollup": "^3.17.1",
56
- "rollup-plugin-dts": "^5.2.0",
57
- "rollup-plugin-swc3": "^0.8.0",
58
- "standard-version": "^9.5.0",
59
- "typescript": "^4.9.5"
60
- }
55
+ "gitHead": "8a2e9db6427071708c3d701a3230f228b1216893"
61
56
  }