cypress - 修复 cypress 中的片状测试的建议
问题描述
我有一个基于地图的应用程序(如谷歌地图),我正在为放大选项编写测试。放大时覆盖所有缩放级别的测试。我的测试正在运行,但结果不一致并且不稳定。
我的代码:
static verifyAllAvailableZoomInZoomSizeInNM() {
var expectedValues = [500,200,100,50,20,10,5,2,1,0.5,0.2,0.1,0.05,0.02,0.01,0.005,0.002,0.001,0.0005,0.0002,0.0001,0.00005,0.00002];
cy.getCurrentZoomSizes({
numValues: 26,//I expect to give 23 but I just gave 26 in order to capture all sizes
waitBetween: 1000,
elementLocator: BTN_MAP_ZOOMIN,
}).should("be.eql", expectedValues);
}
赛普拉斯命令:
/* Get the numeric value of zoom size in nm */
Cypress.Commands.add("getCurrentZoomSize", () => {
cy.get('div[class="ol-scale-line-inner"]').then(
($el) => +$el[0].innerText.replace(" nm", "")
);
});
/* Get a sequence of zoom size values */
Cypress.Commands.add("getCurrentZoomSizes", ({ numValues, waitBetween, elementLocator }) => {
const values = [];
Cypress._.times(numValues, () => {
cy.getCurrentZoomSize()
.then((value) => values.push(value))
cy.get(elementLocator)
.click()
.wait(waitBetween);
});
return cy.wrap(values);
});
和测试结果1:
正如您在屏幕截图中看到的那样,一些缩放尺寸重复了。我尝试在每次放大点击之间给予足够的等待,但这也无济于事。有什么办法可以解决这个不稳定的测试吗?
解决方案
该循环的执行速度比 Cypress 命令或缩放操作快得多,如果您console.log()
在循环内添加一个 just 就可以看到它
Cypress._.times(numValues, (index) => {
console.log(index)
这不一定是问题,它只是很快填满了命令队列,然后命令就会消失。
但是在getCurrentZoomSize()
通话之间,您需要放慢速度以完成缩放,并且使用.wait(waitBetween)
可能是事情变得不稳定的原因。
如果您将 应用于.should()
每个缩放级别,您将重试并在每个缩放操作之间等待。
问题是弄清楚如何安排事情以便发生正确的重试。
如果你这样做
cy.getCurrentZoomSize()
.should('eq', currentZoom);
这相当于
cy.get('div[class="ol-scale-line-inner"]')
.then($el => +$el[0].innerText.replace(" nm", "") )
.should('eq', currentZoom);
它不起作用,内部的转换.then()
妨碍了重试。
这行得通,
cy.get('div[class="ol-scale-line-inner"]')
.should($el => {
const value = +$el[0].innerText.replace(" nm", "")
expect(value).to.eq(expectedValue)
})
或这个
cy.get('div[class="ol-scale-line-inner"]')
.invoke('text')
.should('eq', `${currentZoom} nm`);
所以完整的测试可能是
Cypress.Commands.add("getCurrentZoomSizes", (expectedValues, elementLocator) => {
const numValues = expectedValues.length;
Cypress._.times(numValues, (index) => {
const currentZoom = expectedValues[index];
cy.get('div[class="ol-scale-line-inner"]')
.invoke('text')
.should('eq', ${currentZoom} nm`); // repeat scale read until zoom finishes
// or fail if never gets there
cy.get(elementLocator).click(); // go to next level
});
});
const expectedValues = [...
cy.getCurrentZoomSizes(expectedValues, BTN_MAP_ZOOMIN)
推荐阅读
- php - 缺少附件 .pdf 扩展名的传入电子邮件
- sql - 当小数点后出现零时在 Presto 中截断
- android-studio - 如何在 Android Studio 4.2 上导入 .AAR 模块
- networking - GCP - 没有 Cloud NAT,但给定公共 IP 离开 VPC
- python - 在弹丸运动中找到击中目标的角度
- django - 为什么 djangoRest 框架 retrieveAPIView 不能用电子邮件过滤?
- uml-designer - UML-Designer:隐藏关联标签
- javascript - 如何根据状态渲染组件
- anaconda - Anaconda Navigator 无法使用 PythonQtError 打开
- python - python左不包括2个数据帧之间的连接