I’m now relatively well versed in using Selenium when it comes to UI testing, but there is one area which I’m not too confident on which I haven’t seen covered much in the courses I have taken, and that’s interaction with the database; the things that spring to mind are below:

1) when data is written to the db, how we can check it is correct? For example if I am on Amazon and I buy a product for a certain amount, I’d then want to check that this amount has been written to the db

2) extracting data out of the db to use for testing. Again, using the Amazon example, how would I interact with the db and be able to pull out products to use for testing.

I’ve used SQL for a few years now so I’m comfortable with the syntax, but when it comes to integrating this with automation this is where my knowledge falls down. Any useful pointers/links to tutorials etc would be appreciated to help me overcome this!



Selenium itself can't do that as it is only for interacting with the HTML page.

However your tests can of course make other calls. For example you can establish a database connection and make a query.

How you do this will depend on your language, e.g. Java, Ruby, etc. and/or any tooling you are using such as editor.

Word of caution - we started doing this at one company I worked at previous but quickly abandoned it due to one reason

intermittent failures

The asynchronous nature of the web made intermittent errors (the worst kind, e.g. 1% of the time, worst as impossible to reliably reproduce).

This will often lead you to not check the database in the UI tests but instead make sure that of the three levels of automation of

  • unit
  • integrated
  • UAT UI

you have

integrated tests

that verify database connections and calls. After all your browsser itself doesn't actually call the database, it calls the web server when then handles making that call through application logic on the web/application server(s)

| improve this answer | |

Normally all popular programming languages have their connectivities to relational database (and for noSQL as well).

For example if we're talking about Java (again, the similar conception is implemented for all popular languages) there we can use JDBC interface. All popular vendors of RDBMS provide their drivers which let you to interact with their databases in some common way, using the same interface.

Hence answering your points

1) You can operate with database in the same code where you operate with your UI. For example you can invoke some user actions with Selenium Web Driver and then fetch data from the database

2) Whatever way of feeding your code with test data you use (either your own or using the conception provided by unit-test frameworks) you can introduce the code fetching the data from any database you need and pass it to your tests as an input.

| improve this answer | |

Selenium itself cannot do anything concerning a database. You can integrate your selenium code with a database adaptor for whichever database you are trying to interact with.

Whichever language you are using has a database adapter which will allow you to access the database, provided that you have the correct credentials. For instance, if you are using Java, you could use JDBC. JDBC will let you use raw SQL statements to view and manipulate your database. This however, requires that you have knowledge of your database structure.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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