On the HTML page nested div tags are there. And either div tag ID = x or tag ID =y. in different HTML pages.
<--! example 1 -->
<body>
<div>
<div></div>
<div id=Y></div>
</div>
</body>
<--! example 2 -->
<body>
<div>
<div></div>
<div></div>
<div>
<div id=X></div>
</div>
</div>
</body>
We can iterate through all
div
s in your DOM, and using JQuery commands, we can determine if thediv
has a certain id.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 thatdiv#X
existed, we could just docy.get('div#X')
and get on with the tests. You could sure this up by adding a specificdata-cy
attr (althoughid
should be sufficient) and more importantly by writing your tests to be determinant and atomic.