【问题标题】:Cypress.Cookies.preserveOnce('session_id') isn't working when called in afterEach hookCypress.Cookies.preserveOnce('session_id') 在 afterEach 钩子中调用时不起作用
【发布时间】:2021-12-04 20:54:35
【问题描述】:

我正在使用 cypress 测试,我希望避免在每次测试之前都必须登录。所以,我想在每个测试文件中保留 cookie。

触发了 afterEach 钩子中的日志语句,但是在第二个测试用例中清除了 cookie。

describe('Users Page Scenarios', () => {
    before(() => {
        myApp.pages.Login.navigate();
        myApp.pages.Login.login(
            credentials.globalAdmin.email,
            credentials.globalAdmin.password
        );
    });

    beforeEach('navigate to users page before each test', () => {
        myApp.sharedComponents.Header.navigateToUsers();
    });

    afterEach(() => {
        Cypress.Cookies.preserveOnce('session_id');        
        cy.log('test');
    });

    describe('Users List', () => {
        it('Should redirect the user to users page after clicking on users in the navigation header', () => {
            cy.url().should('eq', `${Cypress.config().baseUrl}/user`);
        });
    })

    describe('New User Creation', () => {
        it('Should open new user modal after clicking on invite administrator', () => {
            myApp.pages.Users.UsersList.inviteAdministrator();
            cy.url().should('eq', `${Cypress.config().baseUrl}/user/new`);
        });

        it('Should create a new user successfully', () => {
            myApp.pages.Users.UsersList.inviteAdministrator();
            myApp.pages.Users.UsersInfo.createNewUser(user.generateUser());
        })
    });

【问题讨论】:

    标签: mocha.js cypress e2e-testing


    【解决方案1】:

    docs 表示Cypress.Cookies.preserveOnce('session_id') 用于beforeEach()

    看起来为时已晚。

    describe('Dashboard', () => {
      before(() => {
        // log in only once before any of the tests run.
        // your app will likely set some sort of session cookie.
        // you'll need to know the name of the cookie(s), which you can find
        // in your Resources -> Cookies panel in the Chrome Dev Tools.
        cy.login()
      })
    
      beforeEach(() => {
        // before each test, we can automatically preserve the
        // 'session_id' and 'remember_token' cookies. this means they
        // will not be cleared before the NEXT test starts.
        //
        // the name of your cookies will likely be different
        // this is an example
        Cypress.Cookies.preserveOnce('session_id', 'remember_token')
      })
    

    如果您要保留 localStorage 或 sessionStorage,或者您没有正确识别所有 cookie,请尝试使用 cy.session()

    beforeEach(() => {                 // must be beforeEach()
      cy.session('mySession', () => {  // preserves localStorage, sessionStorage, cookies
        myApp.pages.Login.navigate();
        myApp.pages.Login.login(...);  // only called once (despite beforeEach())
      })
    })
    

    【讨论】:

    • 我在 beforeEach 中也尝试过,但没有成功。
    • 已经被很多人成功使用了,你的cookie名称对吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-15
    • 2021-11-12
    • 2020-09-24
    • 2021-04-10
    • 1970-01-01
    • 2011-06-11
    相关资源
    最近更新 更多