Click each element from list until match with specific url

Viewed 59

First at all im newbie in automation test!

Well, I'm trying click every element from a list and for every element clicked the url change. So I want to stop the loop when the url match with my regex.

[List of elements][1] In the picture I marked the element that change the url /&selectedPaymentMethodId=70/

To do the first step i have the following code that obtains the list of elements to be clicked:

    cy.get('.CheckoutPayment-wrapper > div:nth-child(1) > div > div > div >')
    .each(($el,index,collection) => {
    for (let i = 0; i <$el.length; i++ ) {       
        cy.wrap($el).eq(i).then($size => {
            if ($size.length) { 
                cy.wrap($el).click()
                Cypress.on('uncaught:exception', (err, runnable) => {
                cy.url().should('not.match', /&selectedPaymentMethodId=70/)
                    return false
                })
               
            }
        })             
    }
}

I put an exception to no fail the test and continue but I don't know how to stop the loop when the executions will found the match

PD: I used some parts of code from another mates in the community! [1]: https://i.stack.imgur.com/TXEir.png

2 Answers

Well I found another way without compare the url looking in children's the data that can I use:

   let test = dataPool.cod;

    cy.get('.STRList-with_left_item')
    .each(($el,index,collection) => {
        cy.wrap($el).children().eq(0).children().invoke('attr', 'aria-labelledby').then($methdPayment => {
            cy.log($methdPayment.substr(8))
            if ($methdPayment.includes(test)) { 
                cy.wait(100)
                cy.log('Element found' )
                cy.wrap($el).click()
                cy.wait(200)  
            }
        });
    }
Related