【问题标题】:Testing a redirect to a new route with Cypress使用 Cypress 测试重定向到新路由
【发布时间】:2018-04-01 01:26:00
【问题描述】:

我正在使用Cypress 测试我的网络应用程序。

这个sn-p目前有效,会提交一个新东西:

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?
  })
})

正如评论所示,我不确定如何测试重定向到新路由是否有效。我可以在浏览器模拟中看到重定向有效,我只是想为它添加一个测试。

谢谢!!!

【问题讨论】:

    标签: javascript testing mocha.js cypress


    【解决方案1】:

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

    赛普拉斯中有许多命令可用于对 url 进行断言。具体来说,cy.url()cy.location()cy.hash() 可能满足您的要求。您的用例可能是最好的例子:

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

    【讨论】:

    • 所以......我的网络应用程序正在发布到的服务器可能需要很长时间才能响应(比如超过十秒)......我仍然得到这个错误:CypressError: Timed out retrying : 预期 '/create' 等于 '/newthing/:id' ...我可以更改 cypress 将继续尝试多长时间吗?另外,谢谢你的帮助,真的很感激!!!
    • 是的。您可以详细了解这些定时重试的工作原理here。从本质上讲,赛普拉斯将不断检查位置的路径,直到它与您的断言 (/newthing/:id) 匹配或直到它超时。默认超时为 4000 毫秒。您可以通过将超时选项传递给cy.location() 来增加此时间,如下所示:cy.location('pathname', {timeout: 10000}).should('eq', '/newthing/:id')
    【解决方案2】:

    不管其他参数如何,使用下面的代码找出url

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

    【讨论】:

      猜你喜欢
      • 2012-02-12
      • 2017-10-30
      • 2014-10-22
      • 2015-11-24
      • 2020-08-28
      • 2017-07-27
      • 1970-01-01
      • 2020-04-29
      • 2021-12-22
      相关资源
      最近更新 更多