79250128

Date: 2024-12-04 06:59:11
Score: 0.5
Natty:
Report link

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:

enter image description here

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>
Reasons:
  • Probably link only (1):
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Low reputation (0.5):
Posted by: Emmanuelle Seigner