1 year ago
#332789
Lahun Akbal
Connecting Cypress to SQL Server Database using Tedious
I'm trying to connect Cypress with a SQL Server database, I installed Tedious in the project, I've already done all the necessary settings (plugins/index.js, support/commands.js & cypress.json), but the task that is executing the query returns timeout (as you can see in the image below) every time.
cypress.json
{
"env": {
"db": {
"server": "server\\devtransaction",
"authentication": {
"type": "default",
"options": {
"username": "username",
"password": "password"
}
},
"options": {
"port": 3300,
"database": "database",
"encrypt": true,
"rowCollectionOnRequestCompletion": true,
"trustServerCertificate": true,
"validateBulkLoadParameters": true
}
}
}
}
plugins/index.js
module.exports = (on, config) =\> {
on('task', {
sqlServer: (sql) =\> {
return execSQL(sql, config.env.db);
},
});
return config;
};
const tedious = require('tedious');
function execSQL(sql, config) {
const conn = new tedious.Connection(config);
return new Promise((res, rej) =\> {
conn.on('connect', (err) =\> {
if (err) {
rej(err);
}
const request = new tedious.Request(sql, function (err, rowCount, rows) {
return err ? rej(err) : res(rows);
});
conn.execSQL(request);
});
});
}
support/commands.js
Cypress.Commands.add('sqlServer', (query) =\> {
if (!query) {
throw new Error('Query must be set');
}
cy.task('sqlServer', 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;
});
});
And I'm using it like this, on my spec:
cy.sqlServer(
`delete from TABLE where NR_CNPJ=${cnpj}`
).then((res) => cy.log(res));
But I get an error:
cy.task('sqlServer') timed out after waiting 60000ms.
and nothing happens!
I would expect a successful SQL Server database connection, so I can setup my tests.
sql-server
database
cypress
ui-automation
tedious
0 Answers
Your Answer