如何使用 Cypress 设置新窗口属性

问题描述 投票:0回答:2

我想在 Cypress 中加载页面时设置一个新的窗口属性。例如,这是一个新属性,我想在页面加载完成后在应用程序上下文中设置它。

window.configTemplate = {
   templates: {}
}

我尝试使用以下方法,但是当我尝试访问控制台中的 window 属性时,“window.configTemplate”返回“未定义”

cy.window().then(win => {

win.configTemplate = {
         templates: {
            'list-1': {
              index: 1,
              componentRef: 'titleContainer',
              props: {
                title: 'Test XX',
              },
            }
         }
}
});

我也尝试将代码片段放入 onBeforeLoad (win) {} 中,但它不起作用:(

window cypress
2个回答
0
投票

这可能确实有效,但您记录了错误的窗口(Cypress runner 有

window
并且您使用

访问应用程序窗口
cy.window().then(win => {
  console.log(win.configTemplate)

但是您的应用程序需要

configTemplate
吗?还是只是测试?如果进行测试,您可以使用运行器窗口或
Cypress
对象

Cypress.configTemplate = {
  templates: {
    ...
  }

console.log(Cypress.configTemplate)

0
投票

您是否尝试过 Cypress 上的 before load 事件

Cypress.on('window:before:load', win => {
  win.configTemplate = {
    "templates": {
        "list-1": {
            "index": 1,
            "componentRef": "titleContainer",
            "props": {
                "title": "Test XX"
            }
        }
    }
  }
});

这将在页面加载之前设置

windows.configTemplate
。您还可以调整活动以满足您的要求。

© www.soinside.com 2019 - 2024. All rights reserved.