feat: Add new local and session storage commands (#24859)

This commit is contained in:
Chris Breiding
2022-12-01 10:29:11 -05:00
committed by GitHub
parent e3de5e77b5
commit a80ebad8ab
24 changed files with 976 additions and 552 deletions
+62 -1
View File
@@ -142,6 +142,39 @@ declare namespace Cypress {
clear: (keys?: string[]) => void
}
// TODO: raise minimum required TypeScript version to 3.7
// and make this recursive
// https://github.com/cypress-io/cypress/issues/24875
type Storable =
| string
| number
| boolean
| null
| StorableObject
| StorableArray
interface StorableObject {
[key: string]: Storable
}
interface StorableArray extends Array<Storable> { }
type StorableRecord = Record<string, Storable>
interface OriginStorage {
origin: string
value: StorableRecord
}
interface Storages {
localStorage: OriginStorage[]
sessionStorage: OriginStorage[]
}
interface StorageByOrigin {
[key: string]: StorableRecord
}
type IsBrowserMatcher = BrowserName | Partial<Browser> | Array<BrowserName | Partial<Browser>>
interface ViewportPosition extends WindowPosition {
@@ -792,7 +825,35 @@ declare namespace Cypress {
clearCookies(options?: CookieOptions): Chainable<null>
/**
* Clear data in local storage.
* Get local storage for all origins.
*
* @see https://on.cypress.io/getalllocalstorage
*/
getAllLocalStorage(options?: Partial<Loggable>): Chainable<StorageByOrigin>
/**
* Clear local storage for all origins.
*
* @see https://on.cypress.io/clearalllocalstorage
*/
clearAllLocalStorage(options?: Partial<Loggable>): Chainable<null>
/**
* Get session storage for all origins.
*
* @see https://on.cypress.io/getallsessionstorage
*/
getAllSessionStorage(options?: Partial<Loggable>): Chainable<StorageByOrigin>
/**
* Clear session storage for all origins.
*
* @see https://on.cypress.io/clearallsessionstorage
*/
clearAllSessionStorage(options?: Partial<Loggable>): Chainable<null>
/**
* Clear data in local storage for the current origin.
* Cypress automatically runs this command before each test to prevent state from being
* shared across tests. You shouldn't need to use this command unless you're using it
* to clear localStorage inside a single test. Yields `localStorage` object.
+26
View File
@@ -1062,3 +1062,29 @@ namespace CypressClearCookiesTests {
cy.clearCookies({ timeout: '10' }) // $ExpectError
cy.clearCookies({ domain: false }) // $ExpectError
}
namespace CypressLocalStorageTests {
cy.getAllLocalStorage().then((result) => {
result // $ExpectType StorageByOrigin
})
cy.getAllLocalStorage({ log: false })
cy.getAllLocalStorage({ log: 'true' }) // $ExpectError
cy.clearAllLocalStorage().then((result) => {
result // $ExpectType null
})
cy.clearAllLocalStorage({ log: false })
cy.clearAllLocalStorage({ log: 'true' }) // $ExpectError
cy.getAllSessionStorage().then((result) => {
result // $ExpectType StorageByOrigin
})
cy.getAllSessionStorage({ log: false })
cy.getAllSessionStorage({ log: 'true' }) // $ExpectError
cy.clearAllSessionStorage().then((result) => {
result // $ExpectType null
})
cy.clearAllSessionStorage({ log: false })
cy.clearAllSessionStorage({ log: 'true' }) // $ExpectError
}