我正在尝试按照 NPM 指南 使用 cypress 连接 SQL 数据库。所有依赖项都与所提到的完全一样,但在运行时
cy.sqlServer('SELECT Email FROM [MyDB].[dbo].[User] WHERE Name ="test"')
我在运行时遇到如下错误。
CypressError:cy.task('sqlServer:execute')失败并出现以下错误:
类型错误:未给出连接配置。
虽然我的 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(我使用的是 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);
}
替代 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)})
})
})
我猜问题出在你的 Cypress.json 中的
"server": "test\\test"
中。它可能应该类似于 "server": "localhost"
、"server": "localhost\\SQLExpress"
或类似的内容。该值应与您在 Microsoft SQL Server Management Studio 的“连接到服务器”对话框中使用的值相同。
我通过将配置移动到 cypress.json 中的 env 来修复它:
{
"env": {
"db": {
"host": "localhost",
"user": "sa",
"password": "xxxx",
"database": "CollateralSystem",
"port": 1433
}
}
}
然后在plugins\index.js中:
module.exports = (on, config) => {
tasks = sqlServer.loadDBPlugin(config.env.db);
on('task', tasks);
};
对于赛普拉斯 10+
如果您收到“sqlServer:execute”错误,那么可能会有所帮助: https://stackoverflow.com/a/77799469/21794594
您错过了退货声明,您应该包含如下内容
module.exports = (on, config) => {
tasks = sqlServer.loadDBPlugin(config.db);
on('task', tasks);
return tasks
}