0

How to work with selenium WebDriver with database? is it possible to do database testing with selenium webdriver with Java? How to connect database with selenium webriver using java.

1
  • Selenium is nothing to do with database work. You connect to the database using whatever programming language you are using, in this instance, Java has extensive API methods to connect to pretty much any type of database. Commented Sep 19, 2013 at 8:28

3 Answers 3

8

I have recently worked with our QA team to evaluate Selenium, and we started off with a bunch of questions such as how can we parametrize our tests using Excel sheets and databases, how can we make assertions on the database, how can we make test suites, keep statistics of test runs, et cetera. And then there's your question. And I think it all comes down to the same:

The Selenium web site starts off by saying: "Selenium automates browsers. That's it. What you do with that power is entirely up to you."

If you're in the Java domain, you may use that power in a unit test, e.g. JUnit. And the only thing that you use Selenium for, from within your unit test, is to drive the browser -- as if you were a human interacting with the browser.

The scenario that you want to test and the data/values that you are using as an input are then no longer a concern of Selenium, but of how you (Java-)code your test. Can you interact with that database from within Java? If so, you can interact with it from within your (JUnit) unit test, and thus use its data/values as part of the test scenario. That you use these data/values to determine your exact test scenario, or as input values for browser input, is of no concern to Selenium.

In other words: instead of this:

JUnit calls Selenium calls database
et cetera

It will rather be:

JUnit calls database
JUnit calls Selenium
et cetera

In yet other words:

  • Does Selenium work with your database? No
  • Does Selenium prevent you from working with your database? No
Sign up to request clarification or add additional context in comments.

Comments

2

Selenium has nothing in its API to connect to a database and perform queries. Selenium is a browser automation tool. If you are using Selenium to test your database, you are doing it wrong!

Does this database testing have anything to do with opening a web application in a browser ? If yes then selenium/WebDriver could be used. If your answer is NO, then WebDriver/Selenium is not the tool you need!.

2 Comments

Thanks, i asked this question, because in one interview , they asked , am i doing selenium with database testing, they where telling it is possible, so i also got confuse, thanks for making me clear.
This is useful to you then click on up arrow.
0
        //Connection URL Syntax: "jdbc:mysql://ipaddress:portnumber/db_name"        
        String dbUrl = "jdbc:mysql://localhost:3036/emp";                   

        //Database Username     
        String username = "username";   

        //Database Password     
        String password = "pwd";                

        //Query to Execute      
        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();            
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.