【问题标题】:Cypress environment variable undefined赛普拉斯环境变量未定义
【发布时间】:2022-11-07 21:59:02
【问题描述】:

在 cypress.json 文件中,我有以下代码

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

  "env": { "email": "test@email.com", "password": "password" }
}

当我试图通过调用 Cypress.env('password') 来访问它时,它在打印时在控制台日志中显示未定义,这是什么问题。

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

【问题讨论】:

  • 为什么不改用console.log(password)
  • 对我来说 Cypress.env('email') 会返回 test@email.com。您的 cypress.json 在哪里?它应该在顶部的 cypress 文件夹中,而不是底部的文件夹中
  • @PhilipAllStar 是的,文件被放置在错误的目录中

标签: javascript node.js automation cypress


【解决方案1】:

我不知道或不检查 cypress.json 文件位置的错误,将其移动到顶部 cypress 文件夹并正确显示值。

【讨论】:

  • 请将此答案标记为解决方案,因为现在此问题仍显示为未解决。
  • 非常感谢 Artjom Prozorov 的回答? 上帝保佑你!
【解决方案2】:

赛普拉斯 V10 的更新。

扩展@Artjom Prozorov 的答案,

现在在较新的版本中,cypress.json 命名约定已被弃用。 因此,我们必须使用cypress.config.ts 作为配置文件名。

下面给出的文件内容示例。

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

注意:这个 cypress.config.ts 必须是里面柏树目录。

【讨论】:

    【解决方案3】:

    在我的 Projekt(版本 10.xx)中,cypress.config.ts 必须在根路径中,而不是在 cypress 文件夹中。您可以使用 UI 生成配置,以将其放在正确的位置: 设置 > 项目设置 > cypress.config.ts

    【讨论】:

      猜你喜欢
      • 2022-07-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-17
      • 2020-10-09
      • 2023-01-16
      • 2017-01-28
      • 1970-01-01
      相关资源
      最近更新 更多