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)
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)
},
})
}
Step 3: Added the following dependencies in cypress.json
"env" : {
"db": {
"host": "name",
"user": "user",
"password": "password"
}
}
Step 4: Added the following code in the spec.js file
it('Verify the retrieved data', () => {
cy.task('queryDb','select * from soandso where soandso = value').then((resp) => {
console.log(resp.rows)
})
})
But while running the spec file getting the below error
cy.task('queryDb') failed with the following error: > ER_NOT_SUPPORTED_AUTH_MODE: Client does not support authentication protocol requested by server; consider upgrading MySQL client
Please help me to fix this error.
can you check your dependency in the package.json?
It should have sth like below.
"dependencies": { "mysql": "^2.18.1" }
If you do not have this dependency, you need to run 'npm install mysql'
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.