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
Extends Cypress' cy commands with localStorage methods. Allows preserving localStorage between tests.
9
+
Extends Cypress' cy commands with localStorage methods. Allows preserving localStorage between tests and disabling localStorage.
10
10
11
-
## The problem
11
+
## The problems
12
12
13
-
You want to preserve localStorage between Cypress tests.
13
+
* You want to preserve localStorage between Cypress tests.
14
+
* You want to disable localStorage to check error handling.
14
15
15
16
## This solution
16
17
17
-
This solution allows you to use all browser localStorage methods through Cypress commands, and preserve it between tests.
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.
18
19
19
20
## Installation
20
21
@@ -38,45 +39,47 @@ You can now use all next commands:
38
39
39
40
### Commands
40
41
41
-
Save current localStorage values into an internal "snapshot":
42
+
##### `cy.saveLocalStorage()`
42
43
43
-
```js
44
-
cy.saveLocalStorage();
45
-
```
44
+
Saves current localStorage values into an internal "snapshot".
46
45
47
-
Restore localStorage to previously "snapshot" saved values:
46
+
##### `cy.restoreLocalStorage()`
48
47
49
-
```js
50
-
cy.restoreLocalStorage();
51
-
```
48
+
Restores localStorage to previously "snapshot" saved values.
52
49
53
-
Clear localStorage "snapshot" values:
50
+
##### `cy.clearLocalStorageSnapshot()`
54
51
55
-
```js
56
-
cy.clearLocalStorageSnapshot();
57
-
```
52
+
Clears localStorage "snapshot" values, so previously saved values are cleaned.
58
53
59
-
Get localStorage item. Equivalent to `localStorage.getItem` in browser:
54
+
##### `cy.getLocalStorage(item)`
60
55
61
-
```js
62
-
cy.getLocalStorage("item");
63
-
```
56
+
Gets localStorage item. Equivalent to `localStorage.getItem` in browser.
64
57
65
-
Set localStorage item. Equivalent to `localStorage.setItem` in browser:
58
+
*`item`_(String)_: Item to get from `localStorage`.
66
59
67
-
```js
68
-
cy.setLocalStorage("item", "value");
69
-
```
60
+
##### `cy.setLocalStorage(item, value)`
70
61
71
-
Remove localStorage item. Equivalent to `localStorage.removeItem` in browser:
62
+
Sets localStorage item. Equivalent to `localStorage.setItem` in browser.
72
63
73
-
```js
74
-
cy.removeLocalStorage("item");
75
-
```
64
+
*`item`_(String)_: Item to set value.
65
+
*`value`_(String)_: Value to be set.
66
+
67
+
##### `cy.removeLocalStorage(item)`
68
+
69
+
Removes localStorage item. Equivalent to `localStorage.removeItem` in browser.
70
+
71
+
*`item`_(String)_: Item to be removed.
72
+
73
+
##### `cy.disableLocalStorage(options)`
74
+
75
+
Disables localStorage. It produces localStorage methods to throw errors.
76
+
77
+
*`options`_(Object)_: Options to use when disabling `localStorage`.
78
+
*`withError`_(Error)_: If provided, invocations to `localStorage` methods will throw this error.
76
79
77
80
### Preserving local storage between tests
78
81
79
-
Use `saveLocalStorage` to save a snapshot of current `localStorage` at the end of one test, and use the `restoreLocalStorage` command to restore it at the beginning of another one. _The usage of `beforeEach` and `afterEach` is recommended for this purpose._
82
+
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._
80
83
81
84
### Examples
82
85
@@ -116,7 +119,7 @@ describe("Accept cookies button", () => {
116
119
});
117
120
```
118
121
119
-
> Note the usage of `beforeEach` and `afterEach` for preserving `localStorage` between all tests. Also `clearLocalStorageSnapshot` is used in the `before` statement to avoid possible conflicts with other test files preserving localStorage.
122
+
> 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.
Use `cy.disableLocalStorage()` to simulate that `localStorage` is disabled, producing that any invocation to `localStorage.setItem`, `localStorage.getItem`, `localStorage.removeItem` or `localStorage.clear` will throw an error. [As MDN docs recommend](https://developer.mozilla.org/en-US/docs/Web/API/Storage/setItem), _"developers should make sure to always catch possible exceptions from setItem()"_. This command allows to test that possible exceptions are handled correctly.
159
+
160
+
Note that:
161
+
162
+
* Only pages loaded after calling this command will have `localStorage` disabled, so always use `cy.reload` or `cy.visit` after executing it.
163
+
* The `localStorage` only remains disabled for all pages loaded during the current test. If you want to disable it for multiple tests, execute it in all of them, or in a `beforeEach` statement.
164
+
* If any of the other plugin commands (except `clearLocalStorageSnapshot`) is executed while `localStorage` is disabled, it will do nothing but producing a Cypress log as: _"localStorage.setItem is disabled"_
165
+
166
+
### Examples
167
+
168
+
#### Disabling localStorage in a single test
169
+
170
+
Based on previous "Accept cookies button" example, next tests could be added:
0 commit comments