gpt4 book ai didi

javascript - Cypress 从窗口获取全局变量

转载 作者:行者123 更新时间:2023-11-28 21:16:24 25 4
gpt4 key购买 nike

为什么我不能使用 Cypress 获取全局变量(附加到 window 对象?)的内容? ?
我已经尝试了很多不同的东西并最终得到了这个,它仍然不起作用:

it('blabla', () => {...
var arr = [];
cy.window()
.then((win) => {
cy.log(win);
arr = win.teasers;
})
.then(() => {
arr.forEach(function (el, i) {
cy.get('h1').should('be.visible');
cy.get('h1').should('have.text', win.teasers[i]);
cy.tick(11800);
});
});
...});

错误为 ReferenceError: win is not defined,即使它在上面记录了 window 就好了,并且记录的对象还包含 teasers 数组作为属性。为什么它在第一个 .then() 中记录第二行时不能与第二行一起使用?
感谢任何帮助,谢谢。

编辑:注释掉上面的代码片段并改为执行以下断言:

cy.window().should('have.property', 'teasers');

工作正常。这意味着该属性必须存在,并留下了为什么我不能遍历该数组的问题?

最佳答案

win 不在第二个 then() 的范围内,因此出现错误 ReferenceError: win is not defined。它只可用一次,在第一个 then() 中。

由于 arr 的值在第二个 then() 执行时已经设置为 win.teasers,因此更改此:

cy.get('h1').should('have.text', win.teasers[i]);

对此:

cy.get('h1').should('have.text', arr[i]);

关于javascript - Cypress 从窗口获取全局变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57418282/

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