question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

cy.session: Support IndexedDB

See original GitHub issue

What would you like?

The new cy.session API currently doesn’t do anything with indexeddb. Is that likely to be supported in the future?

Why is this needed?

I’m testing an app that stores significants amounts of data in indexeddb to support offline use.

In the app, when a users choses to “Go Offline,” data is downloaded from the server to indexeddb. I would like to cache/restore that data with cy.session so that each tests for offline functionality doesn’t have to re-download that data.

Other

No response

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:9
  • Comments:7

github_iconTop GitHub Comments

3reactions
nicolassanmarcommented, Dec 20, 2022

I found a workaround to circumvent this: You can store the contents of the IndexedDB in localstorage in the setup() function, and then restore them on the validate() function. The exportIDB and importIDB functions use the utilities provided by this gist: https://gist.github.com/loilo/ed43739361ec718129a15ae5d531095b

Cypress.Commands.add(
  'exportIDB',
  (dbName) =>
    new Cypress.Promise(async (resolve) => {
      indexedDB.open(dbName).onsuccess = async (event) => {
        const db = (event.target as IDBOpenDBRequest).result
        const json = await exportToJson(db)
        console.log(json)
        localStorage.setItem(dbName, json)
        resolve(json)
      }
    })
)

Cypress.Commands.add(
  'importIDB',
  (dbName) =>
    new Cypress.Promise(async (resolve) => {
      const json = localStorage.getItem(dbName)
      console.log(json)
      if (!json) {
        resolve()
        return
      }
      indexedDB.open(dbName).onsuccess = async (event) => {
        const db = (event.target as IDBOpenDBRequest).result
        await clearDatabase(db)
        await importFromJson(db, json.toString())
        resolve()
      }
    })
)


Cypress.Commands.add('fn', () => {
  cy.session(
    () => {
      /*regular session setup */

      // export the data to localStorage, as session does not persist the IndexedDB
      cy.exportIDB()
    },
    {
      validate: () => {
        // import the data from localstorage to the indexedDB
        cy.importIDB()

        /* validate extra stuff */
      },
      // if you want this session to be cached only for a single file, remove the following option
      cacheAcrossSpecs: true,
    }
  )
})
0reactions
liquid1982commented, Dec 20, 2022

This is brilliant! Will give it a try. Thanks for sharing!

Read more comments on GitHub >

github_iconTop Results From Across the Web

session - Cypress Documentation
The cy.session() command will inherit the testIsolation value to determine whether or not the page is cleared when caching and restoring the browser...
Read more >
How open connection with indexedDB for Cypress tests?
The IndexedDB can be manipulated easily from Cypress by using localforage package. I'm using it to set things like the access_token inside ...
Read more >
Cypress testing your IndexedDb contents with ... - This Dot Labs
IndexedDb is a browser API for storing significant amounts of structured data locally. It is very useful when you are working on a...
Read more >
cypress-io/cypress - Gitter
Hi guys, I would like to create a custom command to my cypress/support/commands.js file to login before each test. The problem is I...
Read more >
paramètres des témoins - Stripe
Remembers whether you dismissed a notice on support.stripe.com. ... Session cookie used by splashthat for identification of visitors to event marketing ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found