测试枚举器.forEach()
在before()
挂钩之前运行,但是如果你确切知道需要处理多少个URL,就可以了。
在你引用的Cypress示例中,在中设置了url before()
。
let urls = [];
describe('Logo', () => {
before(() => {
cy.exec('npm run generator')
.then(() => { // writing the file will be async
cy.readFile("./urls.json").then(data => {
urls = data;
});
});
})
Cypress._.range(0, 2).forEach(index => { // only need the number of tests here
it(`Should display logo #${index}`, () => {
const url = urls[index] // runs after before()
cy.visit(url)
...
})
})
})
注意,url
不能再出现在测试描述中,但是索引可以。
如果url的数量未知,从技术上讲,仍然可以通过将测试枚举数设置为最大值来实现,但是日志中的结果是混乱的。未使用的测试插槽仍显示为通过。
基本问题是生成器脚本需要在赛普拉斯节点进程中运行,但规范在浏览器进程中运行。
但是浏览器和节点之间的ipc通信是通过cy.*
只能在回调中运行的异步命令完成的,该回调命令只能在执行阶段运行。
你最好从外部运行生成器脚本,例如
"scripts": {
"gen:test": "npm run generator & cypress open"
}
然后使用简单的方法require()
来收集数据
const data = require('./urls.json')
let urls = data.urls;
const testCount = data.urls.length;
describe('Logo', () => {
before(() => {
// not required
})
Cypress._.range(0, testCount).forEach(index => {
or
urls.forEach((url, index) => {