最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Connection to SQL DB with Cypress - Stack Overflow

programmeradmin2浏览0评论

I'm trying to connect with SQL db using cypress followed the NPM guide . All the dependencies are exactly as mentioned but on running this

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

I am getting error as below while running.

CypressError: cy.task('sqlServer:execute') failed with the following error:

TypeError: No connection configuration given.

Although my cypress.json file has got my database connection string.

Cypress.json

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

The below is my plugins/index.js file

  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);
}

I'm trying to connect with SQL db using cypress followed the NPM guide . All the dependencies are exactly as mentioned but on running this

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

I am getting error as below while running.

CypressError: cy.task('sqlServer:execute') failed with the following error:

TypeError: No connection configuration given.

Although my cypress.json file has got my database connection string.

Cypress.json

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

The below is my plugins/index.js file

  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);
}
Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked Aug 9, 2019 at 13:50 PranyPrany 2,1332 gold badges16 silver badges35 bronze badges 2
  • Can you share the contents of your cypress.json and your plugins file? – Zach Bloomquist Commented Aug 9, 2019 at 21:18
  • @ZachBloomquist - Added in the question. – Prany Commented Aug 12, 2019 at 8:25
Add a comment  | 

5 Answers 5

Reset to default 7

To anyone looking for how to solve this like I was:

For some reason Cypress(I am using 3.8.2 and I am not sure what version cypress-sql-server author was using) doesn't see the custom property of 'db'.

One method(you can do this many ways) is to just require the cypress config in the plugins file and reference your custom property from there.

To do this just change the plugins/index.js to look like this:

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

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

alternative to cypress-sql-server

npm i tedious

cipress.json

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

plugins/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);
        });
      });
    }
  }
  )};

support/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;
  });
});

integration/example/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)})


  })

  })

I guess that the problem is in "server": "test\\test" in your Cypress.json. It should probably something like "server": "localhost","server": "localhost\\SQLExpress" or something along those lines. The value should be the same value that you use in "Connect to Server" dialog in Microsoft SQL Server Management Studio.

I fixed it by moving the configuration into env in cypress.json:

{
  "env": {
    "db": {
      "host": "localhost",
      "user": "sa",
      "password": "xxxx",
      "database": "CollateralSystem",
      "port": 1433
    }
  }
}

Then in plugins\index.js:

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

You have missed the return statement which you should include as below

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

评论列表(0)

  1. 暂无评论