How to get the URL of a type 'submit' button during Cypress test?

1.5k Views Asked by At

Below is a submit button on the page I am trying to test.

When you click this button, a pop-up window is displayed. The URL of this pop-up is https://login.microsoftonline.com/........etc.

enter image description here

This sign in uses SSO, but we are having difficulty bypassing the sign-in process using requests.

So we are instead trying to capture the URL of the pop-up during the test.

I have seen examples where people retrieve the href attribute of a button. However, as you can see above, there is no href on this button.

I've searched the HTML for a form element too, but can't find that.

I'm just wondering, is there a way I can get the URL of the pop-up window that clicking the above button generates?

2

There are 2 best solutions below

0
Fody On

To catch the URL, try adding an intercept before the click

cy.intercept('POST', '*').as('submit')

cy.get('button').click()

cy.wait('@submit').then(interception => {
  console.log(interception.request.url)
})

Note this is debugging code only to help find the URL. Don't use it in a long-term test.

Additional notes:

  • I'm assuming 'POST' as it's most common for submit, but may need 'GET' instead.

  • using '*' will catch anything, so you may catch so stray requests from the page load with the first cy.wait('@submit'). If so, just add more cy.wait('@submit') or a long cy.wait() before the click.

0
jjhelguero On

The new tab is likely to be trigger via javascript. If it is using [window.open][1] the you can stub it to open in the same ta

cy.window().then(win => {
    cy.stub(win, 'open').callsFake((url, target) => {
      expect(target).to.be.undefined
      // call the original `win.open` method
      // but pass the `_self` argument
      return win.open.wrappedMethod.call(win, url, '_self')
    }).as('open')
  })
  cy.get('a').click()
  cy.get('@open').should('have.been.calledOnceWithExactly', 'url')
})


  [1]: https://glebbahmutov.com/blog/cypress-tips-and-tricks/#deal-with-windowopen