【问题标题】:How can I access a variable in beaforeEach using cy.wrap如何使用 cy.wrap 访问 beforeEach 中的变量
【发布时间】:2021-12-01 15:45:11
【问题描述】:

我有这段代码,当我运行它时,它显示“未定义读取'auth'”。我在这里做错了什么?我阅读了文档,它应该可以工作?

beforeEach(() => {
        cy.login(email).then((token) => {
            let headers = { "x-access-token": token }
            cy.wrap(headers).as("auth")
        })
    })

    it("Buy " + par, () => {
        if (stables.indexOf(par) < 0) {
            cy.request({
                method: "POST",
                url: url,
                headers: this.auth,
                body: payloadBUYToken,
            })
        }

【问题讨论】:

    标签: cypress


    【解决方案1】:

    this.* 符号仅在函数回调中可用,但您的代码使用箭头符号。

    这里是更正的变体:

    beforeEach(() => {
            cy.login(email).then((token) => {
                let headers = { "x-access-token": token }
                cy.wrap(headers).as("auth")
            })
        })
    
        it("Buy " + par, function() {
            if (stables.indexOf(par) < 0) {
                cy.request({
                    method: "POST",
                    url: url,
                    headers: this.auth,
                    body: payloadBUYToken,
                })
            }
    

    详情请见this article

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-08
      • 2011-09-17
      相关资源
      最近更新 更多