8

After reading so many answers and forums it makes me confused. Some people say:

Selenium webdriver is a browser automation tool and it has nothing to do with the database.

And some other showing JARs and code that how we can connect selenium to the database and we can test database using selenium webdriver.

So I would really like to know if we can do database testing using selenium? If yes then which databases tool it supports like Mysql, SQL server, Oracle...

And if selenium is not suitable for database testing then please recommend to me the best tool for database testing.

4
  • 1
    Selenium is indeed a browser UI tool. Nothing to do with database testing. In this article there are a few tools listed, see section 4.4: agiledata.org/essays/databaseTesting.html
    – FDM
    Commented Apr 10, 2015 at 5:23
  • Are you using Java? Then jdbc is a good option Commented Apr 10, 2015 at 5:24
  • Yes I am using Java for selenium. site which I am testing have database in SQL server. Commented Apr 10, 2015 at 5:48
  • 1
    In a sense you can: when you call UI with Selenium, it calls backend which in turn could call database. So you're testing database, indirectly.
    – dzieciou
    Commented Jan 30, 2021 at 17:33

6 Answers 6

6

Selenium itself is only a framework to drive web-browsers, but you can combine it with some code to check and or manipulate the database the application under test uses.

If you use Selenium with a programming language then you can use the same language in combination with a database library to access and change the database. Supported databases depend on if a library is available for the used programming language, but most databases are supported by most languages.

Often you use a unit-testing framework to drive tests, the unit-test frameworks are used to Arrange (setup) a state, do an Action (with or without Selenium) and Assert the test outcome.

There are two things you might want to test to verify database content with Selenium.

  • After an action trigger in the web-application check the database state is correct
  • With a certain data-set the web-application displays the data correctly in its interface

If you need to test a database without a web-application on top then this is not the framework you are looking for.

5

Selenium Webdriver is a web test automation framework and primarily used for functional testing of front-end part of the application (that piece, that the user sees in his/her browser).

Since this framework is open source and can be relatively easily extended, someone could have already implemented some additional libraries for interacting with the database. But as I've already said, this is not the primary goal of framework.

I'm assuming you don't need to test your database in a very special way so that I would advise you to take a look at Robot Framework, which is also web testing automation framework with great community and short learning curve.

With help of Robot Framework, you can easily combine both "front-end" testing with database testing, since it already has available database libraries, which extend its' functionality and don't require you to write much code.

The very good thing about Robot Framework is that you can to either use existing libraries or write your own in order to extend the framework's functionality in the way you need. Moreover you can do it with Java or Python.

Here are the examples of Robot Framework database libraries:

3

Selenium WebDriver itself cannot be used to test databases, but the language you are using to code your WebDriver tests (eg. C# or Java, etc.) can.

1

What is your goal in the tests you're writing?

If you're testing the database itself (e. g. "Do I get the expected results from running these queries?") then no, Selenium is not what you're looking for.

If you're doing data driven testing of web interfaces (e. g. "Is the user interface in a web browser telling me the same thing the database is?"), then yes, Selenium can help you with that, but it's only a piece of what you need. As others have said, most languages can access most databases. So you would need to do something like the following:

  1. Directly query the database for the data you wish to examine. This is done using the facilities of the language you're using.
  2. Do any manipulation on the data you need to do in order to compare it to what you'll see in the browser.
  3. Use Selenium to control a browser and navigate to the web page where you'll see the data.
  4. Compare the data you're seeing in the browser to the data you extracted from the database.
0

No. Selenium automates browsers. That's it! You can check it on SeleniumHQ Page. Screenshot from the SeleniumHQ page below.

enter image description here

0

I always do mysql database testing in my selenium tests with python and it is quite simple.

import MySQLdb
db = MySQLdb.connect(host="your_host",user="your_user",db="your_db",port=3306,passwd="your_password")

with db: 
    cur = db.cursor()
    cur.execute("select * from table1 limit 1")
    rows = cur.fetchone()
    first_column_content=rows[0]
    print first_column_content

Not the answer you're looking for? Browse other questions tagged or ask your own question.