【问题标题】:Cypress preserving cookies over for a complete test suite赛普拉斯为完整的测试套件保留 cookie
【发布时间】: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


    【解决方案1】:

    cy.preserveAllCookiesOnce() 是您在 Cypress 的项目中创建的 custom command 吗?请注意,您应该改用Cypress.Cookies.preserveOnce()

    beforeEach(function () {
        // 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');
    })
    

    要启用或禁用 cookie 调试,请使用 Cypress.Cookies.debug()

    // Cypress will now log in the console when
    // cookies are set or removed
    Cypress.Cookies.debug(true)
    
    cy.setCookie('fakeCookie', '123ABC')
    cy.clearCookie('fakeCookie')
    cy.setCookie('fakeCookie', '123ABC')
    cy.clearCookie('fakeCookie')
    cy.setCookie('fakeCookie', '123ABC')
    

    你可以阅读更多关于Cypress.Cookies的APIhere

    【讨论】:

    • 抱歉,忘记添加我的自定义命令。相应地更新了 OP
    • 到目前为止,我无法让脚本正常工作,但我会与Cypress.Cookies.debug(true)一起玩,看看我是否最终能找到罪魁祸首!我很确定这会对我有所帮助
    • 使用Cypress.Cookies.debug(true)会得到什么。我会尝试在第一次尝试时不使用custom command 抽象来让它工作。实施成功后,您可以将其移至custom command
    • custom command 迄今为止在数十种规格方面都完美无缺。不幸的是,设置Cypress.Cookies.debug(true) 没有显示任何内容。据我了解,它只会在您使用命令cy.setCookie()cy.clearCookie() 时记录一些内容
    • 我接受你的回答是正确的。到目前为止,我确信您的解决方案是正确的,但是我正在测试的系统中存在一个奇怪的错误,并且它与 Cypress 无关
    【解决方案2】:

    好的,我终于找到了问题的原因。它确实与 Cookies 有关,但我绝对无法解释是什么原因造成的。

    在第一步中,我以管理员用户身份登录。我为此用户保存会话 cookie,然后继续执行 POST 请求。

    然而,在第 5 步中,当我想检查客户页面时,我突然以非管理员用户身份登录(我创建的第二个测试帐户以非管理员权限进行测试)。

    总结一下:

    it('log in', () => {
            //in this step I log in as Admin
        })
        it('test some stuff', function () {
            //in this step I am still logged in as Admin
        })
        it('send POST/client request with incorrect values', function () {
            //still logged in as Admin
        })
        it('send POST/client request with correct values', function () {
            //suddenly log in changes to non-Admin account
        })
        it('go to clients page and assert created client', () => {
            //logged in as non-Admin, therefor this test fails
        })
    })
    

    我在我的代码中找不到任何原因导致 POST 测试中的登录状态发生变化。

    但即使我找不到原因,我现在可以通过在第 5 步之前添加一个额外的步骤来解决此问题,该步骤只是以管理员用户身份注销并重新登录。

    这是导致登录更改的 POST 步骤的代码:

    it('send POST/client request with incorrect values', function () {
            cy.request({
                method: 'POST',
                url: Cypress.env('url') + '/service/clients',
                headers: {
                    'API-KEY': api_key,
                },
                body: body1,
                failOnStatusCode: false
            })
                .then(response => {
                    expect(response.status).to.eq(400)
                    expect(response.body.fields).to.contain({
                        "some stuff"
                    })
                })
        })
    

    我看不出为什么这个请求应该改变我的会话状态。此外,此规范文件中没有使用非管理员用户的登录凭据

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-21
      • 1970-01-01
      • 2023-02-14
      • 1970-01-01
      • 1970-01-01
      • 2021-10-20
      • 2020-05-16
      • 1970-01-01
      相关资源
      最近更新 更多