Reputation: 547
I have a custom command where I want to override the default timeout of 4 seconds. I tried setting the timeout in the command's code and in the tests that use it but with no success yet.
Cypress.Commands.add("login", () => {
Auth.signIn(username, password)
.then(() => {
console.log('Programmatically logged in as ' + username);
})
.catch(err => console.log('Error when login in: ', err));
})
I attempted the following:
Cypress.Commands.add("login", { timeout : 10000} () => {...}
and
Cypress.Commands.add("login", () => {
Auth.signIn(username, password)
.then(({ timeout : 10000}) => {
console.log('Programmatically logged in as ' + username);
})
.catch(err => console.log('Error when login in: ', err));
})
EDIT/UPDATE
I added async/await to the Auth.signIn()
call and that makes the execution wait for that method to finish, but if it reaches the 4 seconds default timeout it makes the test fail.
The best I could do was include all the test code inside the then
of the login
command.
it('Test that requires login', () => {
cy.login().then(function (){
cy.visit('/');
//...
});
})
Although the test waits for the login operation to finish, it fails if the timeout is reached before it is logged in.
Upvotes: 2
Views: 3099
Reputation: 6312
I'm not sure about timeout
in Cypress's custom commands, but you could use a workaround by passing a default param into your custom command like so:
Cypress.Commands.add("login", (waitTimer = 1000) => {
Auth.signIn(username, password)
.then(() => {
cy.wait(waitTimer);
console.log('Programmatically logged in as ' + username);
})
.catch(err => console.log('Error when login in: ', err));
})
Note: Beware that passing timeouts
or arbitrarily waiting for an x
amount of seconds is not considered good practice. You should instead check for the response's status
& assert it returns a 200
status or use route aliases.
Example:
......
.then(response => {
if (response.status !== 200) {
cy.log('An error happened while logging in');
}
Upvotes: 2