【问题标题】:How to access Cypress.env-variables from a helper-file? Or how to access files and fixtures in a readable way如何从帮助文件访问 Cypress.env 变量?或者如何以可读的方式访问文件和固定装置
【发布时间】:2022-12-09 14:12:36
【问题描述】:

我正在做几个项目,最终会有一个非常复杂的夹具结构。我通常会得到这样的结果:

cypress
  |- fixtures
     |- development
     |  |- tmp
     |  |  |- domain-hash.txt
     |  |
     |  |- secret
     |  |  |- admin-user.json
     |  |
     |  lang.json
     |
     |- staging
     |  |- tmp
     |  |  |- domain-hash.txt
     |  |
     |  |- secret
     |  |  |- admin-user.json
     |  |
     |  lang.json
     |
     |- production
     |  |- tmp
     |  |  |- domain-hash.txt
     |  |
     |  |- secret
     |  |  |- admin-user.json
     |  |
     |  lang.json

这样我就可以轻松地针对不同的环境运行测试,其中的固定装置通常会有所不同。

然后我将我的cypress.config.js设置为具有这样的变量:

  env: {
    fixtureFolder: 'development',
    tmpFolder: '/tmp',
    secretFolder: '/secret',
    domainHashFileName: "domain-hash.txt",
  }

但这意味着每当我必须指向一个文件或路径时,我都会得到这样的结果:

// This path to this domainHash is not easy to read nor understand/debug.
let domainHash = Cypress.env( 'fixtureFolder' ) + '/' + Cypress.env( 'tmpFolder' ) + Cypress.env( 'domainHashFileName' );

// I use that path like this:
cy.task( 'readFileMaybe', domainHash )
  .then( ( domainHash ) => {
    ...
    ...
  });

首要问题

我希望能够参考它需要加载/删除的文件/装置,尽可能短且易于阅读,这样代码就不会像上面所示那样臃肿,每次我必须访问装置或文件时。


解决方案尝试1:在cypress.config.js中定义一堆不同的路径

我试过在cypress.config.js中加入这些行:

domainHashFileName: "domain-hash.txt",
domainHashFromFixture: "development/tmp/domain-hash.txt",
domainHashFromRoot: "cypress/fixtures/development/tmp/domain-hash.txt",

但这意味着我必须为每个变量定义三个变量。不理想。


解决方案尝试 2:将其添加为赛普拉斯命令

由于这些赛普拉斯函数的异步性质,那么我将不得不chain the hell out of this,这只会将我的问题转移到其他地方。

【问题讨论】:

  • AFAIk Cypress.env() 应该可以在那里访问。我不完全确定你为什么得到 NotANumber (NaN),但是对于解决方案尝试 1,你是否尝试过使用带有反引号的字符串插值?像`cypress/fixtures/${Cypress.env('fixtureFolder')}/${folderName}`之类的东西
  • 谢谢@agoff。我一定是打错了字什么的。我现在开始工作了,所以我将该解决方案尝试移到了答案中。

标签: cypress


【解决方案1】:

解决方案是创建一个辅助函数:cypress/support/utils/helpers.js

内容:

export const getFullEnvFolderPath = (folderName) => {
  return 'cypress/fixtures/' + Cypress.env('fixtureFolder') + '/' + folderName;
}

export const getEnvFolderPath = (folderName) => {
  return Cypress.env('fixtureFolder') + '/' + folderName;
}

// ... with several other functions.

在我的 e2e 测试中,我可以这样做:

import { getFullEnvFolderPath } from "../../support/utils/helpers";

...
...

let domainHash = getFullEnvFolderPath( Cypress.env( 'domainHashFileName' ) );
cy.task( 'readFileMaybe', domainHash )
  .then( ( domainHash ) => {
    ...
    ...
  });

【讨论】:

    【解决方案2】:

    看起来夹具之间的主要区别是基于测试运行环境的根文件夹名称。我不确定为什么你有名为tmpFoldersecretFolder的环境变量映射到/tmp和@的值987654325@ 看起来他们永远不会改变。

    因此,基于此,我有两个建议:

    1. 为运行测试的环境(生产、暂存、开发)设置一个 env var,然后将字符串插入目录路径
    2. 使用cy.fixture()读取夹具文件
      // cypress.config.js
      fixturesFolder: 'cypress/fixtures',
      env: {
        system: 'production' // set this depending on environment
      }
      
      // Test code
      const env = Cypress.env('system')
      cy.fixture(`${env}/temp/domain-hash.txt`).then(domainHash => /* code */ );
      cy.fixture(`${env}/secret/admin-user.json`).then(adminUser => /* code */ );
      cy.fixture(`${env}/lang.json`).then(lang => /* code */ );
      

      https://docs.cypress.io/api/commands/fixture

    【讨论】:

      猜你喜欢
      • 2017-05-08
      • 1970-01-01
      • 2018-11-16
      • 2015-05-06
      • 2015-07-22
      • 1970-01-01
      • 2019-10-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多