Hire QA – Specialized in QA Recruitment, Technical Interviews and Testing Solutions

Database Testing using Selenium

Category: Selenium WebDriver

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. It is responsible for the connectivity between the Java Programming language and a wide range of databases. The JDBC API provides the following classes and interfaces

Driver Manager
Driver
Connection
Statement
ResultSet
SQLException

//You also need to load the JDBC Driver using the code
Class.forName("com.mysql.jdbc.Driver");

Connection con = DriverManager.getConnection(dbUrl,username,password);
ex URL: jdbc:mysql://localhost:3036/emp

"//You can use the Statement Object to send queries.
Statement stmt = con.createStatement();                        

//Once the statement object is created use the executeQuery method to execute the SQL queries:
ResultSet rs = stmt.executeQuery(select *  from employee;);

while (rs.next()){
                            String myName = rs.getString(1);                                                                        
                            String myAge = rs.getString(2);                                                                       
                            System. out.println(myName+""  ""+myAge);                
                    }                
                               // closing DB Connection                
                              con.close();

Results from the executed query are stored in the ResultSet Object.
JAVA provides loads of advanced methods to process the results:

  • String getString() – to fetch the string type data from resultset.
  • int getInt() – to fetch the integer type data from resultset
  • double getDouble() – to fetch double type data from resultset
  • Date getDate() – to fetch Date type object from resultset
  • boolean next() – moves to the next record in the resultset
  • boolean previous() – move to the previous record in the resultset
  • boolean first() – moves to the first record in the resultset
  • boolean last() – moves to the last record in the resultset
  • bolean absolute – to move to specific record in the resultset

Leave a Reply

Your email address will not be published. Required fields are marked *