Error when try open connection with indexedDB in Cypress

Viewed 215

I make request to server for login, and then before redirect user to home page I try to open indexedDB connection in order to see this page, bacause home page go to the indexedDB and get some data. So below is my code and photo of error

    beforeEach(() => {
    cy.request({
      method: 'POST',
      url :'http://localhost:3000/api/auth/login',
      body : {
        email: "email",
        password: "password"
      }
    }).then(function(response) {



        window.indexedDB.open("testDB");

        localforage.config({
          driver: [localforage.INDEXEDDB],
          name: 'testDB',
          storeName: 'testDB',
          version: '1.0',
        });
    
      
      localforage.clear().then(() => {
        localforage.setItem('jobs', [{name: 'fdf'}]);
      });
    }).then(()=>{
      cy.visit('http://localhost:3000/');
    })
  })

enter image description here

I also try this way but it doesnt works too, what I do wrong?

function open() {
  var request = window.indexedDB.open("testDB", 1);
  request.onerror = function(event) {
   
  };
  request.onsuccess = function(event) {
  };

  request.onupgradeneeded = function(event) {
    var db = event.target.result;

    var objectStore = db.createObjectStore("jobs", { keyPath: "name" });

    objectStore.createIndex("name", "name", { unique: false });
    objectStore.add({name: 'fsdf'});
  }
}

describe('The Login Page', () => {


  beforeEach(() => {
    open()
    cy.request({
      method: 'POST',
      url :'http://localhost:3000/api/auth/login',
      body : {
        email: "gfdgfdg",
        password: "gdfgfdg"
      }
    })
        .then(function(response) {

    })
        .then(()=>{
      cy.visit('http://localhost:3000/');
    })
  })
1 Answers

One thing that is (probably) incorrect is the window reference.

Cypress runs the browser window as an automation "shell", which is what you get when you use

window.indexedDB.open("testDB")

but the app window is inside an iframe.

You can access either with

cy.window().then(win => win.indexedDB.open("testDB"))

or

cy.state('window').indexedDB.open("testDB")  // undocumented!
Related