Tester Unknown
Tester Unknown

Reputation: 63

Actions before Retry on Cypress

I'm writing a test on Cypress and I want to clear the field values before retries. How should I do it?

it('Fill info', { retries: 3 },function () {
    cy
        .get('#name')
        .type('Test')

    cy.intercept('POST', '**/api/**/Find')
        .as('memberresponse')

    cy
        .get('.btn')
        .click()

    cy.wait('@memberresponse').then(xhr => {
        cy.log(JSON.stringify(xhr.response.body));
        cy.writeFile('***/memberinfo.json', xhr.response.body)
    })
})

I want to clear the field value before retry in case of request doesn't pass. What should I do?

Upvotes: 0

Views: 234

Answers (2)

Fody
Fody

Reputation: 31954

Is this what you are looking for?

cy.get('#name')
  .clear()
  .type('Test')

I presume clearing it every run (first or retry) is ok.

Otherwise you could include the page load (cy.visit) in the test.


Action before retry

I've used this pattern to handle repeating a before() hook when retry happens.

You may be able to run your actions there.

Cypress.on('test:after:run', (result) => {
  if (result.currentRetry < result.retries && result.state === 'failed') {
    cy.get('#name')
      .clear() 
  }
})

Upvotes: 1

Alapan Das
Alapan Das

Reputation: 18619

Just add clear before typing like this:

it('Fill info', {retries: 3}, function () {
  cy.get('#name').clear().type('Test')

  cy.intercept('POST', '**/api/**/Find').as('memberresponse')

  cy.get('.btn').click()

  cy.wait('@memberresponse').then((xhr) => {
    cy.log(JSON.stringify(xhr.response.body))
    cy.writeFile('***/memberinfo.json', xhr.response.body)
  })
})

Upvotes: 0

Related Questions