首页 > 解决方案 > 使用 Cypress 连接到 SQL DB

问题描述

我正在尝试按照NPM 指南使用 cypress 连接 SQL db 。所有依赖项都与提到的完全相同,但在运行时

 cy.sqlServer('SELECT Email FROM [MyDB].[dbo].[User] WHERE Name ="test"')

我在运行时收到如下错误。

CypressError: cy.task('sqlServer:execute') 失败并出现以下错误:

TypeError:没有给出连接配置。

虽然我的 cypress.json 文件有我的数据库连接字符串。

赛普拉斯.json

{
"baseUrl": "myurl",
"db": {
    "userName": "test",
    "password": "test",
    "server": "test\\test",
    "options": {
        "database": "test",
        "encrypt": true,
        "rowCollectionOnRequestCompletion" : true
    }
}    
}

下面是我的 plugins/index.js 文件

  const sqlServer = require('cypress-sql-server');
module.exports = (on, config) => {
  // `on` is used to hook into various events Cypress emits
  // `config` is the resolved Cypress config
  tasks = sqlServer.loadDBPlugin(config.db);
  on('task', tasks);
}

标签: javascriptsqlnpmdatabase-connectioncypress

解决方案


对于任何像我一样寻找如何解决这个问题的人:

出于某种原因,赛普拉斯(我使用的是 3.8.2,我不确定 cypress-sql-server 作者使用的是什么版本)没有看到“db”的自定义属性。

一种方法(您可以通过多种方式执行此操作)是只需要插件文件中的 cypress 配置并从那里引用您的自定义属性。

为此,只需将 plugins/index.js 更改为如下所示:

const sqlServer = require('cypress-sql-server');
const dbConfig = require('../../cypress.json');

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

推荐阅读