最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin5浏览0评论

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';
    
    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('#pany')
    .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.

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.';
    
    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('#pany')
    .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.

Share Improve this question edited Jan 26, 2021 at 3:15 Hvaandres asked Jan 26, 2021 at 2:34 HvaandresHvaandres 1,0154 gold badges22 silver badges46 bronze badges 3
  • I think you could just use UUIDs for email usernames btw. Might make things easier. – Dennis Hackethal Commented Jan 26, 2021 at 2:56
  • I wish I can change the logic of my System, but I have to run a test like that.... – Hvaandres Commented Jan 26, 2021 at 3:05
  • I wasn't menting on your need to test, so it has no bearing on that. – Dennis Hackethal Commented Jan 26, 2021 at 3:20
Add a ment  | 

2 Answers 2

Reset to default 3

I think most of the solutions here are needlessly plex.

This should be enough:

cy.get('#input_email').type(`${Date.now()}@aharotest.`)

No need for any 'helper' function ${Date.now()} will output the current unix time (e.g. 1674214877166)

This will be different every time you run the test, so the email will always be unique.

The solution to this issue is to create a function that will create random text + adding a string that will plete 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 mand 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('#pany')
      .type('Testing')
    cy.get('#phone_number')
      .type('2022569878')
    cy.get('#email')
      .type(makeid(6) + "@aharo.")
    cy.get('#password')
      .type('Abcd1234')

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

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

发布评论

评论列表(0)

  1. 暂无评论