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
})