避免使用cy.wait()等待页面加载,因为获取请求cypress被中止 [英] Avoid using cy.wait() to wait for a page to load due to aborted get request cypress

查看:1565
本文介绍了避免使用cy.wait()等待页面加载,因为获取请求cypress被中止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行一个赛普拉斯测试,该测试要求在单击元素并登录之前先加载页面,但是由于某种原因,失败(且中止)的GET请求导致页面永久加载并最终超时除非我在cy.click()调用之前添加cy.wait(6000)。我需要以某种方式等待页面加载而不使用cy.wait()。鉴于我无法解决中止的GET请求,该怎么办?

I'm running a cypress test that requires a page to load before clicking an element and logging in, but for some reason a failed (and aborted) GET request is causing the page to take forever to load and eventually it times out unless I add a cy.wait(6000) before the cy.click() call. I need to somehow wait for the page to load without using a cy.wait(). How can I do this, given that I cant fix the aborted GET request?

  cy.visit(loginUrl)
  cy.url().should('contain', '#/loginPortal')
  cy.wait(6000) //Allows page to load before trying to log in, needs to be removed
  cy.contains('ButtonText').click()


推荐答案

可以在断言之前让Cypress等待任何特定的XHR调用。等待时间由 responseTimeout配置定义。

You can make Cypress wait for any specific XHR call, before you assert. How long it waits is defined by the responseTimeout configuration.

cy.server();
cy.route('**/api/getData').as('getData');
cy.visit('/home');
cy.wait('@getData');
cy.contains('ButtonText').click()

赛普拉斯最佳实践:不必要的等待。

在等待别名时使用Cypress文档。

这篇关于避免使用cy.wait()等待页面加载,因为获取请求cypress被中止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆