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?
CodePudding user response:
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()
}
})
CodePudding user response:
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)
})
})