In my current client’s EPiServer 9 website project we are using Selenium to run automated UI tests on our code. For this we keep an EPiServer content database backup which we restore before each execution of the test suite. This ensures that any alterations to the content made by the tests are reset before each run.

In previous projects we’ve kept a somewhat fresh dump of the production database for this purpose, together with a [Selenium] node in the page tree for specially set up content. Of course there are both pros and cons with this approach; for instance, while we get production like data to run the tests on, the backup may take longer time to restore and if the content is changed between backups it would require us to repair broken tests.

Working with a template EPiServer content database for automated testing

In this project we’ve tried a slightly different approach. We set up a very lightweight EPiServer template database backup file, containing only the most important settings. Then we had our test setup fill the database with code generated EPiServer pages, blocks and other content. While we in this way didn’t get the pros of having production like data for the tests, we got the benefit of being in total control of our test content.

So, we set up a test area in a separate project in our Visual Studio solution; a project that would not be built nor deployed for the Production build type. In this area we have a DatabaseController with actions to manipulate the database. For instance, RestoreDBs which restores the bak file and also gives us some timing output. I’ve replaced constructor injection with the ServiceLocator to make things more clear.

Important here is apart from the call to our restore method itself, the line containing EPiServer’s IContentCacheRemover. It is vital that we clear EPiServer’s page cache between database restores. Otherwise the Save method will believe that pages already exists while they actually do not (since we just restored a blank database). Without the cache clear, we would get URLs like /somepage2/some-other-page2/ while expecting /somepage/some-other-page/; making the tests go 404.

The actual restore of the backup file is rather simple. It could probably be refactored into something prettier, but here’s what we use.

The next step in our Selenium test setup phase is to generate all of the test data that our tests will need. I won’t go into detail on how to do this since there are already so many articles on how to create EPiServer pages and blocks from code. Just use EPiServer’s ServiceLocator, or even better, constructor injection to retrieve an instance of IContentRepository and start creating your test data. It may be a good idea to put some thought into how you’d like to structure this; keeping everything in the same place may be messy once you start to have more and more content.

Easy way to get your EPiServer content back locally

It’s often a good thing to keep a somewhat production like EPiServer database with reliable content while developing new features and functionality. If you run your UI tests locally using this template database method, you will quickly find yourself missing lots of valuable data. For this purpose it may be a good idea to keep a content rich EPiServer database backup file as well, and create a different action that restores it to the SQL server. So, while surfing to /Database/RestoreDB/ restores the template database, surfing to /Database/ContentRichDB/ restores your content.