Javascript 节省本地存储是一项艰巨的任务

Javascript 节省本地存储是一项艰巨的任务,javascript,local-storage,cypress,Javascript,Local Storage,Cypress,我看到了其他一些关于本地存储的帖子,但它们都与令牌和登录有关 我们有一个iframe,第一次访问我们的站点时,它会被创建并从右侧弹出,我试图阻止这个iframe永远打开。开发人员为我设置了一个标识符,告诉我这是我的Cypress测试,并且没有启动iframe,但它是不可靠的 我正在使用插件处理我的本地存储 这在我的命令文件中: 导入“cypress localstorage命令” 在我的测试中,我有以下几点: beforeEach(() => { cy.restor

我看到了其他一些关于本地存储的帖子,但它们都与令牌和登录有关

我们有一个iframe,第一次访问我们的站点时,它会被创建并从右侧弹出,我试图阻止这个iframe永远打开。开发人员为我设置了一个标识符,告诉我这是我的Cypress测试,并且没有启动iframe,但它是不可靠的

我正在使用插件处理我的本地存储

这在我的命令文件中:
导入“cypress localstorage命令”

在我的测试中,我有以下几点:

    beforeEach(() => {
        cy.restoreLocalStorage();
        cy.setLocalStorage('is_cypress_test', 'true');
    })

    afterEach(() => {
        cy.saveLocalStorage();
    })
但是,这经常会失败,iframe会打开。当它工作时,它还向控制台输出检测到Cypress的信息(这是在我们的站点代码中添加的,用于验证它是否工作)

这是我测试的基本外观

/// <reference types="Cypress" />

describe(`it browses to xxxx`, () => {
    // sets up service cookie to preserve session
    Cypress.Cookies.defaults({
        preserve: 'foo',
    });

    beforeEach(() => {
        cy.setLocalStorage('is_cypress_test', 'true');
        cy.restoreLocalStorage();
    })

    afterEach(() => {
        cy.saveLocalStorage();
    })

    it(`should log in via a POST, and browse xxx`, () => {
        cy.serviceLoginByCSRF(Cypress.env('user_name'), Cypress.env('password'));
        cy.visit('/#/asitepage');
    });
    
    describe(`it checks all xxxxx`, () => {

        it(`should verify xxxxxx`, () => {
            cy.get('h3').should('be.visible').invoke('text').then(data => {
                let regex = /\n|\*|Back/g;
                cy.textCleanup(data, regex).should('eq', 'bar');
            });
        });
     });

     describe(`it checks all yyyy`, () => {

        it(`should verify yyyy`, () => {
            cy.get('h3').should('be.visible').invoke('text').then(data => {
                let regex = /\n|\*|Back/g;
                cy.textCleanup(data, regex).should('eq', 'foo');
            });
        });
     });
});
//
描述(`it浏览到xxxx`,()=>{
//设置服务cookie以保留会话
Cypress.Cookies.Default({
保存:“foo”,
});
在每个之前(()=>{
cy.setLocalStorage('is_cypress_test','true');
恢复性存储();
})
之后(()=>{
cy.saveLocalStorage();
})
它(`应该通过帖子登录,然后浏览xxx`,()=>{
cy.serviceLoginByCSRF(Cypress.env('user_name')、Cypress.env('password');
cy.visit('/#/asitepage');
});
描述(`it检查所有xxxxx`,()=>{
它(`should verify xxxxxx`,()=>{
cy.get('h3')。应该('be.visible')。调用('text')。然后(数据=>{
让regex=/\n | \*| Back/g;
cy.textCleanup(data,regex).should('eq','bar');
});
});
});
描述(`it检查所有yyyy`,()=>{
它(`should verify yyyy`,()=>{
cy.get('h3')。应该('be.visible')。调用('text')。然后(数据=>{
让regex=/\n | \*| Back/g;
cy.textCleanup(data,regex).should('eq','foo');
});
});
});
});
波束编码

<!-- Beamer for product updates -->
<script>
    var beamer_config = {
        product_id: "foobar",
        selector: "#beamer",
        user_email: 'example@test.blogspot.gov',
        user_firstname: 'Hank',
        user_lastname: 'Williams',
        filter: 'production',
        onopen: function(){
            // localStorage.setItem("is_cypress_test", "true") --  is test
            if(localStorage.getItem("is_cypress_test")){
                console.log("Skipping beamer load for Cypress");
                return false;
            }
        }
    };
    </script>
    <script type="text/javascript" src="https://asite.js" defer="defer"></script>
<!-- // Beamer for product updates -->

var beamer_配置={
产品编号:“foobar”,
选择器:“#比默”,
用户_电子邮件:'example@test.blogspot.gov',
用户名:“Hank”,
用户名:“Williams”,
过滤器:'生产',
onopen:function(){
//setItem(“is_cypress_test”,“true”)--is test
if(localStorage.getItem(“is\u cypress\u test”)){
log(“跳过Cypress的beamer加载”);
返回false;
}
}
};
我想知道我是不是把它设置错了,还是设置错了区域

任何关于如何最好地使用它的帮助或说明,以便在每次测试之前都能在localStorage中使用,我们将不胜感激

想法


谢谢

有同样的问题,对我来说,解决这个问题的方法是将以下代码添加到
commands.js

Cypress.LocalStorage.clear = function (keys, ls, rs) {
  return;
}

有同样的问题,我解决的是在
commands.js
中添加以下代码:

Cypress.LocalStorage.clear = function (keys, ls, rs) {
  return;
}

同样的事情,由于开始使用localStorage作为令牌的存储位置,使用cypress localStorage命令或使用own命令,测试变得不稳定同样的事情,因为开始使用localStorage作为令牌的存储位置,使用cypress localStorage命令或使用own命令,测试变得不稳定