我有下面的 Input.json 作为夹具,它包含两个不同的测试用例。
Input.json(夹具文件夹)
[
{
"searchKeyword":"cypress"
},
{
"username":"QATesting",
"password":"testprofile"
}
]
上述数据将验证 Google 的两种不同功能。一个将验证搜索引擎,另一个将验证用户登录活动(这仅用于示例用例,可能会模仿我的实际需求)。
我刚刚创建了 cypress runner,我只想使用下面的 runner.js 文件运行规范文件
const cypress = require('cypress')
const fixtures = require('./cypress/fixtures/Test.json')
const promises = fixtures.map(fixture => {
return cypress.run({
env: {
fixture
},
spec: './cypress/integration/test.spec.js',
});
});
我刚刚在下面的“test.spec.js”文件中分别添加了两个不同的It(测试用例)。一项测试将执行搜索功能,另一项测试将检查现有用户登录活动:
describe("How to map two different data set with respective test function",() =>{
const baseUrl = "https://www.google.com/";
const testData = Cypress.env('fixture')
beforeEach("",()=>{
cy.visit(baseUrl);
});
it("Test Case1: Search the keyword", function () {
cy.xpath("//input[@name='q']").type(testData.searchKeyword);
cy.xpath("//input[@value='Google Search']").click();
cy.get("//ul/li[2]").should("be.visible");
});
it("Test Case2: login to the gmail account", function(){
cy.xpath("//a[contains(text(),'Sign in')]").click();
cy.xpath("//div[contains(text(),'Use another account')]").click();
cy.xpath("#identifierId").type(testData.username);
cy.xpath("//*[contains(text(),'Next')]").click();
cy.xpath("#password").type(testData.password);
cy.xpath("#submitbtn").click();
})
});
但是第二个测试失败了,testData.username 返回未定义。
无论如何在 test.spec.js 文件中将特定的 JSON 数组对象映射到特定的函数?
不确定如何将第一个数据集索引与第一个 It(测试用例 1)和第二个数据集索引分别映射到第二个测试用例。