'How to specify environment to point Cypress tests at the correct DB credentials?
Below is the index.js code I am using to connect to a MySQL DB in my cypress test:
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)
}
})
})
}
module.exports = (on, config) => {
on('task', { queryDb: query => { return queryTestDb(query, config) }, });
require('cypress-grep/src/plugin')(config)
return config
}
Currently, my test use the DB credentials provided in cypress.json on this line:
const connection = mysql.createConnection(config.env.db)
But I want the framework to run in different environments, as the database name is different.
I have already created qa.json & staging.json config files that store the DB credentials like so:
qa.json:
{
"extends": "./cypress.json",
"baseUrl": "myUrl",
"env": {
"db": {
"host": "myHost",
"user": "myUser",
"password": "myPassword",
"database": "taltektc_qa"
}
}
}
staging.json:
{
"extends": "./cypress.json",
"baseUrl": "myUrl",
"env": {
"db": {
"host": "myUrl",
"user": "myUser",
"password": "myPassword",
"database": "taltektc_stage"
}
}
}
Here is the command I am currently using to run the tests:
npx cypress open --config-file staging.json
I tried to update my index.js below, but I get a Cypress is not defined error message:
module.exports = (on, config) => {
on('task', { queryDb: query => { return queryTestDb(query, Cypress.config()) }, });
Can someone please tell me what changes are required in my index.js so that I can specify which config file to use when making the DB connection?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
