Connecting Oracle database in Cypress project
Last month, I’ve learned about how to connect oracle DB with NodeJS for my cypress project. Actually, I got stuck in some problems when I tried to connect at first. In the article, I present to you how to connect to Oracle databases in the cypress project, and how to solve the problems I’ve found. Hope this will be useful for someone who has the same issue as I had.
Pre-requisite
- NodeJs
- Cypress project
- Oracle Instant Client
- Typescript (optional)
Installing node-oracle package
First of all, we have to install oracledb in our project using the below command.
npm install oracledb — save-dev
Adding configuration
We can define the database configurations and node version in cypress.json file.
- By default, Cypress uses the node from its bundle version, but I want to load oracleDB with my system’s Node version (My cypress’s version is 6.8.0. It uses bundled Node.js version 12.18.3, but my local Node.js version is 10.24.0) so I set the value system to my nodeVersion. If I do not set it, I got the below error:
Error: NJS-045: cannot load a node-oracledb binary for Node.js 12.18.3 (darwin x64)
- For database configuration, we can set all properties such as username, password, and connection string in cypress.json file to make it easier for updating the value in the future.
remark: you can verify the bundled Node version using npx cypress version
command
Creating task plugin Event to query data
In this step, we are going to create a task to both connect to the database and query the data. Go to ../plugin/index.js file
- Create event name
sqlQuery
. Inside, it returns the value from the query method:queryData
method withquery
parameter andconfig.env.db
parameter
- Specify Oracle client library path in your local machine at
initOracleClient([Object options]);
. I also got the below timed out when I forgot to specify this line.
- create query method to return the query value with await operator.
- use
connection.execute()
to execute sql statement
Creating the Testcase
We can call the event using cy.task and pass the sql script to query the data from oracle database.
and then try run the script to verify the result.
You also can verify the log in the browser console.
For my example project, you can clone it from the Link
Reference 👧