- To use Selenium Webdriver for Database Verification you need to use the JDBC ('Java Database Connectivity'). JDBC (Java Database Connectivity) is a SQL level API that allows you to execute SQL statements. It is responsible for the connectivity between the Java and a wide range of databases.
- We have seen how to use some of the user Actions methods provided by the API - clickAndHold(element), moveByOffset(10,0), release. The API provides many such methods. Refer to the link for more details. How to get Tooltip Text in Selenium Webdriver. Let's see the demonstration of accessing and verifying the tool tips in the simple scenario.
- Selenium Webdriver is limited to Testing your applications using Browser. To use Selenium Webdriver for Database Verification you need to use the JDBC ('Java Database Connectivity'). JDBC (Java Database Connectivity) is a SQL level API that allows you to execute SQL statements.
Here, in my blog, I will be discussing database testing using selenium webdriver. Selenium webdriver will simply interact with my browser and do the expected tasks which I have scripted in test script using java. Let’s Discuss database testing in more details. What is Database Testing? Database Testing is the verification of retrieved values.
People Also Ask
Thus, Selenium WebDriver alone is ineligible to perform database testing but this can be done using Java Database Connectivity API (JDBC). The API lets the user connect and interact with the data source and fetch the data with the help of automated queries. To be able to exploit the JDBC API,.... Readmore ››
You can take data using excel or CSV file as well but some company still use the database to take data from the different database. As we already know, Selenium does not support Database Testing but partially we can do using JDBC and ODBC.. Readmore ››
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. In Automation Testing, Selenium is one such tool which helps in providing functionalities to test the database.. Readmore ››
Database Validation Using Selenium Webdriver
In selenium scripts, when there is a need of getting the Data from the database we may have to use APIs which helps to interact with database like JDBC. Java Database Connectivity (JDBC) is a Java API which is used to connect and interact with Database.. Readmore ››
Selenium Webdriver Edge
There are time during the functional automation when you the test engineer have to connect to the database in order to verify the data driven functionality. For that purpose, it is necessary to connect to the database during test execution to verify the functionality that is dependent on the data coming from the database.
Following is the class that is written in Java to connect to the database that:
1. creates the connection to the database
2. execute the query on the database
3. close the database connection when the query is executed.
Pre-requisite:
Download oracle ojdbc14.jar or ojdbc6.jar from the internet and configure it to java-build path in your favorite IDE.
Database connection class
package com;
import java.io.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class OrclConn {
private Connection connection;
private String serverName;
private String portNumber;
private String sid;
private String url;
private String username;
private String password;
private Statement stmt;
public ResultSet rset;
/*
This constructor initializes the database variables that requires to connect to the database
*/
public OrclConn(String serName, String portNum,
String osid, String dbUrl, String dbUserName, String dbPassword ) throws IOException
{
connection = null;
serverName = serName;// pass server name of IP from the calling classs
portNumber = portNum;// pass port number from the calling classs
sid = osid; // pass oracle service name from the calling classs
url = dbUrl + serverName + ':' + portNumber + ':' + sid;// 'jdbc:oracle:thin:@' + serverName + ':' + portNumber + ':' + sid;
username = dbUserName; // pass oracle database name from the calling classs
password = dbPassword; // pass oracle database password (plaintext) from the calling classs
}
public void OpenDBConnection()
{
try
{
// Load the JDBC driver
String driverName = 'oracle.jdbc.driver.OracleDriver';
Class.forName(driverName);
// Create a connection to the database
connection = DriverManager.getConnection(url, username, password);
}
catch (ClassNotFoundException e)
{
System.out.println('Class not found from database' );
e.printStackTrace();
}
catch (SQLException e1)
{
System.out.println('ORACLE Connection error ' );
e1.printStackTrace();
}
}
/* This Function execute the query on the connected database and return the ResultSet collection
Upon callling from the test case class where actual verification is being done on UI and Database.
*/
public ResultSet RunQuery(String Query) throws IOException
{
try{
stmt = connection.createStatement();
rset = stmt.executeQuery(Query);
}
catch(SQLException e1)
{
System.out.println('Query Execution Error' );
e1.printStackTrace();
}
return rset;
}
/* This Function closes the database connection from the Oracle Database Connection
Upon callling from the test case class where actual verification is being done.
*/
public void OracleCloseConnection() throws IOException
{
try{
connection.close();
}
catch(SQLException e1)
{
System.out.println('Query Execution Error' );
e1.printStackTrace();
}
}
}
How do you connect to the database from the Java?
Following is the class that is written in Java to connect to the database that:
1. creates the connection to the database
2. execute the query on the database
3. close the database connection when the query is executed.
Pre-requisite:
Download oracle ojdbc14.jar or ojdbc6.jar from the internet and configure it to java-build path in your favorite IDE.
Database connection class
package com;
import java.io.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class OrclConn {
private Connection connection;
private String serverName;
private String portNumber;
private String sid;
private String url;
private String username;
private String password;
private Statement stmt;
public ResultSet rset;
/*
This constructor initializes the database variables that requires to connect to the database
*/
public OrclConn(String serName, String portNum,
String osid, String dbUrl, String dbUserName, String dbPassword ) throws IOException
{
connection = null;
serverName = serName;// pass server name of IP from the calling classs
portNumber = portNum;// pass port number from the calling classs
sid = osid; // pass oracle service name from the calling classs
url = dbUrl + serverName + ':' + portNumber + ':' + sid;// 'jdbc:oracle:thin:@' + serverName + ':' + portNumber + ':' + sid;
username = dbUserName; // pass oracle database name from the calling classs
password = dbPassword; // pass oracle database password (plaintext) from the calling classs
}
public void OpenDBConnection()
{
try
{
// Load the JDBC driver
String driverName = 'oracle.jdbc.driver.OracleDriver';
Class.forName(driverName);
// Create a connection to the database
connection = DriverManager.getConnection(url, username, password);
}
catch (ClassNotFoundException e)
{
System.out.println('Class not found from database' );
e.printStackTrace();
}
catch (SQLException e1)
{
System.out.println('ORACLE Connection error ' );
e1.printStackTrace();
}
}
/* This Function execute the query on the connected database and return the ResultSet collection
Upon callling from the test case class where actual verification is being done on UI and Database.
*/
public ResultSet RunQuery(String Query) throws IOException
{
try{
stmt = connection.createStatement();
rset = stmt.executeQuery(Query);
}
catch(SQLException e1)
{
System.out.println('Query Execution Error' );
e1.printStackTrace();
}
return rset;
}
/* This Function closes the database connection from the Oracle Database Connection
Upon callling from the test case class where actual verification is being done.
*/
public void OracleCloseConnection() throws IOException
{
try{
connection.close();
}
catch(SQLException e1)
{
System.out.println('Query Execution Error' );
e1.printStackTrace();
}
}
}
How do you connect to the database from the Java?