|
1 | 1 | [![Build status][build-image]][build-url] [![Coverage Status][coveralls-image]][coveralls-url] [![Quality Gate][quality-gate-image]][quality-gate-url] [![Mutation testing status][mutation-image]][mutation-url]
|
2 | 2 |
|
3 |
| -[![NPM dependencies][npm-dependencies-image]][npm-dependencies-url] [](https://renovatebot.com) [![Last commit][last-commit-image]][last-commit-url] [![Last release][release-image]][release-url] |
| 3 | +[](https://renovatebot.com) [![Last commit][last-commit-image]][last-commit-url] [![Last release][release-image]][release-url] |
4 | 4 |
|
5 | 5 | [![NPM downloads][npm-downloads-image]][npm-downloads-url] [![License][license-image]][license-url] [](https://app.fossa.io/projects/git%2Bgithub.com%2Fjavierbrea%2Fcypress-localstorage-commands?ref=badge_shield)
|
6 | 6 |
|
@@ -39,18 +39,24 @@ You can now use all next commands:
|
39 | 39 |
|
40 | 40 | ### Commands
|
41 | 41 |
|
42 |
| -##### `cy.saveLocalStorage()` |
| 42 | +##### `cy.saveLocalStorage([snapshotName])` |
43 | 43 |
|
44 | 44 | Saves current localStorage values into an internal "snapshot".
|
45 | 45 |
|
46 |
| -##### `cy.restoreLocalStorage()` |
| 46 | +* `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 | 47 |
|
48 |
| -Restores localStorage to previously "snapshot" saved values. |
| 48 | +##### `cy.restoreLocalStorage([snapshotName])` |
49 | 49 |
|
50 |
| -##### `cy.clearLocalStorageSnapshot()` |
| 50 | +Restores localStorage to previously "snapshot" saved values. __ |
| 51 | + |
| 52 | +* `snapshotName` _(String)_: Optional. If provided, the localStorage will be restored using data from that specific snapshot. |
| 53 | + |
| 54 | +##### `cy.clearLocalStorageSnapshot([snapshotName])` |
51 | 55 |
|
52 | 56 | Clears localStorage "snapshot" values, so previously saved values are cleaned.
|
53 | 57 |
|
| 58 | +* `snapshotName` _(String)_: Optional. If provided, only data from that specific snapshot will be cleared. |
| 59 | + |
54 | 60 | ##### `cy.getLocalStorage(item)`
|
55 | 61 |
|
56 | 62 | Gets localStorage item. Equivalent to `localStorage.getItem` in browser.
|
@@ -153,6 +159,44 @@ describe("localStorage cookies-accepted item", () => {
|
153 | 159 | });
|
154 | 160 | ```
|
155 | 161 |
|
| 162 | +#### Named snapshots |
| 163 | + |
| 164 | +Next example shows how named snapshots can be used to storage different states of `localStorage` and restore one or another depending of the test: |
| 165 | + |
| 166 | +```js |
| 167 | +describe("Accept cookies button", () => { |
| 168 | + const COOKIES_BUTTON = "#accept-cookies"; |
| 169 | + |
| 170 | + before(() => { |
| 171 | + cy.clearLocalStorageSnapshot(); |
| 172 | + }); |
| 173 | + |
| 174 | + it("should be visible", () => { |
| 175 | + cy.visit("/"); |
| 176 | + cy.get(COOKIES_BUTTON).should("be.visible"); |
| 177 | + cy.saveLocalStorage("cookies-not-accepted"); |
| 178 | + }); |
| 179 | + |
| 180 | + it("should not exist after clicked", () => { |
| 181 | + cy.get(COOKIES_BUTTON).click(); |
| 182 | + cy.get(COOKIES_BUTTON).should("not.exist"); |
| 183 | + cy.saveLocalStorage("cookies-accepted"); |
| 184 | + }); |
| 185 | + |
| 186 | + it("should be visible when cookies are not accepted", () => { |
| 187 | + cy.restoreLocalStorage("cookies-not-accepted"); |
| 188 | + cy.visit("/"); |
| 189 | + cy.get(COOKIES_BUTTON).should("be.visible"); |
| 190 | + }); |
| 191 | + |
| 192 | + it("should not exist when cookies are accepted", () => { |
| 193 | + cy.restoreLocalStorage("cookies-accepted"); |
| 194 | + cy.visit("/"); |
| 195 | + cy.get(COOKIES_BUTTON).should("not.exist"); |
| 196 | + }); |
| 197 | +}); |
| 198 | +``` |
| 199 | + |
156 | 200 | ### Disabling localStorage
|
157 | 201 |
|
158 | 202 | 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.
|
@@ -263,8 +307,6 @@ MIT, see [LICENSE](./LICENSE) for details.
|
263 | 307 | [license-url]: https://github.com/javierbrea/cypress-localstorage-commands/blob/master/LICENSE
|
264 | 308 | [npm-downloads-image]: https://img.shields.io/npm/dm/cypress-localstorage-commands.svg
|
265 | 309 | [npm-downloads-url]: https://www.npmjs.com/package/cypress-localstorage-commands
|
266 |
| -[npm-dependencies-image]: https://img.shields.io/david/javierbrea/cypress-localstorage-commands.svg |
267 |
| -[npm-dependencies-url]: https://david-dm.org/javierbrea/cypress-localstorage-commands |
268 | 310 | [quality-gate-image]: https://sonarcloud.io/api/project_badges/measure?project=javierbrea_cypress-localstorage-commands&metric=alert_status
|
269 | 311 | [quality-gate-url]: https://sonarcloud.io/dashboard?id=javierbrea_cypress-localstorage-commands
|
270 | 312 | [release-image]: https://img.shields.io/github/release-date/javierbrea/cypress-localstorage-commands.svg
|
|
0 commit comments