尝试使用Cypress中的IndexedDB打开连接时出错 [英] Error when try open connection with indexedDB in Cypress

查看:23
本文介绍了尝试使用Cypress中的IndexedDB打开连接时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我向服务器发出登录请求,然后在将用户重定向到主页之前尝试 打开IndexedDB Connection以查看此页面,因为主页转到IndexedDB 获取一些数据。因此,下面是我的代码和错误照片

    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/');
    })
  })

我也尝试过这种方式,但也不起作用,我哪里做错了?

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/');
    })
  })

推荐答案

有一件事(可能)是不正确的,那就是window引用。

Cypress将浏览器窗口作为自动外壳运行,这是您使用时获得的

window.indexedDB.open("testDB")

但应用程序窗口在IFRAME内。

您可以使用

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

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

这篇关于尝试使用Cypress中的IndexedDB打开连接时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆