Database testing with Selenium WebDriver using JDBC

A database is the heart of the application. It should be organized in such a way that there should not be any loss of the information while the exchange between the client and the server. Database testing is something which is taken lightly and ignored by the managers, but, it should not be ignored as it involves the transaction of data. Information in the form of data is very much critical, loss in such information may lead to a loss in business. So, every test manager should involve testing of the database, instead of mainly focussing on GUI based testing.

What is Database Testing?

Database testing is the validation of tables, schemas, triggering of functions, data integration, data masking, and data connectivity between more than one tables. It also involves running and retrieving data from the database by complex queries, checking its performance by giving a high load, ACID validation and Business rule conformity testing.

Recommended Reading: Technique to handle cookies using Selenium WebDriver

Why do we perform database testing?

We perform database testing for-

  • The validation of information stored in the database
  • Testing the display of correct information from the database based on the query
  • Validation of no loss of information
  • Testing of no storage of incomplete steps
  • The validation of no unauthorized access is feasible inside the application and database

Pre-requisites for Database testing with Selenium

You should be ready with the pre-requisites before performing database testing with Selenium project. Those pre-requisites are listed as follows:

Steps to perform database testing with Selenium WebDriver and JDBC

Here are the required steps to integrate the database with Selenium.

Step# 1: Get Database details

You should be ready with the database details like Database URL, Username and Password. Here we need to structure the URL in a specific format and the URL format should be like-

  • URL- databaseType://ipaddress:portnumber/databaseName

Step# 2: Add JDBC connector Jar file

Add JDBC connector Jar file in your project. You can add it through Build path via add as external Jars. See screenshot below:

Database testing mysql connector

Step# 3: Load JDBC driver

Below statement loads MySql JDBC driver.

Class.forName("com.mysql.jdbc.Driver");

Step# 4: Create a connection to the database

Now write below code to create the connection to the database

Connection connection =  DriverManager.getConnection(databaseURL, userName, password);

Step# 5: Create Statement and Send your queries to the database

After establishing the connection with the database, we can run queries and process the results. Use below sample code to create statement and sending queries to the database.

Statement statement = connection.createStatement();

ResultSet result = statement.executeQuery(query);

Step# 6: Close the database connection

After retrieving all the details from the database, we close the connection.

connection.close();

Let’s take a simple scenario in which we fetch URL from the database and put it to the WebDriver’s get URL method. This scenario retrieves data from the database with Selenium WebDriver.

Sample program to implement Database testing with Selenium using JDBC driver

Following the sample program is the implementation of the above scenario. Let’s have a look.

package Test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import  java.sql.ResultSet;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;


public class DatabaseTesting {

	public static void main(String[] args) throws ClassNotFoundException, SQLException {
		
		System.setProperty("webdriver.chrome.driver", "C:\\Selenium\\chromedriver.exe");
		 
		 WebDriver  driver  = new ChromeDriver();
		 
		 //************DatabaseTesting Line*****************//
		 String databaseURL = "jdbc:mysql://localhost:5555/inviul";
		 String userName = "admin";
		 String password = "inviul";
		 String query = "select * from inviul;";
		 
		 //Loading MySql JDBC driver
		 Class.forName("com.mysql.jdbc.Driver");
		 
		 //Database Connection Creation
		 Connection connection =  DriverManager.getConnection(databaseURL, userName, password);
		 
		 //Creating Statement
		 Statement statement = connection.createStatement(); 
		 
		 //Execute Query and store result
		 ResultSet result = statement.executeQuery(query);
		 
		 String URL = result.getString("url");
		 
		 //Closing database connection
		 connection.close();
		 
		 //************End of DatabaseTesting Line*****************//
		 
		 
		 //************Selenium Line Starts*****************//
		  driver.get(URL);
	  	  
	      driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
		  
	  	  driver.manage().window().maximize();
	  	  
	  	  driver.close();
	  	  
	  	  driver.quit();

	}

}

That’s all about Database testing with Selenium. You can post your queries in the comment section below and don’t forget to join our Facebook group for the latest updates in the test automation industry.

Join Inviul fb group

Leave a Reply