Resolve data in custom command
See original GitHub issueDescription
I cannot find a way to resolve data in a custom command, using a Cypress.Promise
.
Code
In support/commands
Cypress.addParentCommand('signIn', function (email, password) {
return new Cypress.Promise((resolve, reject) => {
cy.request({
method: 'POST',
url: '/auth',
body: {email, password}
})
.then(result => {
resolve(result.body);
}, reject)
});
});
In my test:
describe('any test', function () {
it('should work', function() {
cy.signIn('user@email.com', 'password')
.then(res => console.log(res))
});
});
When I return the promise like above, cy.signIn
times out. It does not fail when I do not return the promise, but I still can’t access the request data.
Am I doing anything wrong?
Issue Analytics
- State:
- Created 7 years ago
- Comments:19 (11 by maintainers)
Top Results From Across the Web
Custom Commands in Cypress - Tools QA
These commands can be created to give a different behavior to the existing in-build Cypress command or can be used to create a...
Read more >Custom Command Which invokes an action that returns row ...
Hi, in one of my usecases, I have a custom command. When clicked it does some server side calculations and returns an updated...
Read more >Writing a Custom Cypress Command - Gleb Bahmutov
This blog post teaches you how to write a reusable Cypress Custom Command. Simple custom command Simple command limitation Custom command ...
Read more >How to return a promise from a Cypress custom command in ...
I'm not going to accept this answer because there has to be a better way closer to the original question. function getData(successFn: (data: ......
Read more >How to create custom Commands in Cypress? - YouTube
cypress #cypressAutomation #cypressCommandsCypress comes with its own API for creating custom commands and overwriting existing commands.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
We also had problems with returning values from custom commands. It took us a while to find this thread about
cy.chain()
. Our commands are working now. @brian-mann could you please update the docs for thechain
method because I couldn’t find it in the docs. I would like to understand how it works specifically.Ah okay the reason this is happening is due to the way Cypress chaining works - there are rules about parent > child commands which ultimately resets the subject between new ‘chains’. It makes sense in the general API but when writing custom commands this is basically a hidden landmine.
You need to do this…