【问题标题】:Connection to SQL DB with Cypress使用 Cypress 连接到 SQL DB
【发布时间】:2019-12-17 06:47:11
【问题描述】:

我正在尝试使用NPM guide 之后的 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);
}

【问题讨论】:

  • 你能分享你的cypress.json和你的插件文件的内容吗?
  • @ZachBloomquist - 已添加到问题中。

标签: javascript sql npm database-connection cypress


【解决方案1】:

cypress-sql-server 的替代品

npm 我很乏味

cipress.json

    {
"db": {
  "userName": "xxxxx",
  "password": "xxx",
  "server": "xxxxxxxx",
  "options": {
    "database": "",
    "encrypt": true,
    "rowCollectionOnRequestCompletion": true
  }
}
  
}

插件/index.js

const Tedious = require('tedious');
const dbConfigSQL = require('../../cypress.json');
module.exports = (on) => {
   on('task', {'sqlServer:execute' : (sql) => {
    const connection = new Tedious.Connection(dbConfigSQL.db);
      return new Promise((res, rej) => {
        connection.on('connect', err => {
          if (err) {
            rej(err);
          }

          const request = new Tedious.Request(sql, function (err, rowCount, rows) {
            return err ? rej(err) : res(rows);
          });

          connection.execSql(request);
        });
      });
    }
  }
  )};

支持/command.js

Cypress.Commands.add('sqlServer', (query) => {
  if (!query) {
    throw new Error('Query must be set');
  }

  cy.task('sqlServer:execute', query).then(response => {
    let result = [];

    const flatten = r => Array.isArray(r) && r.length === 1 ? flatten(r[0]) : r;

    if (response) {
      for (let i in response) {
        result[i] = [];
        for (let c in response[i]) {
          result[i][c] = response[i][c].value;
        }
      }
      result = flatten(result);
    } else {
      result = response;
    }

    return result;
  });
});

集成/示例/sqlServer.spec.js

/// <reference types="cypress" />

context('SQL SERVER', () => {
    
  it('SELECT', () => {

    const sql = `SELECT * FROM DATABASE..TABELA where CAMPO = 1`;

    cy.sqlServer(sql).should(res=>{console.log(res)})


  })

  })

【讨论】:

    【解决方案2】:

    您错过了应包括以下内容的退货声明

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

    【讨论】:

      【解决方案3】:

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

      由于某种原因赛普拉斯(我使用的是 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);
      }
      

      【讨论】:

      • 我的 module.exports 已经有很少的任务了,所以我添加了 ` "sqlServer:execute": () => {` `tasks = sqlServer.loadDBPlugin(dbConfig.db) 而不是 tasks=...; on('task', tasks); 行;` ` on("task", tasks);` ` return tasks;` ` },` 让它工作
      • 在此之后我们如何调用其他类中的其他数据库方法?在sqlServer的帮助下?
      【解决方案4】:

      我想问题出在 Cypress.json 中的 "server": "test\\test" 中。它可能应该类似于"server": "localhost","server": "localhost\\SQLExpress" 或类似的东西。该值应与您在 Microsoft SQL Server Management Studio 的“连接到服务器”对话框中使用的值相同。

      【讨论】:

      • 服务器标签上的值与连接管理工作室时的值相同。
      猜你喜欢
      • 2022-10-21
      • 1970-01-01
      • 2020-09-29
      • 2021-12-17
      • 1970-01-01
      • 1970-01-01
      • 2013-07-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多