Set a property on window with cypress

Viewed 4527

I need to test a react app that is supposed to run as a webview within another native app. The webview server sets session variables into the window, which is further used to authenticate my app.

I want to mock this by doing something like:

window.appEnvironment={ 
//session variables
}

I have tried cy.window() , but it doesnt seem to help. How can I set properties on window from cypress.

2 Answers

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. You should use the passed in win object instead of the global window object.

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