-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Can i make a mysql connection in cypress ? #3689
Comments
Since this is backend connection, it cannot be done from the spec.js file which executes in the browser. Use |
@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 ***@***.***> 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 |
@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 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. @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: |
@jennifer-shehane cypress-sql-server does not work, having run the configuration they suggest the error in Cypress is
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.
look 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 |
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 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? |
Getting Below Error
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:
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:
|
I wanted help on full configuration of Microsoft SQL Server Database connection in Cypress...! @jennifer-shehane @you1anna |
Dear, the code is working fine. Can u give me the code for accessing Db for different environment |
Hi,
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 ***@***.***> 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.png>
Leave a comment
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#3689 (comment)>,
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 ***@***.***> 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 ***@***.***> 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.png>
>
> Leave a comment
>
> —
> You are receiving this because you commented.
> Reply to this email directly, view it on GitHub
> <#3689 (comment)>,
> or unsubscribe
> <https://github.com/notifications/unsubscribe-auth/AHADHP3ZBYOFKUSBTACFEQ3SC3LTHANCNFSM4G5HUXBQ>
> .
>
--
Regards
Pranab 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 ***@***.***> 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 ***@***.***> 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.png>
>
> Leave a comment
>
> —
> You are receiving this because you commented.
> Reply to this email directly, view it on GitHub
> <#3689 (comment)>,
> or unsubscribe
> <https://github.com/notifications/unsubscribe-auth/AHADHP3ZBYOFKUSBTACFEQ3SC3LTHANCNFSM4G5HUXBQ>
> .
>
--
Regards
Pranab 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.
|
I was trying to connect to Mysql DB from cypress, did the following steps for the same. Step 1: Added MySQL library in cypress Step 2: Added following code in plugins/index.js file (This I have got while searching in google)
Step 3: Added the following dependencies in cypress.json
Step 4: Added the following code in the spec.js file
But while running the spec file getting the below error
Please help me to fix this error. Thanks in advance. @jennifer-shehane |
You may want to try asking our community in our GitHub Discussions. As an open source project with a small maintainer team we have to focus our time on bugs and features in the product, which Issues are reserved for. |
Hi, spec command: CypresError: cy.task('queryDB') failed with the following error: The task 'queryDB' was not handled in the plugins file. The following tasks are registered: queryDb Fix this in your plugins file here: Note: Using the connection info in cypress.json ( will not show the values here since it's proprietory), I was able to connect to our staging db using node's command line interface. |
@chaz2k Are you still having issues with this? |
Hi @emereyd Thank you very much for checking, I actually got the the queryDB working. Cheers! |
Hello.
I need make a mysql connection in cypress, but i have some problems.
I used this simple code (after install npm mysql):
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 =))
The text was updated successfully, but these errors were encountered: