如何从命令行使用多个 data.json 文件而不提醒代码 - cypress

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

我在 fixtures 中有两个数据文件(dataUAT.json 和 dataQA.json),我在其中获取规范文件的数据。假设我有不同的环境,例如 UAT 和 QA。 我想将 dataUAT.json 文件用于 UAT 环境,将 dataQA.json 文件用于 QA 环境。 有没有什么方法可以在不改变代码的情况下兼顾它们?就像直接从命令行更改它吗?

cypress fixtures
2个回答
1
投票

您可以从命令行传入环境变量,而不是修改插件:

cypress run --env fixtureEnvironment=UAT
cypress run --env fixtureEnvironment=QA

您可以在查找夹具时引用此环境变量。

cy.fixture(
  `data${Cypress.env('fixtureEnvironment') ?? 'QA'}`)
.then(...);

在上面,我包含了一个空合并运算符来检查正在设置的变量,如果没有,则默认为 QA。


0
投票

Cypress docs 有一个示例,我也有一个 repo 也展示了这个和 cypress-grep。

基本上,您需要通过添加以下内容来对

plugins/index.js
进行一些调整:

/ promisified fs module
const fs = require('fs-extra')
const path = require('path')

function getConfigurationByFile(file) {
  const pathToConfigFile = path.resolve('..', 'config', `${file}.json`)

  return fs.readJson(pathToConfigFile)
}

// plugins file
module.exports = (on, config) => {
  // accept a configFile value or use development by default
  const file = config.env.configFile || 'development'

  return getConfigurationByFile(file)
}
© www.soinside.com 2019 - 2024. All rights reserved.