0% found this document useful (0 votes)
25 views

Database Testing with Selenium WebDriver Java

The document provides a comprehensive guide on database testing using Selenium 4 with Java, detailing the setup of a local SQL database, creation of tables, and fetching data records. It includes step-by-step instructions for establishing a JDBC connection and executing SQL queries to verify user actions performed through a frontend interface. Additionally, it outlines necessary dependencies for a Maven project and offers example code for testing user creation in the database.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Database Testing with Selenium WebDriver Java

The document provides a comprehensive guide on database testing using Selenium 4 with Java, detailing the setup of a local SQL database, creation of tables, and fetching data records. It includes step-by-step instructions for establishing a JDBC connection and executing SQL queries to verify user actions performed through a frontend interface. Additionally, it outlines necessary dependencies for a Maven project and offers example code for testing user creation in the database.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Database Testing with

Selenium 4 (Java)

(Automation QA/SDET)

Prepared by :-

Kushal Parikh
Checkout my Topmate : https://topmate.io/kushalparikh11/

Table of Contents
1) Setup SQL Database in your local .............................................................................3
2) How to create your own SQL Database table for testing purpose ? .......................... 10
3) How to fetch your SQL Data records ? ................................................................... 14
4) Database testing with Selenium – JDBC Connection................................................. 15

Follow me on LinkedIn : https://www.linkedin.com/in/kushalparikh11/


Checkout my Topmate : https://topmate.io/kushalparikh11/

1) Setup SQL Database in your local


1. Download link - https://dev.mysql.com/downloads/installer/
2. Click on “Download ”

Follow me on LinkedIn : https://www.linkedin.com/in/kushalparikh11/


Checkout my Topmate : https://topmate.io/kushalparikh11/

3. Click on “No thanks, just start my download.” Link

Wait till download is completed….

4. Once download is done, click on downloaded installer & choose the


“Custom” & click next button

Follow me on LinkedIn : https://www.linkedin.com/in/kushalparikh11/


Checkout my Topmate : https://topmate.io/kushalparikh11/

5. Now select the highlighted products & click on next button & then click
on Execute button

Follow me on LinkedIn : https://www.linkedin.com/in/kushalparikh11/


Checkout my Topmate : https://topmate.io/kushalparikh11/

6. Click on next buttons until below screen comes :

Follow me on LinkedIn : https://www.linkedin.com/in/kushalparikh11/


Checkout my Topmate : https://topmate.io/kushalparikh11/

7. Type your password & click on next button

8. Click on Execute button

Follow me on LinkedIn : https://www.linkedin.com/in/kushalparikh11/


Checkout my Topmate : https://topmate.io/kushalparikh11/

9. Click on Finish button

10. Now finish all further steps – click on Finish button & below screen
will be open

Follow me on LinkedIn : https://www.linkedin.com/in/kushalparikh11/


Checkout my Topmate : https://topmate.io/kushalparikh11/

11. Click on highlighted section & enter your password used during
setup and click on OK button

12. My SQL Workbench will be open

Follow me on LinkedIn : https://www.linkedin.com/in/kushalparikh11/


Checkout my Topmate : https://topmate.io/kushalparikh11/

2) How to create your own SQL Database table for testing purpose ?

1. Click on “scheme” icon

2. Enter your scheme name “Any Name You want” & click on Apply button
Follow me on LinkedIn : https://www.linkedin.com/in/kushalparikh11/
Checkout my Topmate : https://topmate.io/kushalparikh11/

3. Click on Apply button again & click on Finish button

4. Click “Scheme” tab & your scheme is created successfully


Follow me on LinkedIn : https://www.linkedin.com/in/kushalparikh11/
Checkout my Topmate : https://topmate.io/kushalparikh11/

5. Now we will create our records for testing purpose – click on SQL
Query Tab

6. Copy paste the below code in query section

USE qa_demo;
Follow me on LinkedIn : https://www.linkedin.com/in/kushalparikh11/
Checkout my Topmate : https://topmate.io/kushalparikh11/

CREATE TABLE AutomationTesters (


tester_id INT AUTO_INCREMENT PRIMARY KEY,
full_name VARCHAR(100) NOT NULL,
email VARCHAR(100) UNIQUE NOT NULL,
experience_years INT CHECK (experience_years >= 0),
skillset TEXT,
company_name VARCHAR(100) NOT NULL,
job_title VARCHAR(50) DEFAULT 'Automation Tester',
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

7. Press Ctrl + A & click on “execute” icon

8. Your table has been created successfully

9. Now we will add our test records with SQL queries & then later we will
try do add new records using Selenium with JDBC connections
Follow me on LinkedIn : https://www.linkedin.com/in/kushalparikh11/
Checkout my Topmate : https://topmate.io/kushalparikh11/

Use below SQL query to add new records & click on execute icon

USE qa_demo;

INSERT INTO AutomationTesters (full_name, email, experience_years, skillset, company_name,


job_title)
VALUES
('Alice Brown', 'alice.brown@example.com', 4, 'Selenium, Java, Cucumber', 'TCS', 'Senior Automation
Tester'),
('David Wilson', 'david.wilson@example.com', 6, 'Python, Cypress, API Testing', 'Swiggy', 'Lead QA
Engineer'),
('Emma Johnson', 'emma.johnson@example.com', 2, 'Selenium, Jenkins, API Testing', 'Deloitte',
'Automation Tester'),
('Michael Smith', 'michael.smith@example.com', 5, 'Java, Selenium, TestNG', 'Google', 'Automation
Engineer');

Your records are created successfully!!

3) How to fetch your SQL Data records ?

Follow me on LinkedIn : https://www.linkedin.com/in/kushalparikh11/


Checkout my Topmate : https://topmate.io/kushalparikh11/

Using Select Query as below :

SELECT * FROM AutomationTesters;

4) Database testing with Selenium – JDBC Connection

Note:

Selenium is not required for Database testing but what we actually do in real
time scenarios . Suppose requirement is –

1) we need to create user from frontend and need to verify in database that
user is created successfully or not in database
2) Delete user from using delete account functionality & verify in database if
user is deleted or not
3) Update user info from using update account functionality & verify in
database if user info is updated or not.

SO DON’T CONFUSE WITH SELENIUM IS REQUIRED FOR DATABASE TESTING

1) Prerequisites Steps :

Follow me on LinkedIn : https://www.linkedin.com/in/kushalparikh11/


Checkout my Topmate : https://topmate.io/kushalparikh11/

- Open any IDE , Create Maven Project & add required dependencies as below

Follow me on LinkedIn : https://www.linkedin.com/in/kushalparikh11/


Checkout my Topmate : https://topmate.io/kushalparikh11/

Follow me on LinkedIn : https://www.linkedin.com/in/kushalparikh11/


Checkout my Topmate : https://topmate.io/kushalparikh11/

Add below dependencies in POM File :-

<dependencies>

<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->


<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.29.0</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.testng/testng -->


<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.11.0</version>
</dependency>

<!-- https://mvnrepository.com/artifact/com.mysql/mysql-connector-j -->


<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>9.2.0</version>
</dependency>

</dependencies>

Follow me on LinkedIn : https://www.linkedin.com/in/kushalparikh11/


Checkout my Topmate : https://topmate.io/kushalparikh11/

2) Test Scenarios :
- Create a new user (automation tester user) from frontend using Selenium (Java)
- Verify user is created or not by fetching the data from SQL database server

Code :-

import org.testng.annotations.Test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class DatabaseTesting {

@Test public void CreateUser_Test(){

System.out.println("Creating User using Selenium from frontend....");

System.out.println("-----------------------------------------");

System.out.println("Fetching Data to verify if user is created successfully or not ...");

// Database connection details


String url = "jdbc:mysql://localhost:3306/qa_demo"; // Update DB name if different
String user = "root"; // Change if different
String password = "Kush@123"; // Change if different

// SQL Query to fetch all records


String query = "SELECT * FROM AutomationTesters";

// Establish Connection
try (Connection con = DriverManager.getConnection(url, user, password);
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query)) {

// Display Column Headers


System.out.printf("%-5s %-20s %-30s %-5s %-40s %-20s %-25s %-20s%n",
"ID", "Full Name", "Email", "Exp", "Skillset", "Company", "Job Title", "Created At");
System.out.println("---------------------------------------------------------------------------------------------
--------------------------------");

// Iterate over the result set


while (rs.next()) {
int id = rs.getInt("tester_id");
String fullName = rs.getString("full_name");
String email = rs.getString("email");

Follow me on LinkedIn : https://www.linkedin.com/in/kushalparikh11/


Checkout my Topmate : https://topmate.io/kushalparikh11/

int experience = rs.getInt("experience_years");


String skillset = rs.getString("skillset");
String company = rs.getString("company_name");
String jobTitle = rs.getString("job_title");
String createdAt = rs.getString("created_at");

// Print each row


System.out.printf("%-5d %-20s %-30s %-5d %-40s %-20s %-25s %-20s%n",
id, fullName, email, experience, skillset, company, jobTitle, createdAt);
}

} catch (Exception e) {
e.printStackTrace();
}
}

Output :-

Follow me on LinkedIn : https://www.linkedin.com/in/kushalparikh11/

You might also like