cypress: unable to access app from window object

2019-08-27 23:51发布

问题:

I need to access my vuex store in cypress tests, so i added the app to the window object in my main.js:

const app = new Vue({
  router,
  store,
  render: h => h(App)
}).$mount("#app");

window.vueApp = app;

And then i try to access it in my login command (commands.js):

cy
    .request({
      method: "POST",
      url: "http://localhost:8081/api/v1/login",
      body: {},
      headers: {
        Authorization: "Basic " + btoa("administrator:12345678")
      }
    })
    .then(resp => {
      console.log("app:", window.vueApp);
      ...
      window.localStorage.setItem("aq-username", "administrator");
    });

but it's always undefined, what am i doing wrong?

回答1:

window that you're using refers to the cypress runner window. If you want to access the window of your AUT (application under test), use cy.window() command.

Or you can use cy.state('window') which returns the window object synchronously, but this is undocumented and may change in the future.

Related: if you want to access your AUT in the dev console, you'll need to switch the context to Your app...: