Skip to content

Instantly share code, notes, and snippets.

@jennifer-shehane
Last active April 8, 2020 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jennifer-shehane/35847772128616ff477e10de19299040 to your computer and use it in GitHub Desktop.
Save jennifer-shehane/35847772128616ff477e10de19299040 to your computer and use it in GitHub Desktop.
Get ID for later use using custom command
Cypress.Commands.add('getTaskId', (applicationId, retry) => {
function maybeRetryReq() {
return cy.request(({
url: `https://jsonplaceholder.cypress.io/users`
})).then((response) => {
const responseId = response.body[0].id
if (responseId) {
return responseId
}
if (retry) {
return maybeRetryReq()
}
throw new Error('Id not found')
})
}
return maybeRetryReq()
})
it('Get ID for later use', function () {
cy.getTaskId('123', 5).then((id) => {
// We have access to ID here
expect(id).to.eq(1)
})
// alias the ID for later use
cy.getTaskId('123').as('taskId')
// We have access to ID here
cy.get('@taskId').should('eq', 1).then(() => {
// We have access to ID here
cy.wrap(this.taskId).should('eq', 1)
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment