Sumit Soni
Sumit Soni

Reputation: 39

How to pass value of same variable to another function's variable in cypress

I have a requirement that i need to store user email into one variable and then need to use same email to search a user created with same email in another method in cypress how can i do that?

below is my method

class UserManager {
    CreatePrimaryuser() {
        const button = cy.get(':nth-child(2) > a > h3')
        button.click()
        const button1 = cy.get('.btn-edit-user-view')
        button1.click()
        const uemail = cy.get('#emailField')
        var currentDate = new Date();
       var currentTime = currentDate.getTime();
       var commentText = 'Automation' + currentTime + '@email.com';
        uemail.type(commentText)
}
SearchUser() {
        cy.get('#searchUsers').click().type(commentText)

    }

i want to use same value of commontext in searchuser method that is stored in CreatePrimaryuser method

Upvotes: 0

Views: 257

Answers (2)

Verònica Jandrew
Verònica Jandrew

Reputation: 462

You can also save it in an env variable.

class UserManager {
  CreatePrimaryuser() {
    const button = cy.get(':nth-child(2) > a > h3')
    button.click()
    const button1 = cy.get('.btn-edit-user-view')
    button1.click()
    const uemail = cy.get('#emailField')
    var currentDate = new Date();
    var currentTime = currentDate.getTime();
    var commentText = 'Automation' + currentTime + '@email.com';
    uemail.type(commentText)
    Cypress.env('commentText', commentText);
  }

  SearchUser() {
    cy.get('#searchUsers').click().type(Cypress.env('commentText'));
  }
}

Upvotes: 3

Mikhail Bolotov
Mikhail Bolotov

Reputation: 1104

You can just store the value in a field:

class UserManager {
  commentText
  
  constructor() {
    var currentDate = new Date();
    var currentTime = currentDate.getTime();
    this.commentText = 'Automation' + currentTime + '@email.com';
  }

  CreatePrimaryuser() {
    const button = cy.get(':nth-child(2) > a > h3')
    button.click()
    const button1 = cy.get('.btn-edit-user-view')
    button1.click()
    const uemail = cy.get('#emailField')
    
    uemail.type(this.commentText)
  }

  SearchUser() {
    cy.get('#searchUsers').click().type(this.commentText)

  }
}

Upvotes: 1

Related Questions