Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

152
Views
Where can i define common variables used in all test cases for cypress -

I have a form which i am filling and every single test case takes different values for firstname, lastname and email. So i am creating the env variable values within each test case as the value changes each time rather than just one value in cypress.json is there a way i can move the const variables i declared below to a section within cypress, can i move it to the hooks section??

  //Second Test Case with Broker Company
  it('demotestnifty1', {
    env: {
        firstname: 'Lambda',
        lastname: 'Jonas',
        email: 'ljrfnew@gemini.com',
        }
    }, function () {
    const firstname = Cypress.env('firstname')
    const email = Cypress.env('email')
    const lastname = Cypress.env('lastname')
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You could define them at the describe() or context() level, if the data will be the same for all tests in those blocks. This is similar to what you've already done in your it() block.

describe('some test', { env: { foo: 'bar' } }, () => {
  it('logs foos value', () => {
    cy.log(Cypress.env('foo'); // logs 'bar'.
  });
});

You could also store them in your cypress.json or cypress.env.json files.

// cypress.json
{
  ...
  'env': {
    'foo': 'bar'
  }
  ...
}

// cypress.env.json
{
  'foo': 'bar'
}

Cypress will pick up variables included in cypress.json or cypress.env.json automatically, so no additional setup would be required. Note: Environment variables stored in cypress.env.json will override any set in cypress.json.

Either way, after defining the variables at a higher scope than the it() block, you can set them in the before() or beforeEach() of your block.

// assumes value is set in cypress.json
describe('some tests', () => {
  let foo: string;
  
  before(() => {
    foo = Cypress.env('foo');
  });

  it('logs foo value', () => {
    cy.log(foo); // logs 'bar'
  }); 
});

EDIT: After discussion, it seems like the actual question being asked is how the data can be set and easily used. I think the easiest way would just be to set the data in the beforeEach().

describe('some test', () => {
  let foo: string;
  beforeEach(() => {
    foo = Cypress.env('foo');
  })

  it('logs bar', { env: { foo: 'bar' } }, () => {
    cy.log(foo); // logs 'bar'
  });

  it('logs baz', { env: { foo: 'baz' } }, () => {
    cy.log(foo); // logs 'baz'
  });
});

It may also be valuable to set a default value, in case you have a use case that you're using > 50% of the time.

...
let foo: string;
beforeEach(() => {
  foo = Cypress.env('foo') ?? 'defaultValue'
});
...
about 4 years ago · Juan Pablo Isaza Report

0

Make an array of users and index it

// cypress.json
{
  "env": {
    "users": [
      {firstname: 'Lambda', lastname: 'Jonas', email: 'ljrfnew@gemini.com'},
      ...
    ]
  }
}

// test
it('tests with first user', () => {
  const firstname = Cypress.env('users')[0].firstname
  const lastname =  Cypress.env('users')[0].lastname
  ...
})
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!