admin管理员组文章数量:1326292
I've been struggling with returning a value from my custom function due to the fact that I'm dealing with a promise.
Here's my code:
This is my custom function:
Cypress.Commands.add("myFunction", () => {
cy.get('#someID').then($container) => {
const isHidden = $container().children('div:nth-child(3)').is(':hidden');
console.log(isHidden); // This returns either true or false and that is good
return isHidden; // this returns $chainer but I want to return either true or false
}
});
Here is my test suite:
context('some description', () => {
before(function(){
const result = cy.myFunction();
console.log(result); // This is $chainer, but I want to get the value of true or false from isHidden variable
});
});
I've been struggling with returning a value from my custom function due to the fact that I'm dealing with a promise.
Here's my code:
This is my custom function:
Cypress.Commands.add("myFunction", () => {
cy.get('#someID').then($container) => {
const isHidden = $container().children('div:nth-child(3)').is(':hidden');
console.log(isHidden); // This returns either true or false and that is good
return isHidden; // this returns $chainer but I want to return either true or false
}
});
Here is my test suite:
context('some description', () => {
before(function(){
const result = cy.myFunction();
console.log(result); // This is $chainer, but I want to get the value of true or false from isHidden variable
});
});
Share
Improve this question
edited Jul 31, 2021 at 13:48
halfer
20.3k19 gold badges109 silver badges202 bronze badges
asked Jul 29, 2020 at 18:08
DevmixDevmix
1,8688 gold badges44 silver badges84 bronze badges
2
- Does this answer your question? How do I return the response from an asynchronous call? – Taplar Commented Jul 29, 2020 at 18:09
- 2 You can't return values like that in cypress. Did you try using aliases? – Muditha Perera Commented Jul 29, 2020 at 18:55
1 Answer
Reset to default 6I am doing this with cy.wrap
(see https://docs.cypress.io/api/mands/wrap/#Syntax) which returns a Cypress.Chainable that allows you to use then
with the result.
Cypress.Commands.add('myFunction', () => {
return cy.wrap('myResult');
})
cy.myFunction.then((text) => {
console.log(text); // myResult
})
本文标签: javascriptHow to return a value from custom function in CypressStack Overflow
版权声明:本文标题:javascript - How to return a value from custom function in Cypress? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742209396a2433424.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论