赛普拉斯如何将函数产生的值存储到变量中
Cypress how to store values yielded by a function into a variable
这个问题困扰了我很长时间。我不太熟悉 javascript。开始了:
如何将函数的 return 值存储到变量中:
lenValue = cy.get(选择器).children().length
上面的代码行 returns 未定义但是当我尝试在赛普拉斯测试运行器控制台中进行操作时,我得到了一个有效的输出:
cy.$$(selector).children().length --> gives me correct number
- 如何从 then 函数内部 return 取值并捕获它以便稍后重用:
file1.js
function a(selector, attrName){
cy.get(selector).then(function ($el){
return $el.attr(attrName));
}
file2.js
state = file1Obj.a('#name','name')
您的所作所为完全有道理,但简而言之,您做不到。 (根据文档)。
https://docs.cypress.io/guides/core-concepts/variables-and-aliases/#Return-Values
但是,您可以使用别名来完成(我认为)您想要的。
https://docs.cypress.io/guides/core-concepts/variables-and-aliases/#Aliases
这是我进行的赛普拉斯测试中的一个例子,看起来很相关
let oldDescription;
cy.get('input#description').should(($input) => {
oldDescription = $input.val();
});
let randomDescription = Math.random().toString(36).substring(7);
cy.get('input#description').clear().type(randomDescription);
cy.get('input#description')
.parents('.ant-table-row')
.contains('Save').click();
cy.get('input#description').should('not.exist');
cy.contains(`${randomDescription}`);
cy.contains(`${oldDescription}`).should('not.exist');
因为 oldDescription 是在异步回调中设置的,所以期望它被设置是不安全的,在该回调之外的代码中更远,但在很多情况下,使用 cypress 你最终会有一些其他的 .接到电话或等待的事情,有效地暂停代码足够长的时间,您可以不用担心它。
@aeischeid 向您展示了错误的方法。
他的代码仅适用于静态站点,但网页很少是静态的。一旦涉及到 API 获取, 幸运时机 就会发出 window 和测试炸弹。
这就是 Cypress 命令具有自动重试功能的原因。否则我们可以用 jQuery.
构建测试
自 cy.$$(selector).children().length --> gives me correct number
起,在辅助函数中使用它。
function a(selector, attrName) {
return cy.$$(selector).attr(attrName); // jQuery methods used
}
或
function a(selector, attrName) {
return Cypress.$(selector).attr(attrName); // jQuery methods used
}
但要注意jQuery只处理静态页面,如果你要查询的属性到达慢,它不会重试。
为此使用命令
cy.get('#name')
.should('have.attr', 'name') // retries until name exists
.then(name => { // guaranteed to have a value
// use name here
})
这个问题困扰了我很长时间。我不太熟悉 javascript。开始了:
如何将函数的 return 值存储到变量中:
lenValue = cy.get(选择器).children().length
上面的代码行 returns 未定义但是当我尝试在赛普拉斯测试运行器控制台中进行操作时,我得到了一个有效的输出:
cy.$$(selector).children().length --> gives me correct number
- 如何从 then 函数内部 return 取值并捕获它以便稍后重用:
file1.js
function a(selector, attrName){
cy.get(selector).then(function ($el){
return $el.attr(attrName));
}
file2.js
state = file1Obj.a('#name','name')
您的所作所为完全有道理,但简而言之,您做不到。 (根据文档)。 https://docs.cypress.io/guides/core-concepts/variables-and-aliases/#Return-Values
但是,您可以使用别名来完成(我认为)您想要的。
https://docs.cypress.io/guides/core-concepts/variables-and-aliases/#Aliases
这是我进行的赛普拉斯测试中的一个例子,看起来很相关
let oldDescription;
cy.get('input#description').should(($input) => {
oldDescription = $input.val();
});
let randomDescription = Math.random().toString(36).substring(7);
cy.get('input#description').clear().type(randomDescription);
cy.get('input#description')
.parents('.ant-table-row')
.contains('Save').click();
cy.get('input#description').should('not.exist');
cy.contains(`${randomDescription}`);
cy.contains(`${oldDescription}`).should('not.exist');
因为 oldDescription 是在异步回调中设置的,所以期望它被设置是不安全的,在该回调之外的代码中更远,但在很多情况下,使用 cypress 你最终会有一些其他的 .接到电话或等待的事情,有效地暂停代码足够长的时间,您可以不用担心它。
@aeischeid 向您展示了错误的方法。
他的代码仅适用于静态站点,但网页很少是静态的。一旦涉及到 API 获取, 幸运时机 就会发出 window 和测试炸弹。
这就是 Cypress 命令具有自动重试功能的原因。否则我们可以用 jQuery.
构建测试自 cy.$$(selector).children().length --> gives me correct number
起,在辅助函数中使用它。
function a(selector, attrName) {
return cy.$$(selector).attr(attrName); // jQuery methods used
}
或
function a(selector, attrName) {
return Cypress.$(selector).attr(attrName); // jQuery methods used
}
但要注意jQuery只处理静态页面,如果你要查询的属性到达慢,它不会重试。
为此使用命令
cy.get('#name')
.should('have.attr', 'name') // retries until name exists
.then(name => { // guaranteed to have a value
// use name here
})