Reputation: 1005
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.
Upvotes: 1
Views: 6135
Reputation: 288
I think most of the solutions here are needlessly complex.
This should be enough:
cy.get('#input_email').type(`${Date.now()}@aharotest.com`)
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.
Upvotes: 3
Reputation: 1005
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
.
Upvotes: 3