Hello.
I need make a mysql connection in cypress, but i have some problems.
I used this simple code (after install npm mysql):
var mysql = require('mysql');
var con = mysql.createConnection({
host: "localhost",
user: "yourusername",
password: "yourpassword"
});
con.connect(function(err) {
if (err) throw err;
console.log("Connected!");
});
And this error was returned :
-- TypeError: Net.createConnection is not a function
Is possible do that in cypress ? There are some alternative ?
Ps.: I used the same code in other project with node.js, and run with sucess.
Help me =))
Since this is backend connection, it cannot be done from the spec.js file which executes in the browser. Use cy.task
to execute your code, see https://on.cypress.io/task
@bahmutov do you have a sample of db conection ?
Read the cy.task link documentation, there are plenty of examples
Sent from my iPhone
On Mar 12, 2019, at 09:26, Victor Campos Silva notifications@github.com wrote:
@bahmutov do you have a sample ?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or mute the thread.
Really @bahmutov ! Run here ! Tks
hello a question can I test my back-end of my application? Thank you
hello
I would like to know if you can help me, of course, it's like entering the backend of my app, let's say; create a product and I want to know if a property of my idproduct object reacted to the new value. Can this be valued with cypress? thanks for your time
@SUSANO-O Check out our community chat, it can be helpful for debugging or answering questions on how to use Cypress.
I recently found this utility called cypress-sql-server
which may be helpful for connecting to an SQL database.
I have not tried this code myself, so I would take care and inspect the code they are executing. I downloaded it and found the following code they're using. https://gist.github.com/jennifer-shehane/f498ce68b46425583fa72c8d945fe7bb
@bahmutov you are really helpful.
Trying to connect to a mysql instance to get test data from with no luck and documentation pointed by you does not help.
Thank you
@jennifer-shehane the plugin pointed does not work. More than this, I found that adding tree structure under cypress.[ENV].json file makes cypress fail with: Cannot set X of undefined
@jennifer-shehane cypress-sql-server does not work, having run the configuration they suggest the error in Cypress is
Error: CypressError: cy.task('sqlServer:execute') failed with the following error:`
TypeError: No connection configuration given
Have seen similar reported on StackOverflow.
@you1anna I've managed to resolve this error by moving db configuration inside "env" part in _cypress.json_.
"env": {
"db": {
"userName": "",
"password": "",
"server": "",
"options": { }
}
}
Aslo you'll need to update _cypress/plugins/index.js_
tasks = sqlServer.loadDBPlugin(config.env.db);
However, it seems that the cypress-sql-server plugin works only with MS SQL db, as it uses Tedious library for connection, so it won't be working with MySQL.
For those with a mysql db the following code works for me using the mysql lib :
in _cypress.json_ add the db details to env object:
"db": {
"host": "your-host",
"user": "your-username",
"password": "your-pwd"
}
in _plugins/index.js_:
const mysql = require('mysql')
function queryTestDb(query, config) {
// creates a new mysql connection using credentials from cypress.json env's
const connection = mysql.createConnection(config.env.db)
// start connection to db
connection.connect()
// exec query + disconnect to db as a Promise
return new Promise((resolve, reject) => {
connection.query(query, (error, results) => {
if (error) reject(error)
else {
connection.end()
// console.log(results)
return resolve(results)
}
})
})
}
module.exports = (on, config) => {
// Usage: cy.task('queryDb', query)
on('task', {
queryDb: query => {
return queryTestDb(query, config)
},
})
}
Hope this helps. If you are still having issue, it might be your connection credentials?
@emereyd well im still having issues and I follow the same steps as you mentioned.
TypeError: Cannot read property 'host' of undefined
at new ConnectionConfig
look like its not able to get the connection data from cypress.json file
@emereyd well im still having issues and I follow the same steps as you mentioned.
TypeError: Cannot read property 'host' of undefined
at new ConnectionConfiglook like its not able to get the connection data from cypress.json file
I had the same problem.
I understand the problem, we need to put the env object like this
"env":{
"db": {
"host": "base",
"user": "test",
"password": "secret",
"database": "there"
}
}
The previous setting is working for me provided by @emereyd, can somebody provide a working example of some assertion using the previous MySQL connection setting in cypress?
cypress.json:
{
"baseUrl": "https://f.dev/",
"pageLoadTimeout": 50000,
"requestTimeout": 50000,
"defaultCommandTimeout": 50000,
"responseTimeout": 50000,
"waitForAnimations": true,
"viewportWidth": 1280,
"viewportHeight": 800,
"chromeWebSecurity": false,
"env": {
"db": {
"host": "127.0.0.2",
"user": "xxx",
"password": "yyy",
"database": "zz"
}
},
"reporter": "cypress-multi-reporters",
"reporterOptions": {
"configFile": "reporterOpts.json"
}
}
cypress/plugins/index.js
const mysql = require('mysql')
function queryTestDb(query, config) {
const connection = mysql.createConnection(config.env.db)
connection.connect()
return new Promise((resolve, reject) => {
connection.query(query, (error, results) => {
if (error) reject(error)
else {
connection.end()
return resolve(results)
}
})
})
}
Code
cy.task('queryDb', 'DELETE FROM `sometable`')
ok, but how do you use it to do an assertion
Like: cy.task('queryDb', 'SELECT FROM sometable
').should(‘be.equal’,”somevalue”);
in the previous config I had some issue but was fixed by doing this: https://stackoverflow.com/questions/50093144/mysql-8-0-client-does-not-support-authentication-protocol-requested-by-server
You need to use .then
(see https://docs.cypress.io/api/commands/then.html#Usage)
to get the results.
Inside the then you can do some assertions.
e.g.
cy.task('queryDb', `YOUR_QUERY_GOES_HERE`)
.then(results => {
expect(results).to.have.lengthOf(1)
})
Was anyone able to connect to DB that uses SSH connection?
I'm getting ERRCONREFUSED every time.
Getting Below Error
CypressError: cy.task('queryDb') failed with the following error:
Error: ER_NO_DB_ERROR: No database selected
at Query.Sequence._packetToError
Used below query
cy.task('queryDb', 'SELECT ename FROM employee').then(results => {
expect(results).to.have.lengthOf(1)
Index.js
/**
* @type {Cypress.PluginConfig}
*/
const mysql = require('mysql')
function queryTestDb(query, config) {
const connection = mysql.createConnection(config.env.db)
connection.connect()
return new Promise((resolve, reject) => {
connection.query(query, (error, results) => {
if (error) reject (error)
else {
connection.end()
console.log(results)
cy.log("connected")
return resolve(results)
}
})
})
}
module.exports = (on, config) => {
on('task', {
queryDb: query => {
return queryTestDb(query, config)
},
})
}
I am using mysql to connect in cypress I used following code in my project:
In my spec file i used these line in It block:
cy.task('queryDb', 'SELECT FROM `bank`').then(res => {
expect(res).to.have.lengthOf(150);
});
I have used following dependencies:
"cypress-sql-server": "^1.0.0",
"tedious": "^8.3.0"
In plugin file i added following code:
const mysql = require('mysql');
function queryTestDb(query, config) {
// creates a new mysql connection using credentials from cypress.json env's
const connection = mysql.createConnection(config.env.db);
// start connection to db
connection.connect();
// exec query + disconnect to db as a Promise
return new Promise((resolve, reject) => {
connection.query(query, (error, results) => {
if (error) reject(error)
else {
connection.end();
// console.log(results)
return resolve(results);
}
});
});
}
module.exports = (on, config) => {
// Usage: cy.task('queryDb', query)
on('task', {
queryDb: query => {
return queryTestDb(query, config);
},
});
};
Cypress.json i used following code;
"env":{
"db":{
"host": "localhost",
"user":"root",
"password":"root",
"database":"donation"
}
}
After executing code i get following error message please help me out in this regards:
The plugins file is missing or invalid.
Your `pluginsFile` is set to `E:\cypress24\cypress\plugins\index.js`, but either the file is missing, it contains a syntax error, or threw an error when required. The `pluginsFile` must be a `.js` or `.coffee` file.
Or you might have renamed the extension of your `pluginsFile` to `.ts`. If that's the case, restart the test runner.
Please fix this, or set `pluginsFile` to `false` if a plugins file is not necessary for your project.
Error: Cannot find module 'mysql'
Require stack:
- E:\cypress24\cypress\plugins\index.js
- C:\Users\shahmed\AppData\Local\Cypress\Cache\4.4.1\Cypress\resources\app\packages\server\lib\plugins\child\run_plugins.js
- C:\Users\shahmed\AppData\Local\Cypress\Cache\4.4.1\Cypress\resources\app\packages\server\lib\plugins\child\index.js
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:798:15)
at Module._load (internal/modules/cjs/loader.js:691:27)
at Module._load (electron/js2c/asar.js:717:26)
at Function.Module._load (electron/js2c/asar.js:717:26)
at Module.require (internal/modules/cjs/loader.js:853:19)
at require (internal/modules/cjs/helpers.js:74:18)
at Object.<anonymous> (E:\cypress24\cypress\plugins\index.js:18:15)
at Module._compile (internal/modules/cjs/loader.js:968:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:986:10)
at Module.load (internal/modules/cjs/loader.js:816:32)
at Module._load (internal/modules/cjs/loader.js:728:14)
at Module._load (electron/js2c/asar.js:717:26)
at Function.Module._load (electron/js2c/asar.js:717:26)
at Module.require (internal/modules/cjs/loader.js:853:19)
at require (internal/modules/cjs/helpers.js:74:18)
at module.exports (C:\Users\shahmed\AppData\Local\Cypress\Cache\4.4.1\Cypress\resources\app\packages\server\lib\plugins\child\run_plugins.js:206:15)
PS E:\cypress24>
@you1anna I've managed to resolve this error by moving db configuration inside "env" part in _cypress.json_.
"env": { "db": { "userName": "", "password": "", "server": "", "options": { } } }
Aslo you'll need to update _cypress/plugins/index.js_
tasks = sqlServer.loadDBPlugin(config.env.db);
However, it seems that the cypress-sql-server plugin works only with MS SQL db, as it uses Tedious library for connection, so it won't be working with MySQL.
I wanted help on full configuration of Microsoft SQL Server Database connection in Cypress...! @jennifer-shehane @you1anna
ou are still having issue, it might be you
Dear, the code is working fine. Can u give me the code for accessing Db for different environment
Hi,
my SSL connection to mysql is not working. Please let me know what i need to add or update to connect to database with SS tunnel below
"env":{
"db":{
"host": "127.0.0.1",
"user": "",
"password": "**",
"database": "charger_db"
}
},
plugin:
const mysql = require('mysql')
function queryTestDb(query, config) {
// creates a new mysql connection using credentials from cypress.json env's
const connection = mysql.createConnection(config.env.db)
// start connection to db
connection.connect()
// exec query + disconnect to db as a Promise
return new Promise((resolve, reject) => {
connection.query(query, (error, results) => {
if (error) reject(error)
else {
connection.end()
// console.log(results)
return resolve(results)
}
})
})
}
module.exports = (on, config) => {
// Usage: cy.task('queryDb', query)
on('task', {
queryDb: query => {
return queryTestDb(query, config)
},
})
}
mySQL:
Leave a comment
Hi,
You have an extra authentication of the SSH keys. Here you need to check
with your DB team.
On Fri, Aug 28, 2020 at 3:48 AM sarita555 notifications@github.com wrote:
Hi,
my SSL connection to mysql is not working. Please let me know what i need
to add or update to connect to database with SS tunnel below"env":{
"db":{
"host": "127.0.0.1",
"user": "",
"password": "**",
"database": "charger_db"}
},
plugin:
const mysql = require('mysql')
function queryTestDb(query, config) {
// creates a new mysql connection using credentials from cypress.json env's
const connection = mysql.createConnection(config.env.db)
// start connection to db
connection.connect()
// exec query + disconnect to db as a Promise
return new Promise((resolve, reject) => {
connection.query(query, (error, results) => {
if (error) reject(error)
else {
connection.end()
// console.log(results)
return resolve(results)
}
})
})
}module.exports = (on, config) => {
// Usage: cy.task('queryDb', query)
on('task', {
queryDb: query => {
return queryTestDb(query, config)
},
})
}mySQL:
[image: image]
https://user-images.githubusercontent.com/7041696/91500511-aa1ab380-e891-11ea-830e-7dc98ff589db.pngLeave a comment
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://github.com/cypress-io/cypress/issues/3689#issuecomment-682219103,
or unsubscribe
https://github.com/notifications/unsubscribe-auth/AHADHP3ZBYOFKUSBTACFEQ3SC3LTHANCNFSM4G5HUXBQ
.
--
Regards
Pranab Kumar Das
Sr. Engineering Manager Testing
M :- +91 9177240265
Thanks,
Sarita
On Aug 31, 2020, at 12:59 AM, Pranab Kumar Das notifications@github.com wrote:
Hi,You have an extra authentication of the SSH keys. Here you need to check
with your DB team.On Fri, Aug 28, 2020 at 3:48 AM sarita555 notifications@github.com wrote:
Hi,
my SSL connection to mysql is not working. Please let me know what i need
to add or update to connect to database with SS tunnel below"env":{
"db":{
"host": "127.0.0.1",
"user": "",
"password": "**",
"database": "charger_db"}
},
plugin:
const mysql = require('mysql')
function queryTestDb(query, config) {
// creates a new mysql connection using credentials from cypress.json env's
const connection = mysql.createConnection(config.env.db)
// start connection to db
connection.connect()
// exec query + disconnect to db as a Promise
return new Promise((resolve, reject) => {
connection.query(query, (error, results) => {
if (error) reject(error)
else {
connection.end()
// console.log(results)
return resolve(results)
}
})
})
}module.exports = (on, config) => {
// Usage: cy.task('queryDb', query)
on('task', {
queryDb: query => {
return queryTestDb(query, config)
},
})
}mySQL:
[image: image]
https://user-images.githubusercontent.com/7041696/91500511-aa1ab380-e891-11ea-830e-7dc98ff589db.pngLeave a comment
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://github.com/cypress-io/cypress/issues/3689#issuecomment-682219103,
or unsubscribe
https://github.com/notifications/unsubscribe-auth/AHADHP3ZBYOFKUSBTACFEQ3SC3LTHANCNFSM4G5HUXBQ
.--
RegardsPranab Kumar Das
Sr. Engineering Manager Testing
M :- +91 9177240265
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub, or unsubscribe.
Hi,
I’m able to connect to MySQL in workbench but I want to write some tests in cypress to validate data and delete data from cypress. Currently not sure how to pass SSL parameters in cypress.lson to address ssl key for connection
Thanks,
Sarita
On Aug 31, 2020, at 12:59 AM, Pranab Kumar Das notifications@github.com wrote:
Hi,You have an extra authentication of the SSH keys. Here you need to check
with your DB team.On Fri, Aug 28, 2020 at 3:48 AM sarita555 notifications@github.com wrote:
Hi,
my SSL connection to mysql is not working. Please let me know what i need
to add or update to connect to database with SS tunnel below"env":{
"db":{
"host": "127.0.0.1",
"user": "",
"password": "**",
"database": "charger_db"}
},
plugin:
const mysql = require('mysql')
function queryTestDb(query, config) {
// creates a new mysql connection using credentials from cypress.json env's
const connection = mysql.createConnection(config.env.db)
// start connection to db
connection.connect()
// exec query + disconnect to db as a Promise
return new Promise((resolve, reject) => {
connection.query(query, (error, results) => {
if (error) reject(error)
else {
connection.end()
// console.log(results)
return resolve(results)
}
})
})
}module.exports = (on, config) => {
// Usage: cy.task('queryDb', query)
on('task', {
queryDb: query => {
return queryTestDb(query, config)
},
})
}mySQL:
[image: image]
https://user-images.githubusercontent.com/7041696/91500511-aa1ab380-e891-11ea-830e-7dc98ff589db.pngLeave a comment
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://github.com/cypress-io/cypress/issues/3689#issuecomment-682219103,
or unsubscribe
https://github.com/notifications/unsubscribe-auth/AHADHP3ZBYOFKUSBTACFEQ3SC3LTHANCNFSM4G5HUXBQ
.--
RegardsPranab Kumar Das
Sr. Engineering Manager Testing
M :- +91 9177240265
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub, or unsubscribe.
Most helpful comment
For those with a mysql db the following code works for me using the mysql lib :
in _cypress.json_ add the db details to env object:
in _plugins/index.js_:
Hope this helps. If you are still having issue, it might be your connection credentials?