DevLabs Alliance - WhatsApp
DevLabs Alliance Logo

Home / Blogs /How To Automate Dat...

How To Automate Database Testing Using Selenium and JDBC?

John Doe

2024-06-07

DevLabs Alliance Blogs

0 mins read

A database is a software component that provides an efficient way to store data and retrieve it using SQL, a Structured Query Language. Databases hold vital business information and serve as the foundation of a whole application.


Selenium WebDriver has limitations when testing web applications via the browser. You must utilize JDBC (Java Database Connectivity) in Selenium WebDriver for Database Verification.

JDBC


JDBC is a Java Level API that handles database joining and allows us to execute SQL commands. It bridges the gap between the Java programming language and various databases. The JDBC API will include the interfaces and classes listed below:


1. DriverManager

This program is used to keep track of a list of database drivers. This driver recognizes a certain subprotocol under JDBC to establish a database connection.


2. Driver

It is an interface that handles communication with the database server.


3. Connection

An interface containing all the methods needed to connect to a database. The connection object represents the communication context, and the connection object is the only way to communicate with the database.


4. Statement

This interface represents a static SQL statement. You may run a SQL statement and receive the results using the Statement object and its methods. It has methods like execute(), executeBatch(), executeUpdate(), and so forth.


5. ResultSet

This interface represents the database result set, a table formed by running statements. In addition, this interface includes getter and update methods for retrieving and updating its contents.



Step 1


Creating a Database Connection


The syntax for connecting to a database is as follows.


getConnection(URL, "userid", "password")


Here,


The username configured in the database is the userid.


The configured user's password


The URL format is jdbc:dbtype>:


/ipaddress:portnumber/db name”


dbtype>- The database driver to which you are attempting to connect.

This value will be "SQL" to connect to the SQL database.


The URL for connecting to the database named "employee" in MYSQL will be


bejdbc:mysql:/localhost:3036/employee


And the code to establish a connection is as follows:



You must use the code to load the JDBC Driver.



Step 2


Inputting Queries into the Database


Once the connection is established, you must run the queries.

To send inquiries, we use the Statement Object.



Once the statement object is formed, we utilise the executeQuery function to execute the SQL query.




Step 3


Results processing


The ResultSet object stores the results of the run query.


Java has advanced mechanisms for processing the findings.



Example of database testing using selenium


Using Selenium WebDriver and JDBC Step 1: Download and install MYSQL Server and MYSQL Workbench from the URLs below.


MYSQL Server (https://dev.mysql.com/downloads/mysql/)


Workbench for MYSQL: https://dev.mysql.com/downloads/workbench/


Eclipse Setup, JDBC Connection, and Selenium WebDriver:


A driver class is required to build a JDBC connection.

For example, the SQL connector driver class can be obtained following the link.


https://dev.mysql.com/downloads/connector/j/


To configure Eclipse for JDBC connectivity, follow the instructions below:


Step 1: Right-click the selenium project and select 'Build Path' > 'Configure Build Path.'


Step 2: Go to the 'Libraries' tab, click the 'Add External JARs...' button, navigate to the MySQL-connector-java-5.1.39.jar' file, and then click the Open button.



Step 3: Click the Apply and Close button to finish the build path setup, as shown below.



Step 4: Once the driver class is configured, run the test script indicated below.


a) Make a Package (selenium package)


b) Make a Class (SeleniumJDBCConnection)




1. Connect to a database

We connect to the MySQL database after loading the JDBC driver and using ConnectionManager.

As illustrated in the preceding example, this class requires a URL, username, and password.


2. Execute SQL queries in the database using the JDBC connection

We've written a SQL query to pull a record (here, the URL and title) from the database for testing.

We obtain this SQL in a pre-compiled state using the Statement interface and the Connection interface's 'createStatement' method.


3. Process the database's return result set

Here, we use the 'executeQuery' method to run the pre-compiled SQL, which produces a result set with the URL and title.


4. Web testing using WebDriver

Using Firefox WebDriver to load the webpage utilising the URL retrieved from the database. The driver is retrieving the title of the page loaded by the URL.


This title is compared to the title found in the database.


5. Output

Because the title retrieved from the database matches the title retrieved from the website, the test will pass.



Conclusion

Database testing is required to guarantee that the system is functioning correctly. Selenium WebDriver is a tool to automate UI applications and is most commonly utilised by QAs.

Know Our Author

DevLabs Alliance Author Bio

John Doe


DevLabs Alliance TwitterDevLabs Alliance LinkedInDevLabs Alliance Instagram

Author Bio

John Doe is a dedicated Software Development Engineer in Test (SDET) with a strong background in software engineering and quality assurance. John is proficient in a variety of programming languages and testing tools, enabling her to deliver reliable and efficient testing solutions.

INQUIRY

Want To Know More


Email is valid



Phone


By tapping continuing, you agree to our Privacy Policy and Terms & Conditions

“ The hands-on projects helped our team put theory into practice. Thanks to this training, we've achieved seamless collaboration, faster releases, and a more resilient infrastructure. ”
DevLabs Alliance Blogs Page Review
Vijay Saxena

SkillAhead Solutions

DevLabs Alliance Footer section
DevLabs Alliance LinkedIn ProfileDevLabs Alliance Twitter ProfileDevLabs Alliance Facebook ProfileDevLabs Alliance Facebook Profile
DevLabs Alliance Logo

Gurgaon

USA

1603, Capitol Avenue, Suite 413A, 2659, Cheyenne, WY 82001, USA

DevLabs Alliance ISO 9001

DevLabs Alliance Footer SectionDevLabs Alliance Footer SectionDevLabs Alliance Footer SectionDevLabs Alliance Footer SectionDevLabs Alliance Footer SectionDevLabs Alliance Footer SectionDevLabs Alliance Footer SectionDevLabs Alliance Footer SectionDevLabs Alliance Footer Section

`Copyright © DevLabs Alliance. All rights Reserved`

|

Refund & Reschedule Policy

Privacy Policy

Terms of Use