skip to Main Content

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?

2

Answers


  1. 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() 
      }
    })
    
    Login or Signup to reply.
  2. 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)
      })
    })
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search