【发布时间】:2021-12-23 19:53:11
【问题描述】:
我正在测试要求您在执行测试时登录。为了保持登录状态,我在测试运行期间保留了 Cookie。
我使用自定义函数:
Cypress.Commands.add('preserveAllCookiesOnce', () => {
Cypress.Cookies.defaults({
preserve: (cookie) => {
return true;
}
})
})
也试过了
beforeEach(function () {
Cypress.Cookies.preserveOnce('session', 'YII_CSRF_TOKEN');
})
(仔细检查 cookie 的名称)
现在,在这个测试用例中,我在登录后包含了两个发布请求。但是在这两个 POST 请求测试之后,当我再次尝试访问该页面时收到 403 错误。
如果您需要更多详细信息,请告诉我。我试图将代码保持在理解问题所需的最低限度:
import {Login} from "../../pages/login/Login";
describe('test POST/client validation', () => {
beforeEach(function () {
cy.preserveAllCookiesOnce()
})
it('log in', () => {
login.goToLoginPage()
login.loginCredentials(Cypress.env('userEmail'), Cypress.env('userPass'))
})
it('test some stuff', function () {
cy.visit(Cypress.env('url') + '/index.php?r=tc/tcSettings/index&language=de')
.....
})
it('send POST/client request with incorrect values', function () {
cy.request({
method: 'POST',
...
})
.then(response => {
expect(response.status).to.eq(400)
expect(response.body.fields).to.contain({stuff})
})
})
it('send POST/client request with correct values', function () {
cy.request({
...
})
.then(response => {
expect(response.status).to.eq(200)
})
})
it('go to clients page and assert created client', () => {
cy.visit(Cypress.env('url') + '/index.php?r=client/index&language=de')
})
})
看起来保留 cookie 无法通过 POST 测试。当我在最后一步尝试访问该网站时,我得到了 403 状态。
通常我可以使用命令 cy.preserveAllCookiesOnce() 运行任意数量的 it 实例,所以我猜它可能与介于两者之间的 POST 请求有关
POST 步骤后的 Cookie 屏幕截图:
【问题讨论】:
标签: javascript cypress session-cookies preserve