Skip to content

Instantly share code, notes, and snippets.

@ampc
Forked from axle07/cypress_xhr.js
Last active October 1, 2018 10:22
Show Gist options
  • Save ampc/1f9480b2c91bbe8b20792a4f235cbfdb to your computer and use it in GitHub Desktop.
Save ampc/1f9480b2c91bbe8b20792a4f235cbfdb 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.server()
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')
})
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment