gpt4 book ai didi

cypress - 当 JSON fixture 文件中有多个记录时如何使用 cy.fixture 和 Array

转载 作者:行者123 更新时间:2023-12-04 02:36:33 28 4
gpt4 key购买 nike

\Created a Cypress Testscript in and set for execution in loop as would like to execute with multiple set of testdata in fixture json.if you down and see the cy.fixture is works only for first entry and test executed twice 执行两次与 fisrt json 中的条目(测试数据文件).anyhelp 关于如何包含数组的迭代以及夹具会很有帮助

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"}
]

最佳答案

cy.fixture('testdata') 将在 Cypress 执行时进行评估,因此模块顶层的循环将不起作用。

您可以这样做:

describe('Launch testsite', () => {
it('enter details and submit', () => {
cy.fixture('testdata').then(testdata => {
testdata.forEach(data => {
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').click();

// in a real test you probably need to do some kind of assertion here
});
});
});
});

关于cypress - 当 JSON fixture 文件中有多个记录时如何使用 cy.fixture 和 Array,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61593100/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com