|
1 | 1 | # Sauce Labs Visual for Cypress
|
2 | 2 |
|
3 |
| -## Introduction |
4 |
| - |
5 | 3 | This package is Sauce Labs Visual integration for Cypress.<br />
|
6 | 4 | It comes as both a plugin and commands that will be made available for your tests.
|
7 | 5 |
|
8 |
| -It will introduce a new Cypress command: |
9 |
| -- `cy.sauceVisualCheck()`: Takes a screenshot and send it to Sauce Labs Visual for comparison. |
10 |
| - |
11 |
| -> **Warning:** |
12 |
| -> `cy.visualCheck()` has been deprecated and will be removed in a future version. |
13 |
| -
|
14 |
| -## Quickstart |
15 |
| - |
16 |
| -### Step 1: Add Sauce Visual for Cypress dependency |
17 |
| - |
18 |
| -- Install the Sauce Visual for Cypress plugin in your current project. |
19 |
| - |
20 |
| -```sh |
21 |
| -npm install --save @saucelabs/cypress-visual-plugin |
22 |
| -``` |
23 |
| - |
24 |
| -### Step 2: Configure Cypress to use Sauce Visual for Cypress plugin |
25 |
| - |
26 |
| -- Import the plugin in Cypress project configuration, at the top of the file: |
27 |
| -```ts |
28 |
| -import { CypressSauceVisual } from '@saucelabs/cypress-visual-plugin'; |
29 |
| -``` |
30 |
| - |
31 |
| -- Register the plugin to Cypress events in Cypress project configuration: |
32 |
| - |
33 |
| -``` ts |
34 |
| -export default defineConfig({ |
35 |
| - e2e: { |
36 |
| - [...] |
37 |
| - setupNodeEvents(on, config) { |
38 |
| - CypressSauceVisual.register(on, config); |
39 |
| - }, |
40 |
| - }, |
41 |
| -}) |
42 |
| -``` |
43 |
| - |
44 |
| -- Register Sauce Visual for Cypress commands. Add the following line in your `cypress/support/e2e.ts`: |
45 |
| -```ts |
46 |
| -import '@saucelabs/cypress-visual-plugin/commands'; |
47 |
| -``` |
48 |
| - |
49 |
| - |
50 |
| -- (TypeScript Users) Add `@saucelabs/cypress-visual-plugin/commands` to your `compilerOptions/types` in `tsconfig.json` for typed commands: |
51 |
| -```json |
52 |
| -{ |
53 |
| - "compilerOptions": { |
54 |
| - "types": ["cypress", "node", "@saucelabs/cypress-visual-plugin/commands"] |
55 |
| - } |
56 |
| -} |
57 |
| -``` |
58 |
| - |
59 |
| -### Step 3: Add visual tests in your project: |
60 |
| - |
61 |
| -```ts |
62 |
| -context('Sauce Demo', () => { |
63 |
| - it('should render correctly', () => { |
64 |
| - ... |
65 |
| - cy.sauceVisualCheck('visual: my-homepage'); |
66 |
| - ... |
67 |
| - }) |
68 |
| -}); |
69 |
| -``` |
70 |
| - |
71 |
| - |
72 |
| -### Step 4: Configure your Sauce Labs credentials |
73 |
| - |
74 |
| -Sauce Visual relies on environment variables for authentications.<br /> |
75 |
| -Both `SAUCE_USERNAME` and `SAUCE_ACCESS_KEY` need to be set prior starting your WebdriverIO job. |
76 |
| - |
77 |
| -Username and Access Key can be retrieved from https://app.saucelabs.com/user-settings. |
78 |
| - |
79 |
| -```sh |
80 |
| -export SAUCE_USERNAME=__YOUR_SAUCE_USER_NAME__ |
81 |
| -export SAUCE_ACCESS_KEY=__YOUR_SAUCE_ACCESS_KEY__ |
82 |
| -``` |
83 |
| - |
84 |
| -### Step 5: Run the test |
85 |
| - |
86 |
| -Everything is now configured, your tests can be run as any other Cypress project. |
87 |
| - |
88 |
| -Builds will appear on Sauce Labs platform as soon as they have been created by the running tests: https://app.saucelabs.com/visual/builds. |
89 |
| - |
90 |
| -## Advanced usage |
91 |
| - |
92 |
| -### Build name |
93 |
| - |
94 |
| -Sauce Visual for Cypress plugin extends Cypress configuration, allowing to define the context, thus acting on which baselines new snapshots will be compared to. ([More info on baseline matching](../sauce-visual.md#baseline-matching)) |
95 |
| - |
96 |
| -Options: |
97 |
| -- `region`: Sauce Labs Region where the new build will be created (default: `us-west-1`) |
98 |
| -- `buildName`: Name of the build (default: `Cypress Visual Testing`) |
99 |
| -- `project`: Name of the project (default: `None`) |
100 |
| -- `branch`: Name of branch (default: `None`) |
101 |
| -- `defaultBranch`: Name of the main or default branch (default: `None`) |
102 |
| - |
103 |
| -They need to be set through the `saucelabs` attribute of `e2e` configuration. |
104 |
| - |
105 |
| -Example: |
106 |
| -```javascript |
107 |
| -export default defineConfig({ |
108 |
| - e2e: { |
109 |
| - [...] |
110 |
| - saucelabs: { |
111 |
| - region: 'eu-central-1', |
112 |
| - buildName: 'SauceDemo - Cypress', |
113 |
| - project: 'Cypress examples', |
114 |
| - branch: 'feature', |
115 |
| - defaultBranch: 'main', |
116 |
| - }, |
117 |
| - [...] |
118 |
| - } |
119 |
| -} |
120 |
| -``` |
121 |
| -
|
122 |
| -### Ignored regions |
123 |
| -
|
124 |
| -In the case you need to ignore some region when running your tests, Sauce Visual provides a way to ignore user-specified areas. |
125 |
| -
|
126 |
| -Those ignored regions are specified when requesting a new snapshot. |
127 |
| -
|
128 |
| -#### User-specified ignored region |
129 |
| -
|
130 |
| -A region is defined by four elements. |
131 |
| -- `x`, `y`: The location of the top-left corner of the ignored region |
132 |
| -- `width`: The width of the region to ignore |
133 |
| -- `height`: The heigh of the region to ignore |
134 |
| -
|
135 |
| -*Note: all values are pixels* |
136 |
| -
|
137 |
| -Example: |
138 |
| -```javascript |
139 |
| - cy.sauceVisualCheck('login-page', { |
140 |
| - ignoredRegions: [ |
141 |
| - { |
142 |
| - x: 240, |
143 |
| - y: 800, |
144 |
| - width: 1520, |
145 |
| - height: 408 |
146 |
| - } |
147 |
| - ], |
148 |
| - }); |
149 |
| -``` |
150 |
| -
|
151 |
| -#### Component-based ignored region |
152 |
| -
|
153 |
| -Alternatively, an ignored region can be a specific element from the page. |
154 |
| -
|
155 |
| -If the selectors matches multiple elements, all will be ignored. |
156 |
| -
|
157 |
| -Example: |
158 |
| -```javascript |
159 |
| - cy.sauceVisualCheck('login-page', { |
160 |
| - ignoredRegions: [ |
161 |
| - cy.get('[data-test="username"]'), |
162 |
| - ] |
163 |
| - }); |
164 |
| -``` |
165 |
| -
|
166 |
| -### Specifying options for Cypress Screenshot |
167 |
| -
|
168 |
| -Sauce Visual is relying on native screenshot feature from Cypress. As `cy.snapshot` has option, they can be specified in the `cy.visualCheck` command. |
169 |
| -
|
170 |
| -The field `cypress` from `options` will be transmitted as it to `cy.screenshot` command. |
171 |
| -
|
172 |
| -Example: |
173 |
| -```javascript |
174 |
| - cy.sauceVisualCheck('login-page', { |
175 |
| - cypress: { |
176 |
| - capture: 'viewport', |
177 |
| - } |
178 |
| - }); |
179 |
| -``` |
180 |
| -
|
181 |
| -### Capturing the dom snapshot |
182 |
| -
|
183 |
| -Sauce Visual doesn't capture dom snapshot by default. It can be changed in `sauceVisualCheck` options. |
184 |
| -
|
185 |
| -Example: |
186 |
| -```javascript |
187 |
| - cy.sauceVisualCheck('login-page', { |
188 |
| - captureDom: true |
189 |
| - }); |
190 |
| -``` |
191 |
| -
|
192 |
| -## Limitations |
193 |
| -
|
194 |
| -Sauce Visual for Cypress **DOES NOT** support `cypress open`. |
| 6 | +## Installation & Usage |
195 | 7 |
|
196 |
| -Screenshots will be captured and sent to Sauce Labs only when `cypress run` is executed. |
| 8 | +View installation and usage instructions on the [Sauce Docs website](https://docs.saucelabs.com/visual-testing/integrations/cypress/). |
0 commit comments