Contact Us

As the industry moves into big data, databases play a significant role in maintaining information and holding the series. Database Validation and verification play an important role to ensure that there are no bugs while capturing the data and storing them.

There are two ways to achieve it either manually or through Automation.

During manual checking, one has to go through each entry that is getting maintained in the database when any transaction happens at the front end. However, since there are several movements on the computer, it gets more difficult, so it takes time to test all the tables one by one. So Selenium is one such method in Automation Software that aims to include functionalities for checking the database which is time-saving and cost-effective.

Selenium WebDriver is limited to using a Browser to check the applications. You need to use the JDBC (Java Database Connectivity) to use Selenium WebDriver for database verification.

JDBC (Java Database Connectivity) is a Database level API for running SQL statements. The communication between Java programming language and a broad variety of databases is responsible for this. The JDBC API contains the classes and interfaces below

  • Driver Manager: It is used to manage a list of database drivers. This driver recognizes a certain sub-protocol under JDBC in order to establish a database Connection
  • Driver: It is an interface that handles the communications with the database server.
  • Connection: It is an interface that consists of all the methods required to connect to a database. The connection object represents communication context wherein the entire communication with the database is through connection object only
  • Statement: interface is used to execute SQL statements against a relational database.
  • Result Set: It is an object which points the cursor to the current row
  • SQL Exception: An exemption that contains details about a mistake in accessing a database or other errors. Each SQLException offers knowledge of different kinds

A high-Level diagram of database testing


database testing using selenium

Steps to establish Database connection

1) Make a connection to the Database To make a connection with the database, the syntax will be
DriverManager.getConnection(URL, "userid", "password" )

Here,

  • Userid is the username configured in the database
  • The password of the configured user
  • URL is of format jdbc:<dbtype>://ipaddress:portnumber/db_name"
  • <dbtype> - The driver for the database you are trying to connect. To connect to the SQL server database this value will be "SQLEXPRESS2016"

 

For connecting to database with name "emp" in SQL Server URL will jdbc:sqlserver://<IPADDRESS>\\SQLEXPRESS2016:<PORTNUMBER>;databaseName=<DBNAME>;

And the end code will look like

Connection con = DriverManager.getConnection(dburl, username, password)

You also need to load the JDBC Driver using the below code

Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver")

 

2) Send Queries to the Database Once connection is made, you need to execute queries.

You can use the Statement Object to send queries.
Statement stmt = con.createStatement();

Once the statement object is created use the executeQuery method to execute the SQL queries
ResultSet rs = stmt.executeQuery(select * from employee;);

 

3) Process the results

Display the results using below code

while (rs.next()) {
String myName = rs.getString(0);
String myAge = rs.getString(1);
System.out.println(myName + " :::: " + myAge); }

4) Close the connection

con.close();

Need Help?

Publication
Understanding Selenium Test Automation Tool
This article uncovers everything there is to know about Selenium, an open-source automated testing tool for web applications.
Understanding Selenium Test Automation Tool image
Publication
Selenium testing with BrowserStack
The article will introduce the reader to Selenium for cross-browser testing.
Essential B2B Marketing Strategies
Publication
How to check the response code using selenium web driver
In the article, the reader will learn about the need for testing and the Selenium Webdriver for automated testing and a brief description of the HTTPS response codes.
How to check the response code using selenium web driver image