使用赛普拉斯测试重定向到新路由 [英] Testing a redirect to a new route with Cypress
问题描述
我正在使用 Cypress 来测试我的Web应用程序。
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.
谢谢!!!
推荐答案
您需要做的是断言URL处于预期状态。
What you need to do is assert that the url is in the expected state.
赛普拉斯中有很多命令可用于对网址进行断言。具体来说, cy.url()
,< a href = https://on.cypress.io/location rel = nofollow noreferrer> cy.location()
或 cy.hash()
可能满足您的要求。可能是您的用例的最佳示例是:
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')
这篇关于使用赛普拉斯测试重定向到新路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!