Generate Random Emails in Cypress
In Cypress, you can use JavaScript’s Math.random()
function and string concatenation to generate a random email. Here’s an example:
const randomEmail = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15) + "@example.com"
This will generate a random email with the format of randomstringrandomstring@example.com
, where the “randomstring” is generated by Math.random().toString(36).substring(2, 15)
which will return a random alphanumeric string of length between 2 and 15. You can then use this random email in your test:
cy.get('input[name="email"]').type(randomEmail)
You can also use other libraries like faker to generate random emails, for example:
const faker = require('faker') const randomEmail = faker.internet.email()
This will use the faker library to generate a random email. You can then use this random email in your test:
cy.get('input[name="email"]').type(randomEmail)
Note that if you use faker, you need to install it first by running npm install faker
For more Blogs , Click here
Author : Ghulam Nabi
Leave a comment