首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何配置没有cypress.json的cypress-sql-server?(更新)

如何配置没有cypress.json的cypress-sql-server?(更新)
EN

Stack Overflow用户
提问于 2022-09-23 17:32:32
回答 2查看 175关注 0票数 1

我正在尝试安装cypress-sql-server,但是我使用的是10.8.0版本,它不使用cypress.json来配置环境。我找到的所有安装说明都提到使用cypress.json配置插件。在u/Fody的帮助下,我更接近了,但我仍然遇到了一个错误:

代码语言:javascript
复制
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

代码语言:javascript
复制
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

代码语言:javascript
复制
describe('Testing SQL queries', () => {
    
    it("It should return Bob", () => {
        cy.sqlServer("SELECT 'Bob'").should('eq', 'Bob');
        
});
})

我的版本:

代码语言:javascript
复制
\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

有什么建议吗?我还能提供更多的信息来帮忙吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-09-23 19:51:08

这是cypress-sql-serverCypress v9提供的安装指令。

插件文件 插件可以在您的cypress/plugins/index.js文件中初始化,如下所示。 const sqlServer =需要量(‘cypress-sql-server’);module.exports = (on,config) => { sqlServer.loadDBPlugin(config.db);on(任务,任务);}

翻译成Cypress v10+

代码语言:javascript
复制
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文件。 从‘cypress-sql-server’导入sqlServer;sqlServer.loadDBCommands();

Cypress v10+

只需将此代码移动到support/e2e.js

票数 2
EN

Stack Overflow用户

发布于 2022-09-23 18:03:43

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

如果您只想将它们包含在您的cypress.config.js中,它应该如下所示:

代码语言:javascript
复制
const { defineConfig } = require('cypress')

module.exports = defineConfig({
  e2e: {
    baseUrl: 'http://localhost:1234',
    env: {
      db: {
        // your db values here
      }
    }
  }
})
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73831176

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档