Last active
April 8, 2020 14:17
-
-
Save jennifer-shehane/35847772128616ff477e10de19299040 to your computer and use it in GitHub Desktop.
Get ID for later use using custom command
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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