2

I am using cypress to test the flow of my application. At this point it just opens an account and follows the flow as a user would. At a point I copy a link and would like to follow that link.

The issue I have is that the link changes with every test that I run and I don't know what the link is until its been copied. When the test finishes, I would like to paste that link in the browser and make sure that the page does exist.

I cant seem to find a way to paste from my clipboard. Is there a way to do this ? My next test basically needs to start with

cy.visit('paste');

Ive tried doing

cy.visit('{Ctrlv}');

But that does not seem to work.

Poodle
  • 143
  • 7
ItsNotAndy
  • 563
  • 5
  • 17

1 Answers1

4

I think what you want to do is use cy.request() to test the link exist.

cy.visit(pasted-link-here) might be tricky if the link you need to test is outside the original domain, but cy.request() can give you a status code.

cy.window().then(win => {
  win.navigator.clipboard.readText().then(urlFromClipboard => {
    cy.request(urlFromClipboard)
      .then(response => expect(response.status).to.eq(200))
  })
})

Actually, see here

cy.request() requires that the response status code be 2xx or 3xx

so you could just use this

cy.window().then(win => {
  win.navigator.clipboard.readText().then(urlFromClipboard => {
    cy.request(urlFromClipboard)
  })
})

and the test will fail if status code is a failure code.

user16695029
  • 3,365
  • 5
  • 21
  • Thank you ! This worked for what I was trying to achieve. I was told now that I do need to make some assertions on this page... Which I cant do unless it loads, so now Im back to square one #MovingTargetLand :/ – ItsNotAndy Aug 27 '21 at 09:28
  • 1
    Apologies I got last comment backwards - You can set `failOnStatusCode: false` option to allow the response to be checked even if it fails - see [How to test a bad request in cypress](https://stackoverflow.com/a/56853989/16695029) – user16695029 Aug 27 '21 at 09:40