Javascript 使用Cypress测试重定向到新路由

Javascript 使用Cypress测试重定向到新路由,javascript,testing,mocha.js,cypress,Javascript,Testing,Mocha.js,Cypress,我正在使用它来测试我的web应用程序 此代码段当前有效,并将提交新内容: 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()

我正在使用它来测试我的web应用程序

此代码段当前有效,并将提交新内容:

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?
  })
})
如评论所示,我不确定如何测试重定向到新路由是否有效。我可以在浏览器模拟中看到重定向工作,我只想为它添加一个测试


谢谢

您需要做的是断言url处于预期状态

Cypress中有许多命令可用于对url进行断言。具体而言,或可能满足您的要求。您的用例最好的例子可能是:

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

使用下面的代码查找url,而不考虑其他参数

cy.location().should((loc) => {
   expect(loc.pathname.toString()).to.contain('/home');
 });

所以我的web应用发布到的这个服务器可能需要很长时间才能响应(比如超过10秒)。。。我仍然收到以下错误:CypressError:超时重试:预期“/create”等于“/newthing/:id”。。。我能改变cypress尝试一件事的时间吗?还有,谢谢你的帮助,真的很感激!!!对您可以详细了解这些定时重试是如何工作的。基本上,Cypress将持续检查位置的路径,直到它匹配您的断言(
/newthing/:id
)或直到超时。默认超时为4000ms。您可以通过将超时选项传递给
cy.location()
来增加此时间,例如:
cy.location('pathname',{timeout:10000}).should('eq','/newthing/:id')