Skip to content
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

cy.session: Support IndexedDB #18350

Open
nateroling opened this issue Oct 4, 2021 · 13 comments
Open

cy.session: Support IndexedDB #18350

nateroling opened this issue Oct 4, 2021 · 13 comments
Labels
topic: session Issues when using session command Triaged Issue has been routed to backlog. This is not a commitment to have it prioritized by the team. type: feature New feature that does not currently exist

Comments

@nateroling
Copy link

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

@nateroling nateroling changed the title cy.session: support indexeddb cy.session: Support IndexedDB Oct 4, 2021
@jennifer-shehane jennifer-shehane added the topic: session Issues when using session command label Oct 5, 2021
@dannyskoog
Copy link

+1 👍

1 similar comment
@werlein
Copy link

werlein commented May 17, 2022

+1 👍

@jaytavares
Copy link

jaytavares commented Sep 23, 2022

Any word on this? Firebase uses indexedDB for session storage. So cy.session() doesn't work to cache a logged in session in a Firebase app.

@liquid1982
Copy link

+1

Has anyone tried manually saving and restoring IndexedDB using localforage or similar? I also am having challenge because of Firebase.

Would it be possible to coordinate efforts and put together a workaround? I'd love to help.

@tianhuil
Copy link

Facing a similar issue here and would love to work on a solution and publish it back here. I haven't been able to figure out how session actually stores data between sessions. Does anyone have pointers?

@nicolassanmar
Copy link

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,
    }
  )
})

@liquid1982
Copy link

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

@cypress-app-bot
Copy link
Collaborator

This issue has not had any activity in 180 days. Cypress evolves quickly and the reported behavior should be tested on the latest version of Cypress to verify the behavior is still occurring. It will be closed in 14 days if no updates are provided.

@cypress-app-bot cypress-app-bot added the stale no activity on this issue for a long period label Jun 19, 2023
@segevfiner
Copy link

Still needed

@cypress-app-bot cypress-app-bot removed the stale no activity on this issue for a long period label Jun 20, 2023
@mike-plummer mike-plummer added type: feature New feature that does not currently exist Triaged Issue has been routed to backlog. This is not a commitment to have it prioritized by the team. labels Jun 28, 2023
@asier-perez
Copy link

Hi, I am in a JavaScript project and the workaround proposed by @nicolassanmar doesn't work for me. When importDB is executed, event is always empty. For this project I have replaced const db = (event.target as IDBOpenDBRequest).result with const db = event.target.result, because otherwise it was giving me a compilation error. Is this change correct for JavaScript? Or does anybody know why event could be empty?

@Dale-777
Copy link

@jennifer-shehane @mike-plummer could you please see if this can be added to the roadmap?

It seems that cy.session() does not handle IndexedDB, this is used by Firebase for session storage. Therefore cy.session() does not work to cache a logged-in session in a Firebase app.

I know there's a viable workaround, but it seems to me to be a small fix for a big win...

#27155 (comment)

@tinu-doit-intl
Copy link

Same issue here as well. Good to have a fix than a work around.

@jaytavares
Copy link

Same issue. The workaround didn't work for me. 🤷‍♂️ The application stayed logged out and the session would need to be recreated every time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: session Issues when using session command Triaged Issue has been routed to backlog. This is not a commitment to have it prioritized by the team. type: feature New feature that does not currently exist
Projects
None yet
Development

No branches or pull requests