cypress: unable to access app from window object

2019-08-28 00:04发布

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条回答
闹够了就滚
2楼-- · 2019-08-28 00:14

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...:

enter image description here

查看更多
登录 后发表回答