You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+61-14Lines changed: 61 additions & 14 deletions
Original file line number
Diff line number
Diff line change
@@ -6,16 +6,17 @@
6
6
7
7
# Cypress localStorage commands
8
8
9
-
Extends Cypress' cy commands with localStorage methods. Allows preserving localStorage between tests and disabling localStorage.
9
+
Extends Cypress' cy commands with localStorage methods. Allows preserving localStorage between tests and spec files, and disabling localStorage.
10
10
11
11
## The problems
12
12
13
13
* You want to preserve localStorage between Cypress tests.
14
+
* You want to preserve localStorage between Cypress spec files.
14
15
* You want to disable localStorage to check error handling.
15
16
16
17
## This solution
17
18
18
-
This solution allows you to use all browser localStorage methods through Cypress commands, and preserve it between tests. It also allows to simulate that localStorage is disabled in the browser.
19
+
This solution allows you to use all browser localStorage methods through Cypress commands, and preserve it between tests and spec files. It also allows to simulate that localStorage is disabled in the browser.
19
20
20
21
## Installation
21
22
@@ -25,58 +26,102 @@ This module is distributed via npm which is bundled with node and should be inst
Read [Cypress configuration docs](https://docs.cypress.io/guides/references/configuration) for further info.
31
40
32
-
Add this line to your project's `cypress/support/commands.js`:
41
+
<details>
42
+
<summary><strong>Installing commands in Cypress <10.0</strong></summary>
43
+
44
+
Add this line to your project's `cypress/support/index.js`:
33
45
34
46
```js
35
47
import"cypress-localstorage-commands"
36
48
```
37
49
38
-
You can now use all next commands:
50
+
</details>
51
+
52
+
### Installing Node events
53
+
54
+
__⚠ In order to support preserving localStorage across Cypress spec files, the plugin's Node events must be installed also.__ Otherwise, localStorage will be preserved only across tests in the same spec file.
Saves current localStorage values into an internal "snapshot".
45
90
46
91
*`snapshotName`_(String)_: Optionally, a `snapshotName` can be provided, and then data from localStorage will be saved into a snapshot with that name. So, multiple snapshots can be stored.
47
92
48
-
#####`cy.restoreLocalStorage([snapshotName])`
93
+
#### `cy.restoreLocalStorage([snapshotName])`
49
94
50
95
Restores localStorage to previously "snapshot" saved values. __
51
96
52
97
*`snapshotName`_(String)_: Optional. If provided, the localStorage will be restored using data from that specific snapshot.
Clears localStorage "snapshot" values, so previously saved values are cleaned.
57
102
58
103
*`snapshotName`_(String)_: Optional. If provided, only data from that specific snapshot will be cleared.
59
104
60
-
#####`cy.getLocalStorage(item)`
105
+
#### `cy.getLocalStorage(item)`
61
106
62
107
Gets localStorage item. Equivalent to `localStorage.getItem` in browser.
63
108
64
109
*`item`_(String)_: Item to get from `localStorage`.
65
110
66
-
#####`cy.setLocalStorage(item, value)`
111
+
#### `cy.setLocalStorage(item, value)`
67
112
68
113
Sets localStorage item. Equivalent to `localStorage.setItem` in browser.
69
114
70
115
*`item`_(String)_: Item to set value.
71
116
*`value`_(String)_: Value to be set.
72
117
73
-
#####`cy.removeLocalStorage(item)`
118
+
#### `cy.removeLocalStorage(item)`
74
119
75
120
Removes localStorage item. Equivalent to `localStorage.removeItem` in browser.
76
121
77
122
*`item`_(String)_: Item to be removed.
78
123
79
-
#####`cy.disableLocalStorage(options)`
124
+
#### `cy.disableLocalStorage(options)`
80
125
81
126
Disables localStorage. It produces localStorage methods to throw errors.
82
127
@@ -87,6 +132,8 @@ Disables localStorage. It produces localStorage methods to throw errors.
87
132
88
133
Use `cy.saveLocalStorage()` to save a snapshot of current `localStorage` at the end of one test, and use the `cy.restoreLocalStorage()` command to restore it at the beginning of another one. _The usage of `beforeEach` and `afterEach` is recommended for this purpose._
89
134
135
+
> ⚠ When the [plugin's Node events are installed](#installing-node-events), the `cy.restoreLocalStorage()` command will be able to restore the localStorage snapshots saved in other spec files. Otherwise, snapshots are completely cleared between spec files.
136
+
90
137
### Examples
91
138
92
139
#### Cookies button example
@@ -125,7 +172,7 @@ describe("Accept cookies button", () => {
125
172
});
126
173
```
127
174
128
-
> Note the usage of `beforeEach` and `afterEach` for preserving `localStorage` between all tests. Also `cy.clearLocalStorageSnapshot` is used in the `before` statement to avoid possible conflicts with other test files preserving localStorage.
175
+
> Note the usage of `beforeEach` and `afterEach` for preserving `localStorage` between all tests. Also `cy.clearLocalStorageSnapshot` is used in the `before` statement to avoid possible conflicts with other spec files preserving localStorage.
0 commit comments