Selenium tests which extract data from a db

(Andrew) #1

Hi all,

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!


(ernie) #2

Generally, you add a DB library in your test code and interact with the DB, running procs/queries and asserting the values.

Note that you might be able to avoid doing this if there are analogs in the UI/API, e.g. GET endpoints to read the values.

(Viv) #3

Hi Andy,

Depending on which way you want to go there’s a few options…

You could write UI tests using something like selenium to insert rows in to a database and then write some code to query the database to see what’s been inserted. You could create a function to check a table and then check return something from the database which you’d assert i.e. row count for that table if your inserting a new row or check a column on a table for a specific value.

You could also test the database in isolation, inserting dummy data and then checking the data was stored as expected. For example you could have a users table with a field called surname varchar(20) and then your application could be using a stored procedure which inserts in to that table using a parameter which is varchar(18) - this would mean the procedure would truncate surnames over varchar(18) even though that surname field is varchar(20) in the users table. It’s very basic but if you want to test the database specifically you can check out a recent post I wrote of using Pester and dbatools