使用赛普拉斯测试在 Laravel 中存储和清除 cookie 的正确方法

Proper way to store and clear cookie in Laravel with Cypress Testing

我已将 cypress 引入 Laravel 应用程序,并且正在对 axios.post 请求进行一些测试。我已经弄清楚如何让 cypress 在 preservingxsrf token 方面工作,以便 axios 调用可以完成它的过程。问题是,当我尝试在本地化测试中清除 cookie 时,它​​不起作用。下次我打开测试时,我的用户已经登录到他们的帐户,因此每次测试运行时都不会重置 cookie。我这样做不对吗?

Cypress/Support/index.js

Cypress.Cookies.defaults({
    preserve: ["XSRF-TOKEN", "myapp_session", "remember_token"]
});

在我的赛普拉斯测试中的 it() 内部。此时,用户已通过身份验证并正在填写表格。这是单击时提交表单的要点。单击该按钮时,将发送一个 axios.post() 请求。 post 正在按预期工作,我再次希望用户在测试重新启动后必须重新进行身份验证。

 //Submit the form
        cy.get('[data-cy=system-create-submit]').click()

        cy.wait(4500)

        cy.location('pathname').should('eq', '/system')

        //cy.getCookie('myapp_session') // clear the 'myapp_session' cookie
        //Update as of 10.29.21
        cy.clearCookie('myapp_session')

如果我关闭并打开 + 重新启动测试,即使测试的第一步是让用户登录,用户仍然通过身份验证。

编辑:将 clearCookie() 命令放在我测试的最后有效。

您要查找的命令似乎是

cy.clearCookie('myapp_session')