Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cypress: custom timeout in specific should command

I need to have custom timeout in a specific should command in cypress. I have this json file which has global timeout:

{
  "viewportWidth": 1600,
  "defaultCommandTimeout": 10000
}

There is a specific case that I need a higher timeout, I would like something like this:

cy.get('body').should('contain','success', {timeout: 30000})

how do I do this? BTW, I do not want to override default command timeout, I need a specific timeout.

like image 981
Rogger Fernandes Avatar asked Jun 26 '19 18:06

Rogger Fernandes


2 Answers

tl;dr

Just pass the timeout to get, it will pass it down to should.

cy.get('body', {timeout: 30000}).should('contain','success')

Explanation

This is explained in should's official documentation in the Timeouts section:

.should() will continue to retry its specified assertions until it times out.

cy.get('input', { timeout: 10000 }).should('have.value', '10')
// timeout here will be passed down to the '.should()'
// and it will retry for up to 10 secs

The technique is explained in greater detail in the docs about timeouts.

like image 167
totymedli Avatar answered Oct 09 '22 00:10

totymedli


You probably want to move your {timeout: 30000} option to the parent command, like this:

cy.get('body', {timeout: 30000}).should('contain','success')

In this way the parent command's default assertions, and all subsequent assertions inherit this timeout overriding the default command timeout. Read more here: https://docs.cypress.io/guides/core-concepts/introduction-to-cypress.html#Timeouts

like image 3
Vangelisz Ketipisz Avatar answered Oct 08 '22 23:10

Vangelisz Ketipisz