Every application has to maintain a database to store all its data. You may require to connect to database in your test automation code to insert or view the data from the database.
Selenium does not support Database Testing but partially we can do it using JDBC and ODBC. We can connect Java program with database and fetch data and modify the data based on our requirement.
In this post we will discuss Database testing using selenium webdriver using connector.
1) Register driver class
Class.forName("oracle.jdbc.driver.OracleDriver");
2) To establish a connection with the Oracle database
Connection con=DriverManager.getConnection( "jdbc:oracle:thin:@localhost:1521:xe","system","password");
3) To create the Statement object
Statement stmt=con.createStatement();
4) To execute the query and get result
ResultSet rs=stmt.executeQuery("select * from emp");
while(rs.next()){
System.out.println(rs.getInt(1)+" "+rs.getString(2));
}
5) Close connection
con.close();
Example Code :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | public class DBConnectivity { public static void main(String args[]) { try { Class.forName("oracle.jdbc.driver.OracleDriver"); Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "system", "password"); Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery("select * from emp"); while (rs.next()) System.out.println(rs.getInt(1) + " " + rs.getString(2)); con.close(); } catch (Exception e) { System.out.println(e); } } } |
No comments:
Post a Comment