How to increment environment variable in 5

Viewed 87

I want to use, each time i'm running the test suit, a different set of parameters:

{
  "Company": {
    "Name": "CompnayName1040",
    "FullName": "FullName1040",
    "phone": "0505551040"}
}

Meaning after each test run i'm running last test that will increment by 5 the above variables

When running this code the result phone number is: 05055510405

cy.readFile('cypress.env.json', ).then( (envVars) => {
cy.log('Old cypress.env.json.Employee.phone: '+envVars.Employee.phone);
envVars.Employee.phone = String(envVars.Employee.phone).replace(/(\d{4})$/, function(n){ return n+=5 });
cy.log('New cypress.env.json.Employee.phone: '+envVars.Employee.phone);
}

i was expecting the phone number to be 0505551045

2 Answers

If you are looking to use unique strings, you can also use the lodash, which is bundled with Cypress, function .random(). Example

Name = `CompanyName${Cypress._.random(9999)}` // random number between 0 and 9999

To bump the phone number, work with the last 7 digits.

This will preserve your leading "0".

const head = envVars.Employee.phone.substr(0,3)
const tail = envVars.Employee.phone.substr(3)
const bumped = +tail+5
envVars.Employee.phone = `${head}${bumped}`

Using @jjhelguero's idea for random numbers, you might like to try use fakerjs phoneNumber

envVars.Employee.phone = faker.phone.phoneNumber('05055510##')
Related