Moataz Selim
Moataz Selim

Reputation: 19

I need a way to increment a value in cypress code

I'm trying to automate a sign up scenario. In sign up form, I have an email address field which is unique.

I'm using this code:

cy.get('#e_mail',{timeout:10000}).type('[email protected]')

In next run am change the value in the email to be +2 and so on in each run.

Is there a way to make the value increment automatically in each run?

Upvotes: 1

Views: 925

Answers (3)

Evgenii Bazhanov
Evgenii Bazhanov

Reputation: 926

you can get uniq number by using new Date().getTime(); it will return uniq number every time with reference to the current Date

const timestamp = new Date().getTime(); //1642080371822

cy.get('#e_mail').type(`mpataz.tsc+${timestamp}@gmail.com`)

instead of hardcoded timeout you can extract it and add to global defaultCommandTimeout inside cypress.json

  "defaultCommandTimeout": 10000,

Upvotes: 3

jjhelguero
jjhelguero

Reputation: 2555

If you want to make your emails unique, I'd suggest doing using a timestamp with Cypress._.random() to generate the email each time.

const timestamp = String(+new Date()).slice(5)
const email = `test-email+m${timestamp}_${Cypress._.random(5)}@email.com`

Upvotes: 0

Alapan Das
Alapan Das

Reputation: 18626

You can use faker js library to generate unique email id's.

After installing the faker js npm package, At the top of the spec file write:

const faker = require('faker')

Then in your tests write:

cy.get('#e_mail',{timeout:10000}).type(faker.internet.email())

Upvotes: 1

Related Questions