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, 2019 in Selenium by Vinod
1,303 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 16, 2019 by Anvi
• 14,150 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.

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 17, 2019 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,320 points
3,540 views
+1 vote
1 answer

How can I automate the process of adding iPhone to cart in Flipkart using Selenium(java),Page Object Model and TestNG? Also validate if product is added and available in cart?

Hey check this https://www.edureka.co/community/47160/automate-purchase-adding-book-cart-flipkart-using-selenium? It deals with a similar ...READ MORE

answered Jan 13, 2020 in Selenium by Karan
• 19,610 points
8,349 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 15, 2018 in Selenium by Aniket
971 views
0 votes
1 answer

Can anyone explain how an AJAX call can be handled in Selenium webdriver?

Hello Megha, to demonstrate how AJAX calls are ...READ MORE

answered May 9, 2019 in Selenium by Pratibha
• 3,690 points
1,423 views
0 votes
2 answers

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,500 points
13,687 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

enable trusted connection  in internet explorer by ...READ MORE

answered Aug 31, 2020 in Selenium by Sri
• 3,190 points
9,150 views
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,910 points
7,230 views
0 votes
2 answers

How to use such xpath to find web elements

xpath are two types. 1) Absolute XPath:    /html/b ...READ MORE

answered Sep 3, 2020 in Selenium by Sri
• 3,190 points
7,881 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP