How to Integrate Cypress with Database in Azure Pipeline for Test cases Execution
Blog cover about integrating Cypress with Database in Azure Cloud for Test cases Execution. Cypress connects to the azure database
Pre-request
- Azure free account should be set up already
- User already logged into Azure DevOps
Create SQL Database in Azure Cloud
Step 1
Select Create resource from the below screen
After Login we Can see the below screen
![Cypress with Database in Azure Cloud for Test cases Execution](https://mayag.in/qaautomationlabs/wp-content/uploads/2023/09/image-582.png)
Step 2
Click On “Create” under SQL Database
![Run cypress test case in Azure Cloud](https://mayag.in/qaautomationlabs/wp-content/uploads/2023/09/image-580.png)
Below screen opens after clicking on “Create” database
![Cypress with Database in Azure Cloud for Test cases Execution](https://mayag.in/qaautomationlabs/wp-content/uploads/2023/09/image-580.png)
Step 3
Provide database name
![Cypress with Database in Azure Cloud for Test cases Execution](https://mayag.in/qaautomationlabs/wp-content/uploads/2023/09/image-580.png)
Step 4
Click on “Create New” (From the above screen) to create New Server by providing the below details
![](https://mayag.in/qaautomationlabs/wp-content/uploads/2023/09/image-581.png)
Step 5
Create a resource group by clicking on Create new (From below screen)
![Database testing cypress | Cypress with Database in Azure Cloud for Test cases Execution](https://mayag.in/qaautomationlabs/wp-content/uploads/2023/09/image-579.png)
Step 6
After providing the resource group Click on Review +Create “Database creation” is started (See below)
![](https://mayag.in/qaautomationlabs/wp-content/uploads/2023/09/image-578.png)
After a few min deployments is done we can see in the below screen
![](https://mayag.in/qaautomationlabs/wp-content/uploads/2023/09/image-579.png)
On the Home page, we can see the database and resources created
![](https://mayag.in/qaautomationlabs/wp-content/uploads/2023/09/image-583.png)
Step 7
Create Table
Click on Query editor from the below screenshot
![](https://mayag.in/qaautomationlabs/wp-content/uploads/2023/09/image-580.png)
Enter Login/Password
![Cypress with Database in Azure Cloud for Test cases Execution](https://mayag.in/qaautomationlabs/wp-content/uploads/2023/09/image-579.png)
![](https://mayag.in/qaautomationlabs/wp-content/uploads/2023/09/image-576.png)
Create a query with two fields email id, and password (these two fields we will use in login into Cypress)
![Cypress with Database in Azure Cloud for Test cases Execution](https://mayag.in/qaautomationlabs/wp-content/uploads/2023/09/image-577.png)
CREATE TABLE testyou(Login_Id varchar(255),password varchar(255));
Once the table is created we can see it by expanding
Step 8
INSERT data into the table
INSERT INTO testyou (Login_Id,password)VALUES (‘xxxx’,’xxx’);
INSERT INTO testyou (Login_Id,password)VALUES (‘ZZZ’,’ZZZ’);
Table with data created
NOTE: The site we using for testing is http://www.testyou.in/Login.aspx
![](https://qaautomationlabs.com/wp-content/uploads/2022/09/image-12-1024x539.png)
Install Cypress and other Plugin
Step 1
Install Cypress Refer to the link
Step 2
Install Cypress DB pluginnpm install –save-dev cypress-sql-server
Plug-in can be initialized in your cypress/plugins/index.js
file as below.const sqlServer = require(‘cypress-sql-server’);
const dbConfig = require(“../../cypress.json”);module.exports = (on, config) => {
tasks = sqlServer.loadDBPlugin(dbConfig.db);
on(‘task’, tasks);
}
![](https://mayag.in/qaautomationlabs/wp-content/uploads/2023/09/image-578.png)
Step 3
Under support/index.js add the below line
The extension provides multiple sets of commands. You can import the ones you need. Example support/index.js
file.import sqlServer from ‘cypress-sql-server’;
sqlServer.loadDBCommands();
Step 4
Update Your cypress.json
Update cypress.json with the below line”db”: { “userName”: “<userName>”, “password”: “<password>”, “server”: “qatube.database.windows.net”, “options”: { “database”: “QATube”, “encrypt”: true, “rowCollectionOnRequestCompletion” : true } }
Step 5
Create a .spec file to call value from DB (Configure in DB)
![](https://mayag.in/qaautomationlabs/wp-content/uploads/2023/09/image-578.png)
The output of the above run
Here we can see value from the FIRST row is retrieved
![](https://qaautomationlabs.com/wp-content/uploads/2022/09/image-14-1024x366.png)
Step 6
Now we have to use this Data in TEST Case to login into the site
Create a test case login_page.spec.js
![](https://mayag.in/qaautomationlabs/wp-content/uploads/2023/09/image-581.png)
///<reference types =”cypress”/>
describe(“Test login functionality : login into the application and logout “, () => {
let data;
before(() => {
cy.visit(“http://www.testyou.in/Login.aspx”);
cy.sqlServer(“Select *from testyou”).then(function (result) {
data = result;});});
it(“Login Into Application”, () => {
cy.get(“#ctl00_CPHContainer_txtUserLogin”).type(data[0][0]); // Assign data from DB
cy.get(“#ctl00_CPHContainer_txtPassword”).type(data[0][1]); // Assign data from DB
cy.get(“#ctl00_CPHContainer_btnLoginn”).click();});
it(“Logout From Application”, () => {
cy.get(“#ctl00_headerTopStudent_lnkbtnSignout”).click();});
});
The output of the test case run
We can see in logs, Login id and password values are coming from Azure SQL Server DB
![Cypress with Database in Azure Cloud for Test cases Execution](https://mayag.in/qaautomationlabs/wp-content/uploads/2023/09/image-573.png)