Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
77 views
in Technique[技术] by (71.8m points)

javascript - Function that allow me to create random email for cypress test

I'm looking into a function that allows me to create random emails where I would like to add it to the Email input inside of my test. For this reason, I created this function. However, I'm not sure how to add it to my cypress test.

Function:

 it('Product |  build or Remodel', () => {
    
    
    function string(){

    }
chars = 'abcdefghijklmnopqrstuvwxyz1234567890';
    string = '';
    email = '@aharotest.com';
    
    for(var ii=0; ii<15; ii++){
        string += chars[Math.floor(Math.random() * chars.length)];
    }
    
    console.log(string + email)
    
    cy.oneTime()
    cy.buildRemodel()
    cy.get('#full_name')
    .type('MOCKA DATA TEST')
    cy.get('#company')
    .type('Bluehost')
    cy.get('#phone_number')
    .type('2022569879')
    cy.get('#email')
    cy.get('#password')
    .type('Abcd1234')
    cy.logOut()
  })

My element is #email

What could be the best way to approach this situation.

question from:https://stackoverflow.com/questions/65895161/function-that-allow-me-to-create-random-email-for-cypress-test

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The solution to this issue is to create a function that will create random text + adding a string that will complete the email.

My solution is:

 function makeid(length) {
        var result           = '';
        var characters       = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
        var charactersLength = characters.length;
        for ( var i = 0; i < length; i++ ) {
           result += characters.charAt(Math.floor(Math.random() * charactersLength));
        }
        return result;
     }
     
     console.log(makeid(5));


My cypress command will be the following:

Cypress.Commands.add("form", ()=> {
      // fill-out form

      function makeid(length) {
        var result           = '';
        var characters       = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
        var charactersLength = characters.length;
        for ( var i = 0; i < length; i++ ) {
           result += characters.charAt(Math.floor(Math.random() * charactersLength));
        }
        return result;
     }
     
     console.log(makeid(5));

    cy.get('#full_name')
      .type('MOCKDATA TESTING')
    cy.get('#company')
      .type('Testing')
    cy.get('#phone_number')
      .type('2022569878')
    cy.get('#email')
      .type(makeid(6) + "@aharo.com")
    cy.get('#password')
      .type('Abcd1234')

    // click submit
    cy.get(".app-submit-btn-text").click()
 })

The best way to call this command is cy.form.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...