使用赛普拉斯检测Openlayers功能

时间:2020-07-29 15:42:05

标签: javascript openlayers cypress

如何在Cypress测试中检查Openlayers地图上的功能?

我尝试将地图对象分配给全局变量,但赛普拉斯无法将其拾取:

    // openlayers file
    window.olMap = MapInstance;

    // cypress spec
    cy.window().then(window => {
        console.log(window.olMap.getLayers()); // typeerror olMap is undefined, yet I can see it in cypress console
    });

在Cypress中是否有更好的方法来检测功能?



enter image description here

enter image description here

1 个答案:

答案 0 :(得分:3)

由于赛普拉斯的异步特性,必须在测试继续之前加载地图,这是我解决的方法:

    // Add in this get method which will wait until load
    cy.get(".ol-viewport");

    cy.window().then(win => {
        const layers = win.olMap.getLayers().getArray();
        console.log(layers); // Displays list of layers
    });