首页 > 解决方案 > 保存本地存储是不稳定的 - 赛普拉斯

问题描述

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

我们有一个 iframe,它会在第一次访问时从我们网站的右侧创建并弹出,我试图阻止这个 iframe 永远打开。开发人员为我设置了一个标识符,告诉我这是我的赛普拉斯测试并且不触发 iframe,但它很不稳定。

我正在使用插件https://www.npmjs.com/package/cypress-localstorage-commands来处理我的本地存储。

这是在我的命令文件中: import "cypress-localstorage-commands";

在我的测试中,我有以下内容:

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

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

但是,这经常会失败并且 iframe 会打开。当它工作时,它还会向控制台打印出检测到赛普拉斯的信息(这是在我们的网站代码中添加的内容,以验证它是否正常工作)。

这是我的测试的基本外观。

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

投影仪代码

<!-- 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 -->

我想知道我是否以错误的方式或错误的区域设置了这个?

任何帮助,或关于如何最好地使用它的注释,因此在每次测试之前它总是会在 localStorage 中保存,将不胜感激。

想法?

谢谢

标签: javascriptlocal-storagecypress

解决方案


有同样的问题,为我解决的问题是将以下代码添加到commands.js

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

推荐阅读