【问题标题】:Validating the cookie cypress throws 'TypeError: cy.chain is not a function验证 cookie cypress 抛出 'TypeError: cy.chain is not a function
【发布时间】:2018-08-31 08:54:30
【问题描述】:

我在下面的“登录”赛普拉斯测试中验证 cookie,但赛普拉斯抛出以下错误:

TypeError: cy.chain 不是函数

'../support/index.js'下明确导入了以下内容可以请教一下为什么会抛出这个错误

import './commands'

柏树测试:

describe("Login test validate cookie", () => {
    it.only('Verify the cookies test for login', function() {
        cy
        .login(Cypress.env('email'), Cypress.env('password'))
        cy
        .getCookie('csrftoken')
        .then((csrftoken) => {
            console.log(csrftoken)
        })
    })

以下是我的“登录”方法/功能../support/commands.js

Cypress.Commands.add('login', (email, password) => {
    return cy.chain().request({
        method: 'POST',
        form: true,
        url: '${cypress.env("test_server")}',
        body: '{"email", "password"}',
    })
}); 

“cypress.env.json”文件中提供了以下详细信息

{ 
  "email": "test@soccer.nl",
  "password": "test1234"
}

【问题讨论】:

    标签: javascript cypress


    【解决方案1】:

    错误是正确的; cy.chain() 确实不是一个函数。但是,您的命令中存在许多问题:

    1. 除非您打算链接此命令,否则您不需要返回任何内容。不过,这当然不会造成任何伤害。
    2. 如前所述,.chain() 是不必要的。
    3. url 字段需要使用反引号 (``) 才能使 ${...} 起作用。
    4. 您的body 字段将包含“电子邮件”和“密码”,而不是您的实际电子邮件和密码。

    可能还有其他问题,但这些是我能看到的。

    如果没有这些问题,您的命令如下所示:

    Cypress.Commands.add('login', (email, password) => {
        cy.request({
            method: 'POST',
            form: true,
            url: `${cypress.env("test_server")}`,
            body: `{"${email}", "${password}"}`,
        });
    }); 
    

    【讨论】:

    猜你喜欢
    • 2021-05-30
    • 2015-11-06
    • 2014-01-31
    • 2021-01-07
    • 2018-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-20
    相关资源
    最近更新 更多