I wanna create 2 groups (Rose and Sunflower) in App based on the JSON file but the loop only runs 1 time
Here's my code
cy
.fixture('create-delete-group')
.then( (gr) => {
for (let i = 0; i < gr.length; i++) {
cy.window().then((win) => {
cy.stub(win, 'prompt').returns(gr[i].groupNames)
groupManaPage.elements.btnCreateGroup().click()
})
cy.on('window:alert', (str) => {
expect(str).to.equal('New group has been created successful!')
})
}
})
Here's json file
[
{
"groupNames": "Rose",
"members": [
"torido11",
"torido12"
]
},
{
"groupNames": "Sunflower",
"members": "torido13"
}
]
You need to .restore() the first stub in order to use it again, other than that the code is fine.
cy.fixture('create-delete-group').then(gr => {
for (let i = 0; i < gr.length; i++) {
cy.window().then((win) => {
const stub = cy.stub(win, 'prompt') // save stub reference
.returns(gr[i].groupNames)
groupManaPage.elements.btnCreateGroup().click()
cy.on('window:alert', (str) => {
expect(str).to.equal('New group has been created successful!')
stub.restore() // remove stub
})
})
}
})