开发者

How can I do Database testing with Selenium?

开发者 https://www.devze.com 2022-12-27 04:29 出处:网络
Does Selenium supports D开发者_如何学编程atabase testing? If yes, how to do it?A browser testing tool is not the right tool for database testing. For that you use a regular unit testing framework, as

Does Selenium supports D开发者_如何学编程atabase testing? If yes, how to do it?


A browser testing tool is not the right tool for database testing. For that you use a regular unit testing framework, as all database access is within your serverside code.

Unless of course your database access is browser based, in which case you have bigger problems than choosing a test framework.


If you want to connect to a database, use DB connection APIs, for example JDBC for Java.


Consider using TestPlan which allows you to combine Web UI testing with custom Java test units. Those test units can then access your DB and use it in the UI scripts.


For database testing youcan make a connection to the database using JDBC driver and then can fetch data or execute sql queries to perform content testing.

For example-
import java.sql.*;

public class FirstExample {
   // JDBC driver name and database URL
   static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
   static final String DB_URL = "jdbc:mysql://localhost/EMP";

   //  Database credentials
   static final String USER = "username";
   static final String PASS = "password";

   public static void main(String[] args) {
   Connection conn = null;
   Statement stmt = null;
   try{
      //STEP 2: Register JDBC driver
      Class.forName("com.mysql.jdbc.Driver");

      //STEP 3: Open a connection
      System.out.println("Connecting to database...");
      conn = DriverManager.getConnection(DB_URL,USER,PASS);

      //STEP 4: Execute a query
      System.out.println("Creating statement...");
      stmt = conn.createStatement();
      String sql;
      sql = "SELECT id, first, last, age FROM Employees";
      ResultSet rs = stmt.executeQuery(sql);

      //STEP 5: Extract data from result set
      while(rs.next()){
         //Retrieve by column name
         int id  = rs.getInt("id");
         int age = rs.getInt("age");
         String first = rs.getString("first");
         String last = rs.getString("last");

         //Display values
         System.out.print("ID: " + id);
         System.out.print(", Age: " + age);
         System.out.print(", First: " + first);
         System.out.println(", Last: " + last);

//Now here you can test wheather the data you have entered through User Interface gets entered into the database
--Write the code for comparision 


      }
      //STEP 6: Clean-up environment
      rs.close();
      stmt.close();
      conn.close();
   }catch(SQLException se){
      //Handle errors for JDBC
      se.printStackTrace();
   }catch(Exception e){
      //Handle errors for Class.forName
      e.printStackTrace();
   }finally{
      //finally block used to close resources
      try{
         if(stmt!=null)
            stmt.close();
      }catch(SQLException se2){
      }
// nothing we can do
      try{
         if(conn!=null)
            conn.close();
      }catch(SQLException se){
         se.printStackTrace();
      }
//end finally try
   }
//end try

}
//end main
}
//end 
0

精彩评论

暂无评论...
验证码 换一张
取 消