[英]Cypress 10 - variables between cypress tests
我正在尝试从响应正文中获取一个值,然后在赛普拉斯 10 的另一个 it-s 中使用它。我已经为获取和设置全局变量创建了自定义 cy.task,但是当我需要例如 10 个值时,这不是一个好的解决方案从之前的测试:
await cy.task("getValue", {key: "one"}).then((one){ await cy.task("getValue", {key: "two"}).then((two){ await cy.task("getValue", {key: "three"}).then((three){ await cy.task("getValue", {key: "four"}).then((four){ ... if(one === null && two === null && three == null && four == null){ do smth } }); }); }); });
另一种解决方案是从 cy.task 获取值并将其保存到局部变量中:
let one_helper; let two_helper; let ... cy.task("getValue", {key: "one"}).then((one){ one_helper = one; } cy.task("getValue", {key: "two"}).then((two){ two_helper = two; } ... if(one_helper == null && two_helper == null){ do smth }
但这对我来说不是一个好的解决方案,因为我从之前的回复中获得了很多价值。
您可以将一组键传递给一个任务调用。
const { defineConfig } = require('cypress')
const store = {}
module.exports = defineConfig({
e2e: {
setupNodeEvents(on, config) {
on('task', {
getValue(keys) {
if (typeof keys === 'string') { // only a single key?
keys = [keys] // arrayify it
}
return keys.map(key => store[key])
},
})
}
}
})
您可以使用Cypress.env()全局保存任何值。
Cypress.env('somekey', 'somevalue') //saves the value
Cypress.env('somekey') //gets 'somevalue'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.