gpt4 book ai didi

javascript - Cypress 从 json 加载数据 - 之前的夹具

转载 作者:行者123 更新时间:2023-12-01 23:24:29 24 4
gpt4 key购买 nike

我正尝试通过 Cypress 中的 fixture 从 json 文件中检索一些数据,但数据根本无法识别。

before(() => {
cy.fixture('example').then(function (data) {
console.log("this", data.user);
})

})

控制台输出用户,这是有效的。

但在那之后我有一个步骤:

Given("I check data", () => {
console.log("this", this.data.user);
});

这里的数据是未定义的。

我也尝试在 before 中设置:

this.data = data 但没有帮助。我也尝试使用 beforeEach 但没有成功。

最佳答案

不是 cucumber 用户,但在普通的 Cypress 测试中,您只能通过使回调成为函数而不是箭头函数来访问 this

Given("I check data", function() {
console.log("this", this.data.user);
});

我认为您可能还需要对数据进行别名

before(() => {
cy.fixture('example')
.then(function (data) {
console.log("this", data.user)
})
.as('data');
}

请注意, Cypress 会在测试之间清除别名,因此您需要使用 beforeEach() 而不是 before()

关于javascript - Cypress 从 json 加载数据 - 之前的夹具,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67516862/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com