Test dragging a Leaflet map in Cypress(在Cypress中测试拖动传单地图)
本文介绍了在Cypress中测试拖动传单地图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个非常简单的用例:我希望在更改传单地图视口时通过抓取它来测试数据获取。然而,我不知道如何写这个测试。 代码如下:
cy.visit("/map");
// wait for data
cy.get(".leaflet-interactive.multilinestring");
// simulate map moving
cy.get(".leaflet-container")
.trigger("mousedown", "center")
.trigger("mousemove", 30, 30);
.trigger("mouseup");
// map should be loading
cy.get(".leaflet-container.leaflet-loading");
它似乎没有移动地图。
我已尝试在触发器调用之间添加等待,因为我认为可能有关于事件激发速度的警卫,但没有运气。
是否知道如何测试此功能。
推荐答案
似乎不使用cy.trigger()
,而是需要使用本机事件使传单认为地图正在拖动。
我们编写了一个自定义的Cypress命令dragMapFromCenter
,使用它如下所示:
cy.get('#map-canvas').dragMapFromCenter({
// Go 1/6 of map container width to the right (negative direction)
xMoveFactor: -1 / 6,
// Go 1/3 of map container height to the top (positive direction)
yMoveFactor: 1 / 3
});
// We need to wait for something to happen after map starts moving
cy.get(".leaflet-container.leaflet-loading");
下面是dragMapFromCenter
的实现。将其放入cypress/support/commands.js
以便能够在测试中使用。
// # cy.get('#map-canvas').dragMapFromCenter({ xMoveFactor: 0.25, yMoveFactor: -0.5 })
//
// Allows dragging a Leaflet map by the given amounts. A factor of 1 means the map
// will be dragged the whole width of the map canvas in X direction and the whole
// height of the map canvas in Y direction.
Cypress.Commands.add(
'dragMapFromCenter',
{ prevSubject: 'element' },
(element, { xMoveFactor, yMoveFactor }) => {
// Get the raw HTML element from jQuery wrapper
const canvas = element.get(0);
const rect = canvas.getBoundingClientRect();
const center = {
x: rect.left + rect.width / 2,
y: rect.top + rect.height / 2
};
// Start dragging from the center of the map
cy.log('mousedown', {
clientX: center.x,
clientY: center.y
});
canvas.dispatchEvent(
new MouseEvent('mousedown', {
clientX: center.x,
clientY: center.y
})
);
// Let Leaflet know the mouse has started to move. The diff between
// mousedown and mousemove event needs to be large enough so that Leaflet
// will really think the mouse is moving and not that it was a click where
// the mouse moved just a tiny amount.
cy.log('mousemove', {
clientX: center.x,
clientY: center.y + 5
});
canvas.dispatchEvent(
new MouseEvent('mousemove', {
clientX: center.x,
clientY: center.y + 5,
bubbles: true
})
);
// After Leaflet knows mouse is moving, we move the mouse as depicted by the options.
cy.log('mousemove', {
clientX: center.x + rect.width * xMoveFactor,
clientY: center.y + rect.height * yMoveFactor
});
canvas.dispatchEvent(
new MouseEvent('mousemove', {
clientX: center.x + rect.width * xMoveFactor,
clientY: center.y + rect.height * yMoveFactor,
bubbles: true
})
);
// Now when we "release" the mouse, Leaflet will fire a "dragend" event and
// the search should register that the drag has stopped and run callbacks.
cy.log('mouseup', {
clientX: center.x + rect.width * xMoveFactor,
clientY: center.y + rect.height * yMoveFactor
});
requestAnimationFrame(() => {
canvas.dispatchEvent(
new MouseEvent('mouseup', {
clientX: center.x + rect.width * xMoveFactor,
clientY: center.y + rect.height * yMoveFactor,
bubbles: true
})
);
});
}
);
这篇关于在Cypress中测试拖动传单地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:在Cypress中测试拖动传单地图
猜你喜欢
- 如何使用 JSON 格式的 jQuery AJAX 从 .cfm 页面输出查 2022-01-01
- 失败的 Canvas 360 jquery 插件 2022-01-01
- Fetch API 如何获取响应体? 2022-01-01
- Quasar 2+Apollo:错误:找不到ID为默认的Apollo客户端。如果您在组件设置之外,请使用ProvideApolloClient() 2022-01-01
- Css:将嵌套元素定位在父元素边界之外一点 2022-09-07
- 400或500级别的HTTP响应 2022-01-01
- addEventListener 在 IE 11 中不起作用 2022-01-01
- Flexslider 箭头未正确显示 2022-01-01
- CSS媒体查询(最大高度)不起作用,但为什么? 2022-01-01
- 使用RSelum从网站(报纸档案)中抓取多个网页 2022-09-06