0

I have iframe for payments, after successful payment I need to click 'go back to application' button. And here is a problem. Button has a new link in href attribute for the app. After clicking this button, it seems like Cypress loses test scenario, there is no more Cypress steps on the left side of browser window. And it doesn't make further steps.

How to handle this situation ? How to make Cypress keep going by scenario ?

Igor Vlasuyk
  • 514
  • 2
  • 10
  • 22
  • Can you repeat `cy.visit()` to the baseUrl instead of `cy.go('back')`? Or `cy.visit(href)`. – Fody Jun 10 '22 at 06:57
  • Please provide more information including code and urls involved. – SuchAnIgnorantThingToDo-UKR Jun 10 '22 at 07:30
  • 1
    @Fody, yeah, this workaround works. But it doesn't follow user behavior, cause button wasn't clicked. `cy.iframe(locator) .find(element) .invoke('attr', 'href').then(href => { cy.visit(href) });` – Igor Vlasuyk Jun 10 '22 at 07:52
  • 1
    I guess you just need to confirm the `href` matches the expected one. Trying to think what regression can happen, that's the most obvious (href changed to something incorrect). – Fody Jun 10 '22 at 07:58
  • You can see [here](https://glebbahmutov.com/blog/cypress-tips-and-tricks/#deal-with-windowlocationreplace) example of stubbing `window.location.replace` and clicking in test, then check the stub was called. But depends on how the app navigates. I had to stub `window.location.assign` as well. – Fody Jun 10 '22 at 08:10

1 Answers1

0

Not sure if this will suit your test, but you can stub the navigation with cy.intercept().

const href = 'url-from-link-href'

cy.location().then(loc => {  

  cy.intercept({url: href}, (req => {
    expect(req.url).to.eq(href)  // confirms href seen on intercepted network call
    req.url = loc.href           // stop navigation, remain at original loc
  })).as('redirect')

  cy.get(`a[href="${href}"]`)
    .click()                     // click the link
  cy.wait('@redirect')           // confirms intercept caught the redirect

  cy.visit(href)
  ... 
})
Fody
  • 23,754
  • 3
  • 20
  • 37