通过 cypress.env 传递数据库凭据时未定义 Cypress

问题描述 投票:0回答:1

我正在尝试连接到 oracledb 并且它可以工作,但我现在尝试在 Cypress.env() 上传递凭据,但它不起作用。看来我不能像这样跳过环境变量:

帮助文件 /helpers/db.js 包含以下内容:

async function getConnection() {
  return oracledb.getConnection({
    user: Cypress.env("DB_USERNAME"),
    password: Cypress.env("DB_PASSWORD"),
    connectString: Cypress.env("DB_CONNECTION_STRING")
  });
}

在 cypress.env.json 中,这些设置如下。

{
  "DB_USERNAME": "THE-USERNAME",
  "DB_PASSWORD": "THE-PASSWORD",
  "DB_CONNECTION_STRING": "something.com/blabla.com"
}

测试仅运行 cy.task() 来执行 db.js 文件内的代码。

如何在

.getConnection()
函数中引用这些环境变量?

authentication cypress
1个回答
5
投票

根据您调用该函数的方式,您需要传入配置对象,而不是使用

Cypress.env
使用
config.env

cypress.config.js - 启动

import { defineConfig } from "cypress";
import { oracledb } from ...

async function getConnection(config) {
  return oracledb.getConnection({
    user: config.env("DB_USERNAME"),
    password: config.env("DB_PASSWORD"),
    connectString: config.env("DB_CONNECTION_STRING")
  });
}

module.exports = defineConfig({
  e2e: {
    setupNodeEvents(on, config) {
      getConnection(config)
    },
  },
})

cypress.config.js - 任务

import { defineConfig } from "cypress";
import { oracledb } from ...

async function getConnection(config) {
  return oracledb.getConnection({
    user: config.env("DB_USERNAME"),
    password: config.env("DB_PASSWORD"),
    connectString: config.env("DB_CONNECTION_STRING")
  });
}

module.exports = defineConfig({
  e2e: {
    setupNodeEvents(on, config) {
      on('task', {
        getConnection(config)
      }
    },
  },
})
© www.soinside.com 2019 - 2024. All rights reserved.