cypress: unable to access app from window object

Multi tool use
cypress: unable to access app from window object
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 Answer
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.
window
window
window
cy.window()
Or you can use cy.state('window')
which returns the window object synchronously, but this is undocumented and may change in the future.
cy.state('window')
Related: if you want to access your AUT in the dev console, you'll need to switch the context to Your app...
:
Your app...
cy.window().its("vueApp")
Try checking your window object in the console as shown above. Is it possible your
main.js
isn't loaded at the time of your cy.window()
command executes?– Dwelle
Jul 2 at 9:53
main.js
cy.window()
Yes, apparently that was the problem, thanks.
– Tomer
Jul 2 at 11:04
You can wait for your
main.js
to be loaded using something like this (kinda clunky.. dunno of better way to do it yet).– Dwelle
Jul 2 at 11:07
main.js
No need, all i needed was to add
cy.visit("/")
to init the app– Tomer
Jul 2 at 11:34
cy.visit("/")
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Thanks, but i tried
cy.window().its("vueApp")
and i get an error that it doesn't exist.– Tomer
Jul 2 at 8:57