vue.js - 如何使 Cypress 中的请求调用更稳定
问题描述
当我在赛普拉斯测试中观看某些路线时,它失败了 1/8 次。有没有办法让赛普拉斯测试总是通过,而不是在多次尝试后随机失败?
这是我的代码:
it("Check if scroll is working on list", () => {
cy.server()
cy.route('GET', '**/api/scorer/company/news/**', { timeout: 15000 }).as('loadMoreAbstracts');
cy.get('[data-cy=VirtualScroll]', { timeout: 15000 }).eq(1).scrollTo("bottom").then(()=>{
cy.wait('@loadMoreAbstracts', { timeout: 15000 }).its('status').should('equal', 200);
})
cy.get('[data-cy=VirtualScroll]', { timeout: 15000 }).eq(1).invoke('scrollTop').should('be.gt', 0)
cy.get('[data-cy=VirtualScroll]', { timeout: 15000 }).eq(1).scrollTo("bottom")
})
我看不出这段代码有什么问题,但不知何故,它时不时地无缘无故地失败。有一些东西使它不稳定,但是有没有办法让赛普拉斯测试更稳定?
我得到的错误是:
CypressError: Timed out retrying: `cy.wait()` timed out waiting `15000ms` for the 1st request to the route: `loadMoreAbstracts`. No request ever occurred.
解决方案
我不确定,但看起来您正在使用外部 API 来获取数据。这自然会在您的测试中引入可变时间,从而导致一些失败。
如果您使用cy.intercept()
(cy.route()
已弃用),那么您可以提供一个响应对象来满足请求。这样做的好处是它将消除外部(和高度可变的)依赖,并且应该使您的测试更加可靠。
副作用也将是测试将运行得更快。
推荐阅读
- node.js - 查找某个时间段内的mongodb数据
- php - SQL 查询在准备好的语句中使用时不起作用,但在 PhpMyAdmin 中起作用
- python - lightfm 错误:并非所有估计的参数都是有限的,您的模型可能已经发散
- mysql - 使用sql查询连接4个表
- android - 使用 tinylog 2.0
- javascript - 动态生成带有内部图像的 SVG?
- mysql - 如何在另一个表中使用搜索时使用 MySQL 更新表
- ruby - Ruby 解压二进制文件
- awk - 散列文本文件中每一行的第一列并将其写入文件的第三列
- php - 注释表给出 SQLSTATE[42S02] laravel 5.8