简体   繁体   English

赛普拉斯没有正确拦截 leaflet map 磁贴调用

[英]Cypress not properly intercepting leaflet map tile calls

I am testing a react-leaflet based application in cypress.我正在 cypress 中测试基于 react-leaflet 的应用程序。 To avoid making tons of real maptile requests, I am trying to intercept calls to the mapbox maptile server, and replace with a dummy tile.为了避免发出大量真实的 maptile 请求,我试图拦截对 mapbox maptile 服务器的调用,并用虚拟图块替换。 I do this in my cypress/support.index.js file:我在我的cypress/support.index.js文件中这样做:

/**
 * Intercept all calls for mapbox tiles and replace with dummy tile
 */
beforeEach(() => {
    console.log("in beforeach");
    cy.intercept("https://api.mapbox.com/**/*", {
        fixture: "images/tile.png",
    });
});

A simple test:一个简单的测试:

describe("The map", () => {
    it("Tiles should be dummy tiles, not actual tiles", () => {
        cy.visit("http://localhost:3000");
    });
});

I took a look at Mock leaflet resources in Cypress , and this tactic seemed to work for this person.我查看了 Cypress 中的 Mock leaflet 资源,这个策略似乎对这个人有用。 When I run a test, I do see the in beforeach log, so I know its running.当我运行测试时,我确实看到了in beforeach日志,所以我知道它正在运行。 However, in my test, I don't even see calls that leaflet is making to get tiles in the list of.network requests.但是,在我的测试中,我什至没有看到 leaflet 正在为获取 .network 请求列表中的图块而进行的调用。 As you see on the left, I only see XHR requests.如您在左侧所见,我只看到 XHR 请求。 But when I open up the.network tab of the cypress runner, clearly we are calling for the tiles (proprietary stuff covered up):但是当我打开 cypress runner 的 .network 选项卡时,显然我们正在调用 tiles(专有的东西被掩盖了):

在此处输入图像描述

Why is cypress not even showing the calls being made to get map tiles?为什么赛普拉斯甚至不显示为获取 map 个图块而进行的调用? Why are the calls not being intercepted, and the tiles replaced with the dummy?为什么电话没有被拦截,瓷砖被假人取代?

I'm usig Cypress 9.5.2, running Chrome 99, with Leaflet 1.7.1, and NexJS 10.2.0.我正在使用 Cypress 9.5.2,运行 Chrome 99,带有 Leaflet 1.7.1 和 NexJS 10.2.0。

I think you might be getting map tiles from cache, at least that's what I found trying out the intercept on my project.我认为您可能会从缓存中获取 map 个图块,至少这是我在尝试拦截项目时发现的结果。

See Cypress intercept problems - cached response请参阅赛普拉斯拦截问题 - 缓存响应

The server determines the data "cache key" in this case by looking at the if-none-match request header sent by the web application.在这种情况下,服务器通过查看 web 应用程序发送的 if-none-match 请求 header 来确定数据“缓存键”。

Try this to disable caching试试这个来禁用缓存

cy.intercept('https://api.mapbox.com/**/*', req => {
  delete req.headers['if-none-match'];
  req.reply({
    fixture: 'images/tile.png'
  })
})

The above kind of worked and then it didn't, and I can't get my head around it.上面的那种工作然后就没有了,我无法理解它。

As an alternative, you can flick the switch in devtools as follows作为替代方案,您可以按如下所示轻按 devtools 中的开关

cy.wrap(Cypress.automation('remote:debugger:protocol', {
  command: 'Network.clearBrowserCache',
}))

cy.intercept("https://api.mapbox.com/**/*", {
  fixture: "images/tile.png",
})

Since you are using Next, is the app fetching the tiles on the server before the page loads?由于您使用的是 Next,应用程序是否会在页面加载之前获取服务器上的图块?

I know there's various switches/modes, but you made no mention of SSR so looks like you should consider that.我知道有各种开关/模式,但你没有提到 SSR,所以看起来你应该考虑一下。

Without details of the app it's hard to give you code, but there's a bit of code here没有应用程序的详细信息,很难给你代码,但这里有一些代码

that may be useful.这可能会有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM