Skip to content

Add docs for new local/session storage commands #4876

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Dec 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion content/_data/sidebar.json
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@
{
"title": "Svelte API",
"slug": "svelte/api"
}
}
]
},
{
Expand Down Expand Up @@ -693,6 +693,14 @@
"title": "blur",
"slug": "blur"
},
{
"title": "clearAllLocalStorage",
"slug": "clearalllocalstorage"
},
{
"title": "clearAllSessionStorage",
"slug": "clearallsessionstorage"
},
{
"title": "clearCookie",
"slug": "clearcookie"
Expand Down Expand Up @@ -729,6 +737,14 @@
"title": "focus",
"slug": "focus"
},
{
"title": "getAllLocalStorage",
"slug": "getalllocalstorage"
},
{
"title": "getAllSessionStorage",
"slug": "getallsessionstorage"
},
{
"title": "getCookie",
"slug": "getcookie"
Expand Down
69 changes: 69 additions & 0 deletions content/api/commands/clearalllocalstorage.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
---
title: clearAllLocalStorage
---

Clear
[`localStorage`](https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage)
data for all origins with which the test has interacted.

<Alert type="warning">

Cypress automatically runs this command _before_ each test to prevent state from
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Cypress automatically runs this command _before_ each test to prevent state from
Cypress automatically runs this command _before_ each test, when [test isolation](/guides/core-concepts/writing-and-organizing-tests#Test-Isolation) is `on`, to prevent state from

Copy link
Contributor Author

@chrisbreiding chrisbreiding Nov 29, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in 837c4ad

being shared across tests when
[test isolation](/guides/core-concepts/writing-and-organizing-tests#Test-Isolation)
is `on`. You shouldn't need to use this command unless you're using it to clear
localStorage inside a single test or test isolation is `off`.

</Alert>

## Syntax

```javascript
cy.clearAllLocalStorage()
cy.clearAllLocalStorage(options)
```

### Usage

**<Icon name="check-circle" color="green"></Icon> Correct Usage**

```javascript
cy.clearAllLocalStorage()
```

### Arguments

**<Icon name="angle-right"></Icon> options** **_(Object)_**

Pass in an options object to change the default behavior of
`cy.clearAllLocalStorage()`.

| Option | Default | Description |
| ------ | ------- | ---------------------------------------------------------------------------------------- |
| `log` | `true` | Displays the command in the [Command log](/guides/core-concepts/cypress-app#Command-Log) |

### Yields [<Icon name="question-circle"/>](/guides/core-concepts/introduction-to-cypress#Subject-Management)

- `cy.clearAllLocalStorage()` yields `null`.

## Rules

### Requirements [<Icon name="question-circle"/>](/guides/core-concepts/introduction-to-cypress#Chains-of-Commands)

- `cy.clearAllLocalStorage()` requires being chained off of `cy`.

### Assertions [<Icon name="question-circle"/>](/guides/core-concepts/introduction-to-cypress#Assertions)

- `cy.clearAllLocalStorage()` cannot have any assertions chained.

### Timeouts [<Icon name="question-circle"/>](/guides/core-concepts/introduction-to-cypress#Timeouts)

<List><li>`cy.clearAllLocalStorage()` cannot time out.</li></List>

## See also

- [`cy.clearAllSessionStorage()`](/api/commands/clearallsessionstorage)
- [`cy.clearCookies()`](/api/commands/clearcookies)
- [`cy.clearLocalStorage()`](/api/commands/clearlocalstorage)
- [`cy.getAllLocalStorage()`](/api/commands/getalllocalstorage)
- [`cy.getAllSessionStorage()`](/api/commands/getallsessionstorage)
69 changes: 69 additions & 0 deletions content/api/commands/clearallsessionstorage.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
---
title: clearAllSessionStorage
---

Clear
[`sessionStorage`](https://developer.mozilla.org/en-US/docs/Web/API/Window/sessionStorage)
data for all origins with which the test has interacted.

<Alert type="warning">

Cypress automatically runs this command _before_ each test to prevent state from
being shared across tests when
[test isolation](/guides/core-concepts/writing-and-organizing-tests#Test-Isolation)
is `on`. You shouldn't need to use this command unless you're using it to clear
localStorage inside a single test or test isolation is `off`.

</Alert>

## Syntax

```javascript
cy.clearAllSessionStorage()
cy.clearAllSessionStorage(options)
```

### Usage

**<Icon name="check-circle" color="green"></Icon> Correct Usage**

```javascript
cy.clearAllSessionStorage()
```

### Arguments

**<Icon name="angle-right"></Icon> options** **_(Object)_**

Pass in an options object to change the default behavior of
`cy.clearAllSessionStorage()`.

| Option | Default | Description |
| ------ | ------- | ---------------------------------------------------------------------------------------- |
| `log` | `true` | Displays the command in the [Command log](/guides/core-concepts/cypress-app#Command-Log) |

### Yields [<Icon name="question-circle"/>](/guides/core-concepts/introduction-to-cypress#Subject-Management)

- `cy.clearAllSessionStorage()` yields `null`.

## Rules

### Requirements [<Icon name="question-circle"/>](/guides/core-concepts/introduction-to-cypress#Chains-of-Commands)

- `cy.clearAllSessionStorage()` requires being chained off of `cy`.

### Assertions [<Icon name="question-circle"/>](/guides/core-concepts/introduction-to-cypress#Assertions)

`cy.clearAllSessionStorage()` cannot have any assertions chained.

### Timeouts [<Icon name="question-circle"/>](/guides/core-concepts/introduction-to-cypress#Timeouts)

<List><li>`cy.clearAllSessionStorage()` cannot time out.</li></List>

## See also

- [`cy.clearAllLocalStorage()`](/api/commands/clearalllocalstorage)
- [`cy.clearCookies()`](/api/commands/clearcookies)
- [`cy.clearLocalStorage()`](/api/commands/clearlocalstorage)
- [`cy.getAllLocalStorage()`](/api/commands/getalllocalstorage)
- [`cy.getAllSessionStorage()`](/api/commands/getallsessionstorage)
95 changes: 95 additions & 0 deletions content/api/commands/getalllocalstorage.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
---
title: getAllLocalStorage
---

Get
[`localStorage`](https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage)
data for all origins with which the test has interacted.

## Syntax

```javascript
cy.getAllLocalStorage()
cy.getAllLocalStorage(options)
```

### Usage

**<Icon name="check-circle" color="green"></Icon> Correct Usage**

```javascript
cy.getAllLocalStorage()
```

### Arguments

**<Icon name="angle-right"></Icon> options** **_(Object)_**

Pass in an options object to change the default behavior of
`cy.getAllLocalStorage()`.

| Option | Default | Description |
| ------ | ------- | ---------------------------------------------------------------------------------------- |
| `log` | `true` | Displays the command in the [Command log](/guides/core-concepts/cypress-app#Command-Log) |

### Yields [<Icon name="question-circle"/>](/guides/core-concepts/introduction-to-cypress#Subject-Management)

`cy.getAllLocalStorage()` yields an object where the keys are origins and the
values are key-value pairs of `localStorage` data.

For example, if `key1` is set to `value1` on `https://example.com` and `key2` is
set to `value2` on `https://other.com`, `cy.getAllLocalStorage()` will yield:

```js
{
'https://example.com': {
key1: 'value1',
},
'https://other.com': {
key2: 'value2',
},
}
```

## Examples

### Get all localStorage

```javascript
cy.visit('https://example.com', {
onBeforeLoad(win) {
win.localStorage.setItem('key', 'value')
},
})

cy.getAllLocalStorage().then((result) => {
expect(result).to.deep.equal({
'https://example.com': {
key: 'value',
},
})
})
```

## Rules

### Requirements [<Icon name="question-circle"/>](/guides/core-concepts/introduction-to-cypress#Chains-of-Commands)

- `cy.getAllLocalStorage()` requires being chained off of `cy`.

### Assertions [<Icon name="question-circle"/>](/guides/core-concepts/introduction-to-cypress#Assertions)

<List><li>`cy.getAllLocalStorage()` will only run assertions you have chained
once, and will not [retry](/guides/core-concepts/retry-ability).</li></List>

### Timeouts [<Icon name="question-circle"/>](/guides/core-concepts/introduction-to-cypress#Timeouts)

- `cy.getAllLocalStorage()` cannot time out.

## See also

- [`cy.clearAllLocalStorage()`](/api/commands/clearalllocalstorage)
- [`cy.clearAllSessionStorage()`](/api/commands/clearallsessionstorage)
- [`cy.clearCookies()`](/api/commands/clearcookies)
- [`cy.clearLocalStorage()`](/api/commands/clearlocalstorage)
- [`cy.getAllSessionStorage()`](/api/commands/getallsessionstorage)
95 changes: 95 additions & 0 deletions content/api/commands/getallsessionstorage.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
---
title: getAllSessionStorage
---

Get
[`sessionStorage`](https://developer.mozilla.org/en-US/docs/Web/API/Window/sessionStorage)
data for all origins with which the test has interacted.

## Syntax

```javascript
cy.getAllSessionStorage()
cy.getAllSessionStorage(options)
```

### Usage

**<Icon name="check-circle" color="green"></Icon> Correct Usage**

```javascript
cy.getAllSessionStorage()
```

### Arguments

**<Icon name="angle-right"></Icon> options** **_(Object)_**

Pass in an options object to change the default behavior of
`cy.getAllSessionStorage()`.

| Option | Default | Description |
| ------ | ------- | ---------------------------------------------------------------------------------------- |
| `log` | `true` | Displays the command in the [Command log](/guides/core-concepts/cypress-app#Command-Log) |

### Yields [<Icon name="question-circle"/>](/guides/core-concepts/introduction-to-cypress#Subject-Management)

`cy.getAllSessionStorage()` yields an object where the keys are origins and the
values are key-value pairs of `sessionStorage` data.

For example, if `key1` is set to `value1` on `https://example.com` and `key2` is
set to `value2` on `https://other.com`, `cy.getAllSessionStorage()` will yield:

```js
{
'https://example.com': {
key1: 'value1',
},
'https://other.com': {
key2: 'value2',
},
}
```

## Examples

### Get all sessionStorage

```javascript
cy.visit('https://example.com', {
onBeforeLoad(win) {
win.sessionStorage.setItem('key', 'value')
},
})

cy.getAllSessionStorage().then((result) => {
expect(result).to.deep.equal({
'https://example.com': {
key: 'value',
},
})
})
```

## Rules

### Requirements [<Icon name="question-circle"/>](/guides/core-concepts/introduction-to-cypress#Chains-of-Commands)

- `cy.getAllSessionStorage()` requires being chained off of `cy`.

### Assertions [<Icon name="question-circle"/>](/guides/core-concepts/introduction-to-cypress#Assertions)

<List><li>`cy.getAllSessionStorage()` will only run assertions you have chained
once, and will not [retry](/guides/core-concepts/retry-ability).</li></List>

### Timeouts [<Icon name="question-circle"/>](/guides/core-concepts/introduction-to-cypress#Timeouts)

- `cy.getAllSessionStorage()` cannot time out.

## See also

- [`cy.clearAllLocalStorage()`](/api/commands/clearalllocalstorage)
- [`cy.clearAllSessionStorage()`](/api/commands/clearallsessionstorage)
- [`cy.clearCookies()`](/api/commands/clearcookies)
- [`cy.clearsessionStorage()`](/api/commands/clearsessionStorage)
- [`cy.getAllLocalStorage()`](/api/commands/getalllocalstorage)