Saturday, 31 May 2014

Database Testing (MS access) using Selenium

Selenium does not support Database Testing but partially we can do.


Approach- We can connect to Database using ODBC and verify whether data (DB table)is updated or not.

Let see how to connect with Database using ODBC Driver

These are the simple steps to follow

Step 1- Load the driver

Step 2-Create a connection

Step 3- Create a statement

Step 4- Execute your query

Step 5- Store the data in Result set

Step 6- Verify whether data (table) is updated or not

Lets implement the same using Java and DB as Oracle

Precondition- Set the DSN (Data Source Name)

Step to setup DSN-

1- Control Panel > Administrative tool > Click on ODBC

2- Go to user DSN and Click on Add button

3- Select the respective database which you want to connect in this case I selected Oracle 11G

4- Give the name to DSN and Save

Here we go


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.testng.annotations.Test;

public class TestDatabase {



@Test
public void TestVerifyDB(){

try {
                       
                         // This will load the driver
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
System.out.println("Driver Loaded");



    // Specify the path of the database

String dblocation= "C:\\Users\\Desktop\\DB\\FacebookDB1.accdb";    
    // This will connect with Database , getConnection taking three argument
    //  first argument Test_Oracle is the dsn which you can change as per your system,
Connection con=DriverManager.getConnection("jdbc:odbc:AscentAccess;DBQ="+dblocation);            

System.out.println("Connection created");

   // This will create statement
       Statement smt=con.createStatement();
         
       System.out.println("Statement created");

   // Now execute the query, here facebook is the table which I have created in DB
   
ResultSet rs=  smt.executeQuery("Select * from Facebook");

System.out.println("Query Executed");

  // Iterate the resultset now

      while(rs.next()){
 
String uname=    rs.getString("username");
String pass=        rs.getString("password");
String email=      rs.getString("email");

System.out.println("Uname is "+uname+" password is "+pass+" email id is "+email);
 
 }
}
catch (Exception e) {
System.out.println(e.getMessage());
}


}

}

No comments:

Post a Comment