
As the world is evolving towards big data, database plays a major role in handling the records and maintaining the sequence of it. To ensure that there are no defects while processing the data, Database Testing is essential.
A database is a software subsystem that offers an efficient way to store data and request said data via a structured query language known as SQL. Databases store critical business information, thus functioning as the backbone of an entire application.
In Automation Testing, Selenium is one such free open source tool that helps in providing functionalities to test the database. This blog will explain how one can connect selenium with SQL server for Azure AD account using Azure Active Directory – Multifactor Authentication.
when using Microsoft SQL server Management Studio below is the connect window. We need to pass these info to login into database . Same information we have to pass when connecting database by code.

In your selenium maven project add below dependencies according to your JRE installed on your system :
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>10.2.0.jre8</version>
</dependency>
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>msal4j</artifactId>
<version>1.11.3</version>
</dependency>
use below code to connect with SQL server using azure active directory – MFA:
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
public class SQLUtil
{
private static SQLServerDataSource DataSource;
private static Connection DBConnection;
private static Statement DBStatement;
private static ResultSet DBResultSet;
public static boolean ConnectToDatabase(String AzureEmailID, String sDataBaseName,String sServerName)
{
boolean fn_result = false;
try
{
DataSource = new SQLServerDataSource();
DataSource.setServerName(sServerName);
DataSource.setTrustServerCertificate(true);
DataSource.setAuthentication("ActiveDirectoryInteractive");
DataSource.setUser(AzureEmailID);
DBConnection = DataSource.getConnection();
fn_result = true;
}
catch(Exception e)
{
e.printStackTrace();
}
}
public static boolean createDBStatement()
{
try
{
DBStatement = DBConnection.createStatement();
}
catch(Exception e)
{
e.printStackTrace();
}
}
public static String getDBValue(String ColumnName,String DBQuery)
{
String QueryResult = "";
try
{
DBResultSet = DBStatement.executeQuery(DBQuery);
if(DBResultSet.Next())
{
sQueryResult = DBResultSet.getString(ColumnName);
}
}
catch(Exception e)
{
e.printStackTrace()
}
return sQueryResult;
}
}
A browser will open to Authenticate with an Azure AD identity by using interactive authentication. if you have any query or issue . Please connect with us to help you.
References:
Categories: Selenium