更改页面时,Cypress 访问在 Next js 上不起作用

Ask*_*ing 2 javascript next.js cypress

我的下一个应用程序有 2 页:登录、汽车和通过 ID 返回的汽车。在“汽车”页面上,我想参观一辆特定的汽车。该 url 如下所示:/cars用于汽车和/cars/car-id包含汽车的页面。此外,当用户在汽车页面上重定向时,会发生一个返回有关汽车的数据的请求。为此,我在 cypresvisit方法中使用了:

cy.visit('http://localhost:3000/cars/1234') //to go to the car page by id (1234 is the car id)
cy.route('GET', 'fixture:car.json').as('getCar') // here i make the request
cy.wait('@getCar')
Run Code Online (Sandbox Code Playgroud)

但是这样做我得到: CypressError: Timed out retrying: cy.wait() timed out waiting 5000ms for the 1st request to the route: 'getCar'. No request ever occurred. ,但是如果我单击菜单项进入汽车页面,则一切正常: cy.get("a[href*=/car/1234]").click()一切正常。为什么visit会抛出这个错误?以及我的情况如何解决?

Fod*_*ody 5

路由监听器必须在访问前设置好。

替换Cypress v6 之后cy.intercept()替换的内容cy.route()

cy.intercept('GET', 'fixture:car.json').as('getCar') // here I listen for the request

cy.visit('http://localhost:3000/cars/1234') 

cy.wait('@getCar') 
Run Code Online (Sandbox Code Playgroud)