【问题标题】:Adding It function to Cypress fixture将 It 函数添加到 Cypress 夹具
【发布时间】:2022-12-09 13:54:01
【问题描述】:
我想我在这里错过了重点。该夹具不能在测试之外使用,但我想使用“it”函数来测试 JSON 文件中的每个夹具。
我不确定我是否正确理解 fixtures 或正确使用它们。这是我的赛普拉斯 TypeScript 代码:
let row: string;
let stream: string;
cy.fixture('AllNotificationStates').then((notifications) => {
for (let notification in notifications) {
row = notifications[notification].rowId;
stream = notifications[notification].streamId;
it(`${row}`, () => {
cy.intercept('GET', '**/api/', (req) => {
req.reply((req) => {
req.statusCode = 200;
req.body = [notifications[notification]];
});
});
cy.visit('/notification/dashboard');
co.setTestId(row);
co.assertRowIDandStreamID(row, stream);
});
}
});
【问题讨论】:
标签:
typescript
automated-tests
cypress
fixtures
【解决方案1】:
你应该使用cypress-each will运行所有测试,不管一个或多个是否失败.根据需要多长时间,您可能希望将 it.each 测试分解到另一个文件中。
import 'cypress-each' // can included in /support/index.js
import allNotifications from 'notificaitonsPath'
describe('Disclaimer check', () => {
it.each(allNotifications)
((notify) => notify.rowId, // use function to print out row id for test title
(notification) => {
// test code
})
})
【解决方案2】:
如果在 it 块之前使用它,则不能使用 cy.fixture。相反,您可以直接导入它,然后像这样使用它。
import notifications from '../fixtures/AllNotificationStates.json'
for (let notification in notifications) {
let row = notifications[notification].rowId
let stream = notifications[notification].streamId
it(`${row}`, () => {
cy.intercept('GET', '**/api/', (req) => {
req.reply((req) => {
req.statusCode = 200
req.body = [notifications[notification]]
})
})
cy.visit('/notification/dashboard')
co.setTestId(row)
co.assertRowIDandStreamID(row, stream)
})
}