最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Cypress environment variable undefined - Stack Overflow

programmeradmin3浏览0评论

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");
  });

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");
  });
Share Improve this question asked May 12, 2022 at 9:02 Artjom ProzorovArtjom Prozorov 3274 silver badges10 bronze badges 3
  • Why not use console.log(password) instead? – evolutionxbox Commented May 12, 2022 at 9:07
  • 5 For me Cypress.env('email') gives back [email protected]. Where is your cypress.json located? It should be in the top cypress folder, not the bottom one – PhilipAllStar Commented May 12, 2022 at 13:01
  • @PhilipAllStar yeh that was the case the file was placed in wrong directory – Artjom Prozorov Commented May 13, 2022 at 7:18
Add a ment  | 

3 Answers 3

Reset to default 5

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.

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

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",
      mandDelay: 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
    }
  },

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

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

发布评论

评论列表(0)

  1. 暂无评论