在我的应用程序中,当我访问一个页面时,它会发出一些网络请求来获取数据并将其显示在页面上。之后,您单击按钮并填写字段以过滤该数据。
我有一个 cypress 测试,它基本上会访问该页面,应用一些过滤器,并确保 dom 中的内容看起来正确:
it(`filters the data by 'price'`, () => {
cy.server()
cy.route('POST', 'http://my-api.biz/api').as('apiRequest')
cy.visit('/')
// initial page load loads the min and max price bounds for the UI,
// as well as the data to initially populate the page. they happen
// to hit the same URL with different POST params
cy.wait(['@apiRequest', '@apiRequest'])
cy.get('#price-filter-min').type('1000')
cy.get('#price-filter-max').type('1400')
// wait for data to get refreshed
cy.wait('@apiRequest')
cy
.get('[data-test-column="price"]')
.each($el => {
const value = parseFloat($el.text())
expect(value).to.be.gte(1000)
expect(value).to.be.lte(1400)
})
})
但是有时 cypress 似乎会加载页面,在等待之前执行 XHR 请求,然后偶尔会失败:
CypressError:重试超时:cy.wait() 超时等待 30000 毫秒以等待对路由的第二次响应:'apiRequest'。从未发生任何反应。
因为它正在等待一个已经发生的请求。
有没有更好的方法来编写这个测试?有没有办法访问页面并等待避免这种竞争条件的 XHR 请求?
更新
我试图在一个孤立的测试用例中重新创建它,但它似乎一切正常,所以可能存在一些操作员错误。