【问题标题】:How wait for page finish reloading in cypress after click?点击后如何等待页面完成在柏树中重新加载?
【发布时间】:2018-05-28 20:30:26
【问题描述】:

我得到了表单,当我触发点击提交按钮时,这会刷新当前页面。在那之后,我想做一些断言。我怎么知道页面完成刷新并且可以开始搜索元素?

cy.get('#formButton').click() // adds new item and refresh page
// do assertions on page

【问题讨论】:

标签: cypress


【解决方案1】:

这个解决方案是posted by @msty on a similar GitHub issues:

// Navigate / submit form
cy.click('#someButtonToNavigateOnNewPage');

cy.location('pathname', {timeout: 10000})
  .should('include', '/newPage');

// Do assertions here
cy.contains('h1', 'success')

Cypress Docs 的另一个解决方案是:

// Wait for the route aliased as 'getAccount' to respond
// without changing or stubbing its response
cy.server()
cy.route('/accounts/*').as('getAccount')
cy.visit('/accounts/123')
cy.wait('@getAccount').then((xhr) => {
  // we can now access the low level xhr
  // that contains the request body,
  // response body, status, etc
})

【讨论】:

    【解决方案2】:

    5 种方式:

    1) 制作 SPA 不刷新,并在更改 DOM 后使用回调/承诺;

    2) 设置cookie,刷新页面,每次页面加载使用条件如:

    if (existsRefreshCookieAfterFormSubmit()) { ...
    

    3) 像这样阻止表单发送:

    form.onsubmit = function() { return false; };
    

    4) 使用<a> 代替<button><input>

    5) 不要使用 <form> 作为输入的父级,仅使用输入。

    【讨论】:

      猜你喜欢
      • 2018-09-08
      • 1970-01-01
      • 1970-01-01
      • 2018-01-20
      • 1970-01-01
      • 2015-09-27
      • 2019-03-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多