I have a list of options/buttons which I need to be sure they're all set to a specific value. Every wrapper can have several buttons but the first one is always what I want set before my tests are run.
Therefore I need loop these wrappers and target the first child / button of each of them.
Typically this would be a case for each() but Cypress errors after the first click - the DOM re-renders and Cypress can't find the remaining buttons.
Therefore I need an alternative solution. One of them would be a classic for loop. Here's the code:
<div>
<div class="ab-test-switch__experiment">
<div class="ab-test-switch__buttons"><button type="button" class="button ab-test-switch__button button--primary button--lg" data-variant="49_a">
49_a
</button><button type="button" class="button ab-test-switch__button button--secondary button--lg" data-variant="49_b">
49_b
</button>
</div>
</div>
<div class="ab-test-switch__experiment">
<div class="ab-test-switch__experiment__title">
<div class="v-popover">
<div class="trigger" style="display: inline-block;">
<p data-v-d7151c42="">(detail)</p>
</div>
</div>
</div>
<div class="ab-test-switch__buttons"><button data-v-7fea8896="" data-v-d7151c42="" type="button" class="button ab-test-switch__button button--secondary button--lg" data-variant="old_business_section" data-v-5c4d7450="">
old_business_section
</button><button data-v-7fea8896="" data-v-d7151c42="" type="button" class="button ab-test-switch__button button--primary button--lg" data-variant="new_business_section" data-v-5c4d7450="">
new_business_section
</button>
</div>
</div>
<div class="ab-test-switch__experiment">
<div class="ab-test-switch__experiment__title">
<h2 data-v-d7151c42="" data-v-5c4d7450="">53_mobile_banner</h2>
<div data-v-d7151c42="" class="v-popover" data-v-5c4d7450="">
<div class="trigger" style="display: inline-block;">
<p data-v-d7151c42="">(detail)</p>
</div>
</div>
</div>
<div class="ab-test-switch__buttons"><button type="button" class="button ab-test-switch__button button--secondary button--lg" data-variant="none" data-v-5c4d7450="">
none
</button><button type="button" class="button ab-test-switch__button button--primary button--lg" data-variant="mobile_banner" data-v-5c4d7450="">
mobile_banner
</button>
</div>
</div>
<div class="ab-test-switch__experiment">
<div class="ab-test-switch__experiment__title">
<div class="v-popover">
<div class="trigger" style="display: inline-block;">
<p>(detail)</p>
</div>
</div>
</div>
<div class="ab-test-switch__buttons"><button type="button" class="button ab-test-switch__button button--primary button--lg" data-v-5c4d7450="">
explore_a
</button><button type="button" class="button ab-test-switch__button button--secondary button--lg">
explore_b
</button>
</div>
</div>
</div>
// this fails as the DOM changes after each click
cy.get('.ab-test-switch__buttons > :nth-child(1)').each(($el) => {
cy.wrap($el).click()
cy.wait(1000) // didn't help, there's no race condition here
})
before(() => {
cy.visit('/company_profile_frontend/ab-test-switch')
// cy.get('.ab-test-switch__buttons > :nth-child(1)').click({ multiple: true, force: true }) (this didn't work either)
const numberOfAbTests = document.getElementsByClassName('ab-test-switch__buttons').length
for (let i = 1; i <= numberOfAbTests; i += 1) {
cy.get(`.ab-test-switch__buttons > :nth-child(${i})`).click().pause()
}
// cy.get('.ab-test-switch__buttons > :nth-child(1)').each(($el) => {
// cy.wrap($el).click().pause()
// // eslint-disable-next-line cypress/no-unnecessary-waiting
// cy.wait(1000)
// }) (another failed attempt)
})
Any other way to make this work?