Testing a redirect to a new route with Cypress

邮差的信 提交于 2020-07-05 05:34:08

问题


I am using Cypress for testing my web application.

This snippet currently works and will submit a new thing:

describe('The Create Page', () => {
  it('successfully creates a thing', () => {
    cy.visit('/create')
    cy.get('input[name=description]').type('Hello World')
    cy.get('button#submit')
      .click()

    // After POST'ing this data via AJAX, the web app then
    // receives the id of the new thing that was just created
    // and redirects the user to /newthing/:id
    // How do I test that this redirection worked?
  })
})

As the comment illustrates, I am not sure how to test whether or not the redirection to the new route works. I can see it in the browser simulation that the redirect works, I just want to add a test for it.

Thanks!!!


回答1:


What you need to do is assert that the url is in the expected state.

There are many commands in Cypress that can be used to make assertions about the url. Specifically, cy.url(), cy.location() or cy.hash() may meet your requirements. Probably the best example for your use case would be this:

cy.location('pathname').should('eq', '/newthing/:id')


来源:https://stackoverflow.com/questions/46839520/testing-a-redirect-to-a-new-route-with-cypress

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!