D
D
dozzzzer2020-12-26 16:45:59
JavaScript
dozzzzer, 2020-12-26 16:45:59

Swapping variables between Cypress custom commands - how to implement?

There are two custom commands in commands.js
The first one makes a request, parses the result, writes the name and email to the file.
The second one initializes the registration form for the email received by the first command.
What is the best way to implement the transfer of the value of the email received as a result of the request by the first command, for subsequent use by the second command in the best way?

Cypress.Commands.add("getMockData", function() {
    cy.request('https://api.mockaroo.com/api/b2e4f1f0?count=1&key=46aaa0a0').its('body.0').as('body')
    .then((user) => {
        cy.writeFile('cypress/fixtures/user.json',
        { "firstName": user.first_name, "lastName": user.last_name, "email": user.email }, {log: 'true'})
        cy.wrap(user.email).as('email')
    })
})

Cypress.Commands.add("initRegForm", function() {
    reg.getNewEmailField().clear()
    reg.getNewEmailField().type(this.email) // Cannot read property 'email' of undefined
 
    reg.createNewAccountButton().click()
})

I would also be grateful for an explanation why when I try to access this.emailwe get an error ( Cannot read property 'email' of undefined ), although at the same time, if I open the console in debug mode before this line, it this.emailreturns me the correct value obtained from the previous commands.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question