如何在没有 cypress.json 的情况下配置 cypress-sql-server?

问题描述 投票:0回答:2

我正在尝试设置 cypress-sql-server,但我使用的是 10.8.0 版本,该版本不使用 cypress.json 来配置环境。我发现的所有设置说明都涉及使用 cypress.json 来配置插件。我遇到了一个错误:

tasksqlServer:execute, SELECT 'Bob'
CypressError
cy.task('sqlServer:execute') failed with the following error:

The 'task' event has not been registered in the setupNodeEvents method. You must register it before using cy.task()

Fix this in your setupNodeEvents method here:
D:\git\mcare.automation\client\cypress\cypress.config.jsLearn more
node_modules/cypress-sql-server/src/commands/db.js:7:1
   5 |     }
   6 | 
>  7 |     cy.task('sqlServer:execute', query).then(response => {
     | ^
   8 |       let result = [];
   9 | 

cypress.config.js

const { defineConfig } = require("cypress");
const sqlServer = require("cypress-sql-server");

module.exports = defineConfig({
  e2e: {
    setupNodeEvents(on, config) {
      // allows db data to be accessed in tests
      config.db = {
        "userName": "user",
        "password": "pass",
        "server": "myserver",
        "options": {
          "database": "mydb",
          "encrypt": true,
          "rowCollectionOnRequestCompletion": true
        }
      }

      // code from /plugins/index.js
      const tasks = sqlServer.loadDBPlugin(config.db);
      on('task', tasks);

      return config      
      // implement node event listeners here
    },
  },
});

testSQL.spec.js

describe('Testing SQL queries', () => {
    
    it("It should return Bob", () => {
        cy.sqlServer("SELECT 'Bob'").should('eq', 'Bob');
        
});
})

我的版本:

\cypress> npx cypress --version
Cypress package version: 10.8.0
Cypress binary version: 10.8.0
Electron version: 19.0.8
Bundled Node version:
16.14.2
``
automated-tests cypress
2个回答
4
投票

这是

cypress-sql-server
当前为Cypress v9

提供的安装说明

插件文件

插件可以在 cypress/plugins/index.js 文件中初始化,如下所示。

const sqlServer = require('cypress-sql-server');

module.exports = (on, config) => {
  tasks = sqlServer.loadDBPlugin(config.db);
  on('task', tasks);
}

将其翻译为 Cypress v10+

const { defineConfig } = require('cypress')
const sqlServer = require('cypress-sql-server');

module.exports = defineConfig({
  e2e: {
    setupNodeEvents(on, config) {

      // allows db data to be accessed in tests
      config.db = {
        "userName": "user",
        "password": "pass",
        "server": "myserver",
        "options": {
          "database": "mydb",
          "encrypt": true,
          "rowCollectionOnRequestCompletion": true
        }
      }

      // code from /plugins/index.js
      const tasks = sqlServer.loadDBPlugin(config.db);
      on('task', tasks);

      return config
    },
  },
})

其他变体也有效,例如将

"db": {...}
部分放在
"e2e: {...}"
部分下方,但 不将 放在
"env": {...}
部分中。

自定义命令

Cypress v9

说明

命令文件

该扩展提供了多组命令。您可以导入您需要的。
示例

support/index.js
文件。

import sqlServer from 'cypress-sql-server';
sqlServer.loadDBCommands(); 

对于赛普拉斯 v10+

只需将此代码移至

support/e2e.js


-1
投票

cypress.json
是指定 Cypress 环境变量的一种方法。 您可以使用该链接中的任何策略,而不是使用
cypress.json
文件。

如果您只是想将它们包含在您的

cypress.config.js
中,它看起来像这样:

const { defineConfig } = require('cypress')

module.exports = defineConfig({
  e2e: {
    baseUrl: 'http://localhost:1234',
    env: {
      db: {
        // your db values here
      }
    }
  }
})
© www.soinside.com 2019 - 2024. All rights reserved.