in this Cypress framework, we have made certain useful additions that cypress.io doesn't provide by default. In this article, we will be looking at how we can leverage a connection to an SQL database and use this to validate values stored inside a database table.
pg should get reflected in package.json file at the end of devDependencies.
Connecting to an SQL database
In order to connect to a database, we will need the following connection information for the DB. We will add this information towards the end in cypress.json file inside "env" object.
In this article, we are using a simple "pokemonDB" DB and will make our actions on a "pokemon" table that has the following information related to pokemon creatures:
We will start by adding a new Cypress Task called "DATABASE". For this, we will add a new task in the cypress/plugins/index.js file. This task accepts two main things:
i) dbConfig - (this the DB connection info retrieved from cypress.json file using Cypress.env() ii) sql - (this is the SQL command which we need to execute)
Now in order to execute a simple "SELECT * FROM pokemon" SQL query, we will create a sample feature file called DB_test1.feature
Then on executing this test our code will yield all records of the table and printing them on the console.
In order to make assertions on the values retrieved from DB, it will have to be done inside then block where we get the "result" object: