Skip to content

Instantly share code, notes, and snippets.

@axle07
Created March 15, 2018 18:23
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save axle07/758885e866987be96f8dbfd8b19a3d5e to your computer and use it in GitHub Desktop.
Save axle07/758885e866987be96f8dbfd8b19a3d5e to your computer and use it in GitHub Desktop.
Cypress.js: Assert that an XHR POST was successful.
describe('XHR form submission test', () => {
before(() => {
cy.visit('/testurl')
})
it('successfully submits an XHR on form submission', () => {
cy.get('[data-test="form"]').within(() => {
cy.get('#test-input').type('spyfu.com')
cy.route('POST', '/sumbit/here').as('postForm') // note that the route needs to match trailing slashes
cy.get('button[type="submit"]').click()
.wait('@postForm')
.its('status').should('be', 200)
}).then(() => {
cy.get('[data-test="success-message"]').should('exist')
})
})
})
@ruben-haegeman
Copy link

To really create an assert this line .its('status').should('be', 200) should actually be .its('status').should('equal', 200)

Thanks for the help.

@dimanyc
Copy link

dimanyc commented Aug 30, 2018

I think you also need to invoke cy.server() before using cy.route(...)

@ampc
Copy link

ampc commented Oct 1, 2018

I think you also need to invoke cy.server() before using cy.route(...)

I needed to do that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment