import * as db from "@dankieu/cypress-sql";
export default defineConfig({
e2e: {
setupNodeEvents(on, config) {
// Connect to SQL Server
db.sqlServer(on);
// Connect to Oracle
db.sqlOracle(on);
// Connect to MySQL
db.sqlMySql(on);
// Connect to PostgreSQL
db.sqlPg(on);
},
},
});
const { defineConfig } = require("cypress");
const db = require("@dankieu/cypress-sql");
module.exports = defineConfig({
e2e: {
setupNodeEvents(on, config) {
// Implement node event listeners here
db.sqlServer(on);
return config;
},
},
});
The @dankieu/cypress-sql
package supports the following database connections:
- SQL Server:
db.sqlServer(on)
- Oracle Database:
db.sqlOracle(on)
- MySQL:
db.sqlMySql(on)
- PostgreSQL:
db.sqlPg(on)
Each of these methods establishes a connection to the respective database type when Cypress tests are running.
In your cypress/support/e2e.ts
file, simply import the @dankieu/cmd
package. This will automatically add the custom SQL commands to Cypress.
import "@dankieu/cmd"; // Import @dankieu/cmd to initialize custom SQL commands
- Test: Fetch data from a SQL Server database.
- Connection Config:
{ "user": "SA", "password": "Password789", "server": "localhost", "port": 1433, "database": "master", "options": { "encrypt": false, "trustServerCertificate": true } }
- SQL Query:
SELECT * FROM sys.tables;
- Custom Commands:
- Using
cy.task('sqlServer')
:cy.task("sqlServer", { connectConfig: config, sqlQuery: sql }).then(results => { console.log("Query Results:SQL Server", results[0]); });
- Using custom
cy.sqlServer()
command:cy.sqlServer(config, sql).then(results => { console.log("Query Results Custom CMD:SQL Server", results[0]); });
- Using
- Test: Fetch data from an Oracle database.
- Connection Config:
{ "user": "hr", "password": "hr", "connectString": "localhost:1521/orcl" }
- SQL Query:
SELECT employee_id, first_name, last_name, salary FROM employees;
- Custom Commands:
- Using
cy.task('sqlOracle')
:cy.task("sqlOracle", { connectConfig: config, sqlQuery: sql }).then(results => { console.log("Query Results: Oracle", results[0]); });
- Using custom
cy.sqlOracle()
command:cy.sqlOracle(config, sql).then(results => { console.log("Query Results Custom CMD:SQL Oracle", results[0]); });
- Using
- Test: Fetch data from a MySQL database.
- Connection Config:
{ "host": "localhost", "user": "root", "password": "college", "database": "employees" }
- SQL Query:
SELECT dept_no, dept_name FROM employees.departments;
- Custom Commands:
- Using
cy.task('sqlMySql')
:cy.task("sqlMySql", { connectConfig: dbConfig, sqlQuery: sql }).then(results => { console.log("Query Results: MySQL", results[0]); });
- Using custom
cy.sqlMySql()
command:cy.sqlMySql(dbConfig, sql).then(results => { console.log("Query Results Custom CMD:SQL MySQL", results[0]); });
- Using
- Test: Fetch data from a PostgreSQL database.
- Connection Config:
{ "user": "world", "password": "world123", "host": "localhost", "port": 5432, "database": "world-db" }
- SQL Query:
SELECT code, continent, region, surface_area, indep_year, population, life_expectancy, gnp, gnp_old, local_name, government_form, head_of_state, capital, code2 FROM public.country;
- Custom Commands:
- Using
cy.task('sqlPg')
:cy.task("sqlPg", { connectConfig: dbConfig, sqlQuery: sql }).then(results => { console.log("Query Results: Pg", results[0]); });
- Using custom
cy.sqlPg()
command:cy.sqlPg(dbConfig, sql).then(results => { console.log("Query Results Custom CMD:SQL Pg", results[0]); });
- Using
Here is the formatted version of the data:
The data returned is in JSON format, structured as an array of objects.
{
{ "emp_id": 100, "first_name": "Steven", "last_name": "King", "salary": 24000 },
{ "emp_id": 101, "first_name": "Neena", "last_name": "Yang", "salary": 17000 },
{ "emp_id": 102, "first_name": "Lex", "last_name": "Garcia", "salary": 17000 },
{ "emp_id": 103, "first_name": "Alexander", "last_name": "James", "salary": 9000 },
{ "emp_id": 104, "first_name": "Bruce", "last_name": "Miller", "salary": 6000 }
}
This is the structured data in JSON format.
