Can anyone explain the process of database testing from Selenium Webdriver using JDBC?

0 votes
Can anyone explain the process of database testing from Selenium Webdriver using JDBC?
May 15 in Selenium by Vinod
90 views

2 answers to this question.

0 votes

Hi Vinod, you can follow these steps to do Database Testing in Selenium Webdriver using JDBC:

1) Make a connection to the Database: In order to make a connection to the database the syntax is

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

Here,

  • Userid is the username configured in the database

  • Password of the configured user

  • URL is of format jdbc:< dbtype>://ipaddress:portnumber/db_name", so for connecting to database with name "emp" in MYSQL, URL will be jdbc:mysql://localhost:3036/emp

And the code to create connection looks like:

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

You also need to load the JDBC Driver using this line of code:

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

2) Send Queries to the Database: Once connection is made, you need to Execute Queries and for that you can use the Statement Object:

Statement stmt = con.createStatement();			

Once the statement object is created use the executeQuery method to execute the SQL queries:

stmt.executeQuery(select *  from employee;);

3) Process the results: Results from the executed query are stored in the ResultSet Object. Java provides lots of advance methods to process the results. Few of the methods are getString(), getInt(), getDate(), first(), last(), next(), previous() etc.

Following is a sample code snippet to test database using selenium:

Package  testDatabase;		
import  java.sql.Connection;		
import  java.sql.Statement;		
import  java.sql.ResultSet;		
import  java.sql.DriverManager;		
import  java.sql.SQLException;		
public class  SQLConnector {				
    	public static void  main(String[] args) throws  ClassNotFoundException, SQLException {      			String dbUrl = "jdbc:mysql://localhost:3036/emp";							String username = "root";	
		String password = "password";
		String query = "select *  from employee;";	
                
         	    //Load mysql jdbc driver		
           	    Class.forName("com.mysql.jdbc.Driver");			
           
           		//Create Connection to DB		
            	Connection con = DriverManager.getConnection(dbUrl,username,password);
          
          		//Create Statement Object		
        	   Statement stmt = con.createStatement();					
       
       			// Execute the SQL Query. Store results in ResultSet		
         		ResultSet rs= stmt.executeQuery(query);							
         
         		// While Loop to iterate through all data and print results		
				while (rs.next()){
			        		String myName = rs.getString(1);								        
                            String myAge = rs.getString(2);					                               
                            System. out.println(myName+"  "+myAge);		
                    }		
      			 // closing DB Connection		
      			con.close();			
		}
}
answered May 15 by Anvi
• 13,960 points
+1 vote

Why would you be testing the database from Selenium Webdriver?  Selenium Webdriver is a tool for driving a web browser (drag and drop, clicks etc), via code.   If you are looking to test JDBC, I suggest reading up on JDBC (java database connectivity), perhaps read up on the under lying database you are using.  For example if its Oracle, I found this link over on stack overflow near the top of the heap: https://stackoverflow.com/questions/18756113/how-to-test-connection-to-oracle-database-using-java.  

Webdriver provides a lot of power out of the box, but I'm still amazed that some people are trying to jury rig database, or api testing (of non Webdriver APIs), into their Test Frameworks.    In both of these cases you are better off trying to test via the same language code (presumably Java), rather than going through Webdriver, which is unnecessary for a step that hits a database.

answered May 16 by Veretax
@Veretax I am writing a complete automation project which includes testing of my app as well as testing the database connectivity. For the same I wanted to do both the testings by using Selenium Webdriver, so I don't want to create a Java code separately for testing my database connection. Can you suggest some other automation tool which can do both for me?

Related Questions In Selenium

0 votes
1 answer

How can I download the *.jar file from http:// seleniumhq.org using selenium WebDriver?

For Selenium Standalone Server use this: profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/java-archive"); and ...READ MORE

answered Apr 9, 2018 in Selenium by Martin
• 4,280 points
715 views
+1 vote
1 answer

Can someone explain the meaning of WebDriver wd = new ChromeDriver();?

WebDriver webdriver = new ChromeDriver(); The following simply ...READ MORE

answered Dec 14, 2018 in Selenium by Aniket
87 views
0 votes
1 answer
0 votes
1 answer

Can someone please explain the Selenium Webdriver Architecture ?

Hey Keshav, there are four basic components ...READ MORE

answered May 13 in Selenium by Pratibha
• 3,680 points
112 views
0 votes
1 answer

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,430 points
1,196 views
0 votes
1 answer
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,510 points
1,281 views
0 votes
1 answer

How to use such xpath to find web elements

It's much simpler to use: #MyDiv123 > div.super or ...READ MORE

answered Jun 14, 2018 in Selenium by Samarpit
• 5,510 points
1,038 views