【问题标题】:Saving local storage is being flaky - Cypress保存本地存储是不稳定的 - 赛普拉斯
【发布时间】:2020-11-17 21:55:28
【问题描述】:

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

我们有一个 iframe,它会在第一次访问时从我们网站的右侧创建并弹出,我试图阻止这个 iframe 永远打开。开发人员为我设置了一个标识符,告诉我这是我的 Cypress 测试,并且不会触发 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 中。

想法?

谢谢

【问题讨论】:

  • 同样的事情,因为开始使用 localStorage 作为令牌的存储位置,使用 cypress-localstorage-commands 或使用自己的命令,测试变得不稳定 ????

标签: javascript local-storage cypress


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多