Stop cy.each Iteration When Using Cypress Commands Inside The Callback Function

Опубликовано: 16 Март 2022
на канале: gleb bahmutov
3,207
38

If the cy.each callback uses Cypress commands and we want to stop the iteration based on the output of those commands, we need to use cy.then + a local closure variable to stop queueing up Cypress commands. The final solution from https://github.com/bahmutov/better-cy... is this:

cy.visit('index.html')
let shouldStop = false
cy.get('tbody button').each(function ($button, k) {
cy.then(function () {
if (shouldStop) {
return
}
console.log('button', k)
cy.wrap($button)
.click()
.parent()
.parent()
.contains('td', /\d/)
.invoke('text')
.then(Number)
.then(function (n) {
if (n === 7) {
shouldStop = true
}
})
})
})

Tip: to visualize the Cypress command chain this video uses https://github.com/bahmutov/cypress-c... plugin and read the blog post https://glebbahmutov.com/blog/better-...


Смотрите видео Stop cy.each Iteration When Using Cypress Commands Inside The Callback Function онлайн, длительностью часов минут секунд в хорошем качестве, которое загружено на канал gleb bahmutov 16 Март 2022. Делитесь ссылкой на видео в социальных сетях, чтобы ваши подписчики и друзья так же посмотрели это видео. Данный видеоклип посмотрели 3,207 раз и оно понравилось 38 посетителям.