We can iterate through all div
s in your DOM, and using JQuery commands, we can determine if the div
has a certain id.
cy.get('div').each(($div) => {
if ($div.attr('id') === 'x') {
// code to run if id = X
} else if ($div.attr('id') === 'y') {
// code to run if id = y
} else {
// code to run if neither
}
})
If you end up with more than just two or three cases, I'd probably recommend using a switch statement instead of if/else.
Additionally, the above code will solve your problem, but will probably be less performant than if you were to have a stable DOM that you knew either did or did not have the div#X
/div#Y
element. In this case, we have to traverse every single div element. If we knew for certain that div#X
existed, we could just do cy.get('div#X')
and get on with the tests. You could sure this up by adding a specific data-cy
attr (although id
should be sufficient) and more importantly by writing your tests to be determinant and atomic.