javascript - 无法使用 Cypress 获取 vuex 状态

标签 javascript vue.js vuex cypress quasar-framework

我正在关注 this blog . window.app 属性在添加代码后确实出现在控制台中,但是我无法引用博客中提到的 app.$store ans(这类似于在组件中引用商店的方式).我可以引用 app.store,它确实包含我的 vuex 存储中的属性,但它不包含 app.store.state,只包含 getter(和调度等) .

例如使用 const getStore = () => cy.window().its('app.store') 它给出:

screenshot from 2018-04-26 09-27-25

我更希望能够直接使用状态变量,而不仅仅是在我为其创建 getter 时。怎么了?这是因为我在我的 vuex 商店中使用了命名空间吗?

编辑: 这也可能与我将商店分配给窗口的方式有关。我使用 Quasar Framework,它在当前版本 (>0.15) 中根据它使用的 quasar.conf.js 文件中的设置进行了大量配置。我通过将此代码添加为 quasar plugin 来分配商店:

export default ({ app, store, Vue }) => {
  if (window.Cypress) { // should only be available during E2E tests
    window.app = app
  }
}

这样做确实为我提供了我提到的 app.store,但不是 app.$store(包括 state)。 Quasar 配置生成并显示在文件中,添加此插件后包含:

/**
 * THIS FILE IS GENERATED AUTOMATICALLY.
 * DO NOT EDIT.
 *
 * You are probably looking on adding initialization code.
 * Use "quasar new plugin <name>" and add it there.
 * One plugin per concern. Then reference the file(s) in quasar.conf.js > plugins:
 * plugins: ['file', ...] // do not add ".js" extension to it.
 **/
import './quasar'
import Vue from 'vue'
Vue.config.productionTip = false
import 'quasar-app-styl'
import 'src/css/app.styl'
import App from 'src/App'
import router from 'src/router'
import store from 'src/store'

const app = {
  el: '#q-app',
  router,
store,
  ...App
}

const plugins = []

import pluginCypress_vuex_store from 'src/plugins/cypress_vuex_store'
plugins.push(pluginCypress_vuex_store)

import pluginVuelidate from 'src/plugins/vuelidate'
plugins.push(pluginVuelidate)

plugins.forEach(plugin => plugin({ app, router, store, Vue }))

new Vue(app)

如您所见,插件是初始化 Vue 应用程序的一部分,而按照博客和您建议的方式,它是在 Vue 初始化之后附加的。由于 Quasar 自动生成配置,我不知道如何在初始化后执行“window.app = app”以查看这是否会导致我的问题...

最佳答案

我的 Vue 应用程序也是如此。看这个section

Before we write a test, we need to decide how to get to the store reference. To allow testing and controlling the application through the Vue instance I prefer to keep a reference to the component on the window object. In the app.js, set window.app for testability.

const app = new Vue({
  store,
  el: '.todoapp'
  //
})
window.app = app

最后一行是关键,当我添加测试代码如宣传的那样工作时。

请注意,要调整的文件可能是ma​​in.js,而不是app.js


Quasar - 特殊应用插件:启动

引用 this doc ,您可以通过生成此特殊插件并将其更改为

来实现正确的 Cypress window.app 引用
export default ({ app, Vue }) => {
  // do some logic here...

  // ... then, kick off our Quasar website/app:
  window.app = new Vue(app)
  // "app" has everything cooked in by Quasar CLI,
  // you don't need to inject it with anything at this point
}

请注意,这不是标准的类星体应用程序插件。

关于javascript - 无法使用 Cypress 获取 vuex 状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50037628/

相关文章:

javascript - 使用 CSS 和最小的 jquery 或 javascript 形成普通文本

vue.js - 元素 UI 表 - 添加百分比作为宽度

javascript - 带参数的 vuexjs getter

checkbox - Vuejs + Vuex 获取复选框状态(选中)

javascript - 处置图像对象

javascript - ajax 创建的链接不像硬编码链接那样工作(CSS 对象类操作)

javascript - ssh2 模块静默失败,凭据从 CLI 成功

javascript - 对 VueJS 生命周期钩子(Hook)的误解

javascript - vue.js中如何向组件发送数据?

vue.js - 从 VueRouter.beforeEach() 访问 Vuex 时存储 undefined