Artjom Prozorov
Artjom Prozorov

Reputation: 307

Cypress environment variable undefined

In cypress.json file i have the following code

{
  "baseUrl": "test",
  "ignoreTestFiles": [],
  "viewportHeight": 768,
  "viewportWidth": 1024,
  "video": false,

  "env": { "email": "[email protected]", "password": "password" }
}

When i am trying to access it by calling Cypress.env('password') it shows undefined in console log when printing it, what is the issues.

const password: string = Cypress.env('password')

describe("Login to the application", () => {
  beforeEach(() => {
    cy.visit("/");
  });

  it.only("user should login successfully", () => {
    console.log(Cypress.env('email')).   --- undefined 
    loginPage.login(email, password);
    cy.url().should("include", "/wallet");
  });

Upvotes: 8

Views: 7713

Answers (3)

Abhishek Gautam
Abhishek Gautam

Reputation: 1767

UPDATE for CYPRESS V10.

Extending @Artjom Prozorov's answer,

we have to use cypress.config.(js|ts) as the file name for configuration.

sample of file content is given below.

import { defineConfig } from "cypress";

export default defineConfig({
  e2e: {
    specPattern: "src/**/*.cy.{js,jsx,ts,tsx}",
    baseUrl: "http://localhost:3001",
    trashAssetsBeforeRuns: false,
    viewportWidth:1920,
    viewportHeight:1080,
    slowTestThreshold: 1000,
    // watchForFileChanges : false,
    env: {
      apiUrl : "http://localhost:3000",
      commandDelay: 100,
      password: 'here it is'
    },
    reporter: 'mochawesome',
    reporterOptions: {
      reportDir: 'cypress/reports',
      overwrite: false,
      html: true,
      json: false
    },
    setupNodeEvents(on, config) {
      config.env.sharedSecret =
        process.env.NODE_ENV === 'development' ? 'itsDev' : 'itsLocal'

      return config
    }
  },

  component: {
    devServer: {
      framework: "create-react-app",
      bundler: "webpack"
    }
  }
});

NOTE : this cypress.config.ts must be inside the cypress directory.

enter image description here

Upvotes: -2

Gerrit
Gerrit

Reputation: 1

In my Projekt (Version 10.xx) the cypress.config.ts must be in the root path not in the cypress folder. You can generate the config with the UI, to get it on the right location: Settings > Project settings > cypress.config.ts

Upvotes: 0

Artjom Prozorov
Artjom Prozorov

Reputation: 307

My mistake for not knowing or not checking the location of my cypress.json file, moved it to the top cypress folder and value is shown properly.

Upvotes: 5

Related Questions