【发布时间】: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