简体   繁体   English

使用柏树在 window 上设置属性

[英]Set a property on window with cypress

I need to test a react app that is supposed to run as a webview within another native app.我需要测试一个应该在另一个本机应用程序中作为 webview 运行的反应应用程序。 The webview server sets session variables into the window, which is further used to authenticate my app. webview 服务器将 session 变量设置到 window 中,进一步用于验证我的应用程序。

I want to mock this by doing something like:我想通过执行以下操作来模拟它:

window.appEnvironment={ 
//session variables
}

I have tried cy.window(), but it doesnt seem to help.我已经尝试过 cy.window(),但它似乎没有帮助。 How can I set properties on window from cypress.如何在赛普拉斯的 window 上设置属性。

Put this into the support file:将其放入支持文件中:

Cypress.on('window:before:load', win => {
    win.appEnvironment = { 
        //session variables
    }
});

Now this snippet will be evaluated before any of your application code is initialized.现在,将在初始化任何应用程序代码之前评估此代码段。

miklos_me almost had it right. miklos_me 几乎是正确的。 You should use the passed in win object instead of the global window object.您应该使用传入的 win object 而不是全局 window object。

Cypress.on('window:before:load', win => {
    win.appEnvironment = { 
        //session variables
    }
});

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

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