I'm having trouble writing Cypress end-to-end tests for my Angular application due to the additional nested elements that Angular inserts. For example, if I have an Angular component with this HTML:
<a href="{{loginUrl}}" mat-raised-button>
Log In
</a>
Then I try to write a Cypress test like this:
it('has a login button with a login URL', () => {
cy.contains('Log In')
.should('have.attr', 'href');
});
But the test fails because Angular automatically inserts many elements, and the resulting HTML looks like this. cy.contains('Log In') picks up the span, which has no href.
<a href="login-url">
<button class="...">
<span>Log In</span>
</button>
</a>
Is there a generic way to tell Cypress to "put a virtual cursor over 'Log In' and then get the corresponding href"? Or, is there a better way to structure my HTML so that such tests can pass?