Skip to content

Instantly share code, notes, and snippets.

@jennifer-shehane
Created August 8, 2019 07:29
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save jennifer-shehane/f498ce68b46425583fa72c8d945fe7bb to your computer and use it in GitHub Desktop.
Save jennifer-shehane/f498ce68b46425583fa72c8d945fe7bb to your computer and use it in GitHub Desktop.
module.exports = function() {
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;
});
});
}
const Tedious = require('tedious');
module.exports = (dbConfig) => {
return {
'sqlServer:execute': (sql) => {
const connection = new Tedious.Connection(dbConfig);
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);
});
});
}
}
};
@shahmed-nisum-com
Copy link

@ jennifer-shehane Could you please provide mysql code with cypress example with verification db.

@jamilhf
Copy link

jamilhf commented May 12, 2020

can you please post sample from spec file as well

@Sarute
Copy link

Sarute commented Dec 2, 2020

How is your plugin/index.js configured in order to make "sqlServer:execute" of cypress-plugins-db.js called?

@lhimo
Copy link

lhimo commented Feb 15, 2021

Would it be possible to do the same with a DB2 connection? This is working:

    var ibmdb = require('ibm_db');

    ibmdb.open(dbConfig, function (err, conn) {
        if (err) return console.log(err);
        conn.query(query, function (err, data) {
            if (err) console.log(err);
            else console.log(data);
            conn.close(function () {
                console.log('done');
            });
        });
    });

but I would like to do as task/command in order to call it when I need it.

@sindhuakn
Copy link

I use Cypress with Typescript. Will it be possible to call cy.sqlserver in a typescript class file. Could you provide the guidance?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment