如何使用 cy.wrap 访问 beforeEach 中的变量
How can I access a variable in beaforeEach using cy.wrap
我有这段代码,当我 运行 它说“未定义的读数 '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,
})
}
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
我有这段代码,当我 运行 它说“未定义的读数 '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,
})
}
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