Cypress 当JSON fixture文件中有多个记录时,如何将cy.fixture与数组一起使用

Cypress 当JSON fixture文件中有多个记录时,如何将cy.fixture与数组一起使用,cypress,Cypress,\在中创建了一个Cypress Testscript,并设置为在循环中执行,就像希望在fixture json中使用多组testdata执行一样。如果您关闭并看到cy.fixture仅适用于第一个条目,并使用json中的first条目执行两次测试(testdata文件)。任何关于如何将数组的迭代与fixture一起包括在内的帮助都会很有帮助 var Launchindex = 0; for (Launchindex = 0; Launchindex < 2; Launchindex ++

\在中创建了一个Cypress Testscript,并设置为在循环中执行,就像希望在fixture json中使用多组testdata执行一样。如果您关闭并看到cy.fixture仅适用于第一个条目,并使用json中的first条目执行两次测试(testdata文件)。任何关于如何将数组的迭代与fixture一起包括在内的帮助都会很有帮助

var Launchindex = 0;
for (Launchindex = 0; Launchindex  < 2; Launchindex ++) {

describe('Launch  testsite',() => {

    it('try login using testdata', () => {

        cy.visit('https://xyzz')
        cy.title().should('contain','title check')
    })

    it('check url', () => {

        cy.url().should('contain','xyz')

    })

    it('enter details and submit', () => { 
        //Fixture loads the testdata setup in fixtures folder , so setup testdata required before executing test script   



        cy.fixture('testdata').then(testdata  => {

            const ModuleID = testdata[0].ModuleID 
            const LoginName = testdata[0].LoginName
            const gameid = testdata[0].gameid

        cy.get('#ModuleID').type(ModuleID)
        cy.get('#LoginName').type(LoginName)
        cy.get('#gameid').type(gameid)
        cy.get('#btnSubmit').click()

        })
    })

})

}

fixture file looks somethings like this 
[
{"id": 0,"ModuleID": "xxxx","LoginName": "xxxx","gameid": "xxxx"},
{"id": 1,"ModuleID": "yyy","LoginName": "yyy","gameid": "yyyy"}
]
var Launchindex=0;
对于(Launchindex=0;Launchindex<2;Launchindex++){
描述('启动测试站点',()=>{
它('尝试使用testdata登录',()=>{
参观https://xyzz')
cy.title()应('contain','title check'))
})
它('检查url',()=>{
cy.url()应('contain','xyz'))
})
它('输入详细信息并提交',()=>{
//Fixture在fixtures文件夹中加载testdata设置,因此在执行测试脚本之前需要设置testdata
cy.fixture('testdata')。然后(testdata=>{
常量ModuleID=testdata[0]。ModuleID
const LoginName=testdata[0]。LoginName
const gameid=testdata[0]。gameid
cy.get('#ModuleID').type(ModuleID)
cy.get('#LoginName').type(LoginName)
cy.get('#gameid').type(gameid)
cy.get('#btnSubmit')。单击()
})
})
})
}
fixture文件如下所示
[
{“id”:0,“ModuleID”:“xxxx”,“LoginName”:“xxxx”,“gameid”:“xxxx”},
{“id”:1,“ModuleID”:“yyy”,“LoginName”:“yyy”,“gameid”:“yyy”}
]
cy.fixture('testdata')
将在Cypress执行它时进行计算,因此模块顶层的循环将不起作用

您可以这样做:

description('Launch testsite',()=>{
它('输入详细信息并提交',()=>{
cy.fixture('testdata')。然后(testdata=>{
testdata.forEach(数据=>{
const ModuleID=data.ModuleID;
const LoginName=data.LoginName;
const gameid=data.gameid;
cy.get('#ModuleID').type(ModuleID);
cy.get(“#LoginName”).type(LoginName);
cy.get('#gameid').type(gameid);
cy.get('#btnSubmit')。单击();
//在实际测试中,您可能需要在此处执行某种断言
});
});
});
});

谢谢@miklos\u me,但仍然无法读取来自JSON@CypressAuto您想为该装置中的每个条目生成单独的测试用例吗?希望有单个测试,并且能够从json/任何其他格式中一个接一个地读取条目并执行..如何使其工作的任何建议ForEach()为我工作,而不是使用cy.fixture。。const testData=require(“../fixtures/testData.json”);这确实管用!非常感谢!在描述之前声明了以下内容:
const testData=require(../../fixtures/multipleInputFields.json”)
然后
testData.forEach((数据)=>{const message=data.message it('testcase1',function(){cy.log(“数据是:“+data”)cy.get('#用户消息')。类型(消息)。应该('have.value',message cy.get('get input>button')).click()cy.wait(200)cy.get('span#display').should('have.text',message)})