Cypress does not support visiting multiple pages in one test. To check the URL of the link after clicking you must do so inside a cy.origin()
command.
const checkLink = (linkEl, expectedPath) => {
if (!expectedPath || !linkEl.attr('href')) return
cy.wrap(linkEl).invoke('removeAttr', 'target')
cy.wrap(linkEl).click()
cy.origin(expectedPath, { args: { expectedPath } }, ({expectedPath}) => {
cy.url().should('include', expectedPath)
cy.go('back')
})
}
cy.get('a#1').then($a => {
checkLink($a, 'https://example.com/')
})
This logs a passing test:
It also works for target="_blank"
links.
These are my two links, both work
<a id="1" href="https://example.com">Example</a>
<a id="2" target="_blank" href="https://example.com">Example</a>