1.

How to achieve Database testing in Selenium?

Answer»

We know Selenium WebDriver is a tool to automate User Interface and We could only INTERACT with Browser using Selenium WebDriver.

We may face a situation where we want to get the data from the database or to update or delete the data from the Database.  If we want to automate anything outside the browser, we need to use other tools to achieve the task. For Database connection and work on it, use JDBC  API Driver. 

JDBC API lets the user connect and interact with the Database and fetch the data based on the queries used in the automation SCRIPT. JDBC is a SQL LEVEL API that allows EXECUTING SQL statements. It creates connectivity between Java Programming Language and the database.

To make a connection to the database the syntax is -

DriverManager.getConnection(URL, "userid", "password" )

where the user ID is the username in the database

Password of the configured user

URL format is 'jdbc:< dbtype>://ipaddress:portnumber/db_name'

<dbtype> is the driver for the database to be connected. 

Code to create a connection is -

Connection con = DriverManager.getConnection(dbUrl,username,password);

Code to load JDBC Driver -

Class.forName("com.mysql.jdbc.Driver");

Once connection is set, use Statement Object to send queries -

Statement stmt = con.createStatement();

Once the statement object is created use executeQuery method

stmt.executeQuery(SELECT *  from employee;);


Discussion

No Comment Found