@argos-ci/cypress 0.0.1

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,7 @@
1
+ Copyright 2022 Smooth Code
2
+
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
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
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.
package/README.md ADDED
@@ -0,0 +1,62 @@
1
+ # @argos-ci/cypress
2
+
3
+ [Cypress](https://cypress.io) commands and utilities for [Argos](https://argos-ci.com) visual testing.
4
+
5
+ [![npm version](https://img.shields.io/npm/v/@argos-ci/cypress.svg)](https://www.npmjs.com/package/@argos-ci/cypress)
6
+ [![npm dm](https://img.shields.io/npm/dm/@argos-ci/cypress.svg)](https://www.npmjs.com/package/@argos-ci/cypress)
7
+ [![npm dt](https://img.shields.io/npm/dt/@argos-ci/cypress.svg)](https://www.npmjs.com/package/@argos-ci/cypress)
8
+
9
+ ## Installation
10
+
11
+ ```sh
12
+ npm install --save-dev @argos-ci/cypress
13
+ ```
14
+
15
+ And add this line to your `cypress/support/index.js` file.
16
+
17
+ ```js
18
+ import "@argos-ci/cypress/support";
19
+ ```
20
+
21
+ ## Usage
22
+
23
+ `cy.argosScreenshot` - command stabilizes the UI and takes a screenshot.
24
+
25
+ _How to take a screenshot with `cy.argosScreenshot` command_
26
+
27
+ ```js
28
+ describe("Homepage", () => {
29
+ it("should be stable", () => {
30
+ // Screenshot a full page
31
+ cy.argosScreenshot("home");
32
+
33
+ // Screenshot a component
34
+ cy.get("main div.breadcrumb").argosScreenshot("home-breadcrumb");
35
+ });
36
+ });
37
+ ```
38
+
39
+ ### Configuration
40
+
41
+ `cy.argosScreenshot([name][, options])`
42
+
43
+ - `name` - The screenshot name; must be unique; default value to test title
44
+ - `options` - Send to [cy.screenshot command options](https://docs.cypress.io/api/commands/screenshot)
45
+
46
+ ## Helper attributes
47
+
48
+ The `data-visual-test` attributes allow you to control how elements behave in the Argos screenshot.
49
+
50
+ It is often used to hide changing element like dates.
51
+
52
+ - `[data-visual-test="transparent"]` - Make the element transparent (`opacity: 0`)
53
+ - `[data-visual-test="removed"]` - Remove the element (`display: none`)
54
+ - `[data-visual-test="blackout"]` - Blacked out the element
55
+
56
+ _How to use an helper to hide a div from a screenshot_
57
+
58
+ ```html
59
+ <div id="clock" data-visual-test="transparent">
60
+ <!-- the clock code -->
61
+ </div>
62
+ ```
package/package.json ADDED
@@ -0,0 +1,51 @@
1
+ {
2
+ "name": "@argos-ci/cypress",
3
+ "description": "Visual testing solution to avoid visual regression. Cypress commands and utilities for Argos visual testing.",
4
+ "version": "0.0.1",
5
+ "author": "Smooth Code",
6
+ "license": "MIT",
7
+ "repository": "github:argos-ci/argos-cypress",
8
+ "keywords": [
9
+ "cypress",
10
+ "argos",
11
+ "automation",
12
+ "test automation",
13
+ "testing",
14
+ "visual testing",
15
+ "regression",
16
+ "visual regression"
17
+ ],
18
+ "publishConfig": {
19
+ "access": "public"
20
+ },
21
+ "type": "module",
22
+ "main": "./support.js",
23
+ "types": "./support.d.ts",
24
+ "exports": {
25
+ "./support": {
26
+ "require": "./support.js",
27
+ "types": "./support.d.ts",
28
+ "default": "./support.js"
29
+ },
30
+ "./package.json": "./package.json"
31
+ },
32
+ "scripts": {
33
+ "test": "cypress run",
34
+ "test:open": "cypress open --e2e -b chrome",
35
+ "format": "prettier --write \"**/*.{js,md,json}\" ",
36
+ "lint": "eslint --ignore-path .gitignore .",
37
+ "release": "standard-version && conventional-github-releaser --preset angular"
38
+ },
39
+ "dependencies": {
40
+ "cypress-wait-until": "^1.7.2"
41
+ },
42
+ "devDependencies": {
43
+ "conventional-github-releaser": "^3.1.5",
44
+ "cypress": "^10.6.0",
45
+ "eslint": "^8.23.0",
46
+ "eslint-plugin-cypress": "^2.12.1",
47
+ "eslint-plugin-html": "^7.1.0",
48
+ "prettier": "^2.7.1",
49
+ "standard-version": "^9.5.0"
50
+ }
51
+ }
package/support.d.ts ADDED
@@ -0,0 +1,16 @@
1
+ declare namespace Cypress {
2
+ interface Chainable {
3
+ /**
4
+ * Stabilize the UI and takes a screenshot of the application under test.
5
+ *
6
+ * @see https://on.cypress.io/screenshot
7
+ * @example
8
+ * cy.argosScreenshot("my-screenshot")
9
+ * cy.get(".post").argosScreenshot()
10
+ */
11
+ argosScreenshot: (
12
+ name: string,
13
+ options: Partial<Loggable & Timeoutable & ScreenshotOptions>
14
+ ) => Chainable<null>;
15
+ }
16
+ }
package/support.js ADDED
@@ -0,0 +1,90 @@
1
+ import "cypress-wait-until";
2
+
3
+ const GLOBAL_STYLES = `
4
+ /* Hide carets */
5
+ * { caret-color: transparent !important; }
6
+
7
+ /* Hide scrollbars */
8
+ ::-webkit-scrollbar {
9
+ display: none !important;
10
+ }
11
+
12
+ /* Generic hide */
13
+ [data-visual-test="transparent"] {
14
+ color: transparent !important;
15
+ font-family: monospace !important;
16
+ opacity: 0 !important;
17
+ }
18
+
19
+ [data-visual-test="removed"] {
20
+ display: none !important;
21
+ }
22
+ `;
23
+
24
+ /**
25
+ * Injected style to hide elements and fix unstable rendering.
26
+ */
27
+ function injectStyles(document) {
28
+ const css = document.createElement("style");
29
+ css.type = "text/css";
30
+ css.textContent = GLOBAL_STYLES;
31
+ document.body.appendChild(css);
32
+ }
33
+
34
+ /**
35
+ * Check if an element is visible.
36
+ */
37
+ function isVisible(element) {
38
+ return Boolean(
39
+ element.offsetWidth ||
40
+ element.offsetHeight ||
41
+ element.getClientRects().length
42
+ );
43
+ }
44
+
45
+ /**
46
+ * Wait until there is no `[aria-busy="true"]` element on the page.
47
+ */
48
+ function waitUntilNoBusy() {
49
+ cy.waitUntil(() =>
50
+ cy.document().then((document) => {
51
+ const busy = Array.from(document.querySelectorAll('[aria-busy="true"]'));
52
+ return busy.every((element) => !isVisible(element));
53
+ })
54
+ );
55
+ }
56
+
57
+ /**
58
+ * Takes a screenshot for Argos.
59
+ */
60
+ Cypress.Commands.add(
61
+ "argosScreenshot",
62
+ { prevSubject: ["optional", "element", "window", "document"] },
63
+ (subject, name, options = {}) => {
64
+ if (typeof name === "object" && name !== null) {
65
+ options = name;
66
+ name = null;
67
+ }
68
+
69
+ const screenshotName = `argos/${name ?? cy.state("runnable").fullTitle()}`;
70
+ Cypress.log({
71
+ name: "argosScreenshot",
72
+ displayName: `Argos Screenshot`,
73
+ message: screenshotName,
74
+ });
75
+ // Inject styles
76
+ cy.document().then((doc) => injectStyles(doc));
77
+ // Wait until there is no `[aria-busy="true"]` element on the page.
78
+ waitUntilNoBusy();
79
+ // Wait for fonts to be loaded
80
+ cy.document().its("fonts.status").should("equal", "loaded");
81
+
82
+ // Screenshot
83
+ cy.wrap(subject).screenshot(screenshotName, {
84
+ capture: "viewport",
85
+ overwrite: true,
86
+ blackout: ['[data-visual-test="blackout"]', ...(options.blackout ?? [])],
87
+ ...options,
88
+ });
89
+ }
90
+ );