Introduction
Automation testing often requires large amounts of test data such as customer information, product details, employee records, transaction history, and user credentials. While small datasets can be stored in files like Excel, CSV, or JSON, enterprise applications usually store their data in databases.
Database-Driven Testing is a testing approach in which automation scripts retrieve test data directly from a database instead of using external files. This allows Selenium tests to work with real or dynamically maintained data, making the tests more realistic and easier to maintain.
Python provides built-in libraries such as sqlite3 to connect to SQLite databases. Other databases like MySQL, PostgreSQL, Oracle, and SQL Server can also be used with their respective Python connectors.
In this tutorial, you’ll learn what Database-Driven Testing is, why it is used, how Selenium retrieves data from a database, and how to automate a web application using database values.
What is Database-Driven Testing?
Database-Driven Testing is a Data-Driven Testing approach where test data is stored in a database instead of external files.
During execution, the automation framework connects to the database, retrieves the required records using SQL queries, and uses those values as test input.
Typical data stored in databases includes:
Usernames
Passwords
Customer Details
Product Information
Transaction Records
Employee Data
Order Details
Application Settings
Example Database Table:
| ID | Message |
|---|---|
| 1 | DB Message |
| 2 | Selenium Testing |
| 3 | Automation Framework |
The automation framework retrieves the required record before executing the Selenium test.
Why Use Database-Driven Testing?
Database-driven testing provides several advantages:
Uses centralized test data.
Supports large datasets.
Eliminates duplicate test files.
Simplifies test data maintenance.
Supports dynamic data retrieval.
Works well with enterprise applications.
Improves Data-Driven Testing.
Keeps test logic separate from test data.
Common Databases Used in Selenium Automation
Automation frameworks commonly use:
SQLite
MySQL
PostgreSQL
Oracle Database
Microsoft SQL Server
MariaDB
In this example, SQLite is used because it is lightweight, built into Python, and requires no additional server setup.
Example
import sqlite3
from selenium import webdriver
from selenium.webdriver.common.by import By
def test_database_driven_testing(tmp_path):
db_path = tmp_path / "testdata.db"
connection = sqlite3.connect(db_path)
cursor = connection.cursor()
cursor.execute(
"CREATE TABLE messages (id INTEGER PRIMARY KEY, text TEXT)"
)
cursor.execute(
"INSERT INTO messages (text) VALUES (?)",
("DB Message",)
)
connection.commit()
message = cursor.execute(
"SELECT text FROM messages WHERE id = 1"
).fetchone()[0]
connection.close()
driver = webdriver.Chrome()
try:
driver.get(
"https://www.testmuai.com/selenium-playground/simple-form-demo"
)
driver.find_element(
By.ID,
"user-message"
).send_keys(message)
driver.find_element(
By.ID,
"showInput"
).click()
assert driver.find_element(
By.ID,
"message"
).text == message
finally:
driver.quit()
Understanding the Code
Import Required Modules
import sqlite3
from selenium import webdriver
from selenium.webdriver.common.by import By
The required modules are imported.
sqlite3is used to create and access an SQLite database.webdriverlaunches the browser.Byis used to locate web elements.
Create the Database
db_path = tmp_path / "testdata.db"
connection = sqlite3.connect(db_path)
A new SQLite database named testdata.db is created.
Note: This example creates a temporary database during execution for demonstration purposes. In real-world Selenium frameworks, the database already exists and contains application or test data. The automation script simply connects to the existing database and retrieves the required records.
Create a Database Cursor
cursor = connection.cursor()
A database cursor is created.
The cursor is responsible for executing SQL queries.
Create a Table
cursor.execute(
"CREATE TABLE messages (id INTEGER PRIMARY KEY, text TEXT)"
)
A table named messages is created.
The table contains two columns:
id– Primary Keytext– Stores the message
Insert Test Data
cursor.execute(
"INSERT INTO messages (text) VALUES (?)",
("DB Message",)
)
A new record is inserted into the database.
The stored data becomes:
| ID | Text |
|---|---|
| 1 | DB Message |
Save the Changes
connection.commit()
The commit() method permanently saves the inserted data into the database.
Without committing, the inserted record would not be stored.
Read Data from the Database
message = cursor.execute(
"SELECT text FROM messages WHERE id = 1"
).fetchone()[0]
An SQL query retrieves the message whose ID is 1.
The returned value is:
DB Message
This value will be used as input for the Selenium test.
Close the Database Connection
connection.close()
The database connection is closed after retrieving the required data.
Closing unused database connections is considered a good practice.
Launch the Browser
driver = webdriver.Chrome()
A new Chrome browser instance is created.
Open the Application
driver.get(
"https://www.testmuai.com/selenium-playground/simple-form-demo"
)
The browser opens the Selenium Playground Simple Form Demo page.
Enter the Database Value
driver.find_element(
By.ID,
"user-message"
).send_keys(message)
The value retrieved from the database is entered into the message textbox.
Instead of using hard-coded input or external files, Selenium uses the value fetched directly from the database.
Click the Button
driver.find_element(
By.ID,
"showInput"
).click()
The Show Message button is clicked.
The application displays the entered message.
Verify the Result
assert driver.find_element(
By.ID,
"message"
).text == message
The displayed message is compared with the value retrieved from the database.
If both values match, the test passes successfully.
Close the Browser
finally:
driver.quit()
The browser is closed after the test execution.
Practical Example
Suppose an e-commerce website stores thousands of customer records in a database.
Instead of maintaining customer information in Excel files, the automation framework retrieves customer names, email addresses, and shipping details directly from the database before executing order placement or profile update tests.
Automation Testing Example
Consider an online banking application.
Customer login credentials, account numbers, transaction details, and beneficiary information are stored in a database. Before executing transfer or account management tests, the automation framework retrieves the required customer data using SQL queries and uses it during Selenium test execution.
Real-World Example
Database-Driven Testing is widely used in:
Selenium Automation Frameworks
Banking Applications
E-commerce Websites
CRM Systems
ERP Applications
Healthcare Systems
Insurance Applications
Enterprise Web Applications
Typical database records include user credentials, customer information, product details, transaction history, employee records, orders, invoices, and configuration settings.
Advantages of Database-Driven Testing
Supports large datasets.
Uses centralized test data.
Eliminates duplicate data files.
Simplifies data maintenance.
Retrieves real application data.
Supports dynamic testing.
Improves Data-Driven Testing.
Easily integrates with enterprise applications.
Common Mistakes Beginners Make
Creating Test Data Inside Every Test
The example creates a database for learning purposes. In real projects, the automation framework usually connects to an existing database rather than creating a new one for each test.
Forgetting to Close Database Connections
Always close database connections after retrieving the required data to avoid resource leaks.
Writing Incorrect SQL Queries
Ensure that SQL queries retrieve the expected records before using them in Selenium tests.
Mixing Database Logic with Test Logic
Keep database operations in utility classes or helper methods instead of writing SQL code directly inside every test.
Best Practices
Store reusable test data in databases.
Keep SQL queries separate from test logic.
Close database connections after use.
Validate retrieved data before using it.
Use parameterized SQL queries to improve security.
Create reusable database utility classes.
Use existing databases in real automation frameworks instead of creating new ones during every execution.
Conclusion
Database-Driven Testing enables Selenium automation frameworks to retrieve test data directly from databases, making tests more scalable and realistic. By separating test logic from test data and using SQL queries to fetch required information, automation becomes easier to maintain and better suited for enterprise applications. Professional Selenium frameworks commonly integrate with databases to execute reliable, data-driven test scenarios.
Frequently Asked Questions (FAQs)
What is Database-Driven Testing?
Database-Driven Testing is a testing approach where automation scripts retrieve test data directly from a database instead of using external files.
Why is Database-Driven Testing used?
It provides centralized, reusable, and dynamic test data while reducing dependency on Excel, CSV, or JSON files.
Which Python library is used in this example?
The built-in sqlite3 library is used to create and access an SQLite database.
Can Selenium work with databases other than SQLite?
Yes.
Selenium automation frameworks commonly connect to MySQL, PostgreSQL, Oracle, Microsoft SQL Server, MariaDB, and many other databases using their respective Python database connectors.
Is Database-Driven Testing used in professional automation frameworks?
Yes.
Database-Driven Testing is widely used in enterprise Selenium automation frameworks where test data is maintained in centralized databases and retrieved dynamically during test execution.
Key Takeaways
Database-Driven Testingretrieves test data directly from databases.Python’s built-in
sqlite3module allows easy interaction with SQLite databases.SQL queries are used to fetch the required test data.
Database-driven testing improves scalability and maintainability by separating test logic from test data.
Professional Selenium automation frameworks commonly integrate with enterprise databases for dynamic test execution.
