javascript - 赛普拉斯从 json 加载数据 - 之前的夹具
问题描述
我正在尝试通过赛普拉斯中的夹具从 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
但没有成功。
解决方案
不是黄瓜用户,但在普通赛普拉斯测试中,您只能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');
}
请注意,赛普拉斯会在测试之间清除别名,因此您需要使用beforeEach()
而不是before()
.
推荐阅读
- c# - 如何包装 Func
C# 中参数的数量和类型未知? - javascript - 使用 toLocaleString 格式化小数以显示 2 个小数和一个逗号
- angular - 如何在 ngFor 循环表单元素中使用输入法
- flutter - Flutter Promise with modal Loading Animation while async task
- java - Java - 从 HashMap 单键多值中获取键(反向映射)
- angular - 带有 switchmap 的 Ngrx 效果中的竞争条件导致性能下降
- netlogo - 如何向 NetLogo 5.2.1 添加扩展
- azure-machine-learning-service - 如果我已部署到 VNET,有没有办法列出我的数据存储?
- symfony - EventDispatcher 和 Messenger 的区别
- mysql - SQL 您的语法有错误 - 我遇到的语法错误是什么?