DEV Community

Cover image for How to Connect MySQL Database with Selenium Tests in C#
JigNect Technologies
JigNect Technologies

Posted on

How to Connect MySQL Database with Selenium Tests in C#

Integrating MySQL with Selenium in C# enhances your automation framework by enabling real-time backend validations. This allows test scripts to fetch test data, validate UI elements against database records, and ensure that backend logic is functioning correctly.

Using the MySql.Data.MySqlClient library, testers can establish connections, run SQL queries, and ensure data-driven, dynamic testing. This means instead of relying on static values, tests can retrieve actual usernames and encrypted passwords from the database, verify form submissions, and confirm record updates. The result? More reliable, robust, and production-ready automation.

Why This Matters

Most automation tests focus only on the UI clicking buttons and verifying that elements appear correctly. However, many real-world issues occur beneath the surface in the database or business logic layer. By connecting Selenium to your database, you move beyond superficial validations.

You’re no longer just confirming that a login button was clicked, you’re validating that the user’s credentials exist and were authenticated in the database. Instead of assuming a form submission worked, you query the database to ensure the data was actually saved and is accurate.

This approach transforms your tests into data-driven, real-time, and reliable verifications. It bridges the gap between front-end behavior and back-end integrity, ensuring your software behaves exactly as expected across all layers. If you want robust, production-ready automation, integrating database validation is a must.

Architecture Overview

Below is a high-level architecture diagram that outlines the flow of data between your C# test automation framework, MySQL databases, and the web application.

Mysql Database

Setting Up the Environment

Required NuGet Packages

Install the following packages using NuGet Package Manager in Visual Studio:

  • Selenium.WebDriver
  • Selenium.WebDriver.ChromeDriver
  • MySql.Data (for MySQL connectivity)

Database Setup
We’ll use two MySQL databases:

  • classicmodels: Contains customer information such as names and countries.
  • UserDB: Stores login credentials (email + encrypted password). Example table creation for UserDB:

Image 2

Use MySQL Workbench to create these databases, manage schemas, and insert test data using AES_ENCRYPT() for realistic login scenarios.

Connecting C# Selenium with MySQL

Connection Strings

Define connection strings in your code:

Image 3

Establishing the Connection

Image 4

Using MySqlCommand & Reader

Image 5

Using SQL Data in Selenium Tests

Dynamic Login with DB Credentials

Retrieve user credentials and automate login:

Read The Full Blog Here:- JigNect Technologies

Top comments (1)

Collapse
 
cristea_theodora_6200140b profile image
Theodora Cristea

Very useful post! 👏🏻Thanks for your post!🤗