Intuitive Way of MySQL Database Testing Using Selenium | LeanFT

If you have learned Selenium or LeanFT and want to get your hands dirty on Selenium or LeanFT by creating an automation framework, you must know how to interact with the MySQL database using Selenium and LeanFT because any automation framework is incomplete without MySQL database connectivity.

MySQL Database is used for various purposes in automation testing like storing Test Data, Test Script Details, Test Environment details, user credentials for different environments (like Staging, UAT, Prod), configuration details, and etc.

You can also perform MySQL database testing using Selenium and LeanFT once you establish a connection with the database.

In this, article we will see how to perform the various operation with MySQL Database in Selenium or LeanFT using Java.

So, here we go!

How to Do MySQL Database Testing Using Selenium and LeanFT

MySQL database testing in Selenium and LeanFT requires the following activities:

  • Read data from MySQL Database in Selenium and LeanFT
  • Update record in MySQL Database in Selenium and LeanFT
  • Insert a new record in MySQL Database in Selenium and LeanFT

MySQL Database Dependencies in Java

If you are not using either Maven or Gradle you can download the MySQL Connector jar from http://www.java2s.com/Code/Jar/m/Downloadmysqlconnector5118jar.htm and associate it with your project classpath.

MySQL Database Testing in Selenium and LeanFT

Maven: If you are using Maven in your Java project, you can add the following dependency to your pom.xml file.

Gradle: If you are using the Gradle project, you can add the following dependency to your build.gradle file.

JDBC (Java Database Connectivity)

JDBC stands for Java Database Connectivity. JDBC is a Java API that helps us to connect and execute SQL queries with a wide range of databases using Java Programming language. JDBC provides methods to query, insert and update data in a database.

In this article, I will show you how to connect MySQL databases using Java in Selenium and LeanFT. Having said that, you can connect to any database by just providing the driver class for the required database.

Steps to Connect Java application with the MySQL Database

  • Load the JDBC driver for MySQL DB using Class.forName(“com.mysql.cj.jdbc.Driver”);
  • Connect to Databse using DriverManager.getConnection(dbUrl,userName,password);
    • dbURL= MySQL Database HostName
    • userName = MySQL Database Host user name
    • password  = MySQL Database Host password
  • Create Statement object to fire queries to database using
  • Retrieve data from the result set
  • At the end close all the resources like DB connection, statement, and resultset object

How to Read Data from MySQL Database?

In order to understand how to read data from MySql Database, let’s consider the example of the following Employee table where EmpID is the primary key with auto-increment ID value.

Mysql select data in java Employee Table

The syntax for MySql database host URL is as follows.

jdbc:mysql://<<hostname>>:<<portNumber>>/<<DBName>>

Note: The default port number for MySql Database is 3306 if it has not been changed.

I will connect to my localhost database (mytestdb).For that the URL will be as follows.

jdbc:mysql://localhost:3306/mytestdb

The userName for my database is root and the password is an empty value. So to read data from this Employee table, we can use the following code.

The output of the Java Code will be as follows:

Mysql select data in java output

How to Update MySQL Database Table Using Java?

Currently, the department name against EmpID 104 is ‘IT‘.We will update this department value to ‘Finance‘.You can use the following Java program to update the MySQL database table.

How to Insert a Record into MySQL Database in Java?

As of now, there are five records in the Employee table. We will insert a new record into it. We can use the following java program to insert a new record into the Employee table.

Conclusion

In this article, we have seen how to fetch data from the MySQL database along with the update and insert operation. Hope you would like this article. Please share this article and if you have any queries please write in the comment box.

Recommended Posts

Leave a Reply