Validate your Sitecore publishes with Cypress

The problem

It seems obvious but you really want to avoid a content change taking down your website.

No matter how well you code the site, null checking, version checking your items etc.. if a rouge item is published, or key data unpublished then your whole site could grind to a halt.

There are a few solutions to this such as:

  • Workflow. - Of course put all/key content through an approval process. This can be a great solution.
  • Staging publishing target. - This can work nicely alongside workflow, ensure all content is checked on a non public facing publishing target first.

Another option

Another option which can work standalone or alongside the ideas above and also helps mitigate human error, such as content just being approved without being checked is to run some UI tests on publish.

Tests on publish

So the idea is actually rather straightforward.

  • Content editor publishes to stage.
  • The publish action invokes a background Cypress (JavaScript UI test runner) job (http://cypress.io) to run some UI tests on your staging target. These tests can check key journeys of your website are still functional. I.e Homepage is up and displays correct content, the checkout is working as expected.
  • When the user wants to publish to live they see a status of the last test run which gives them confidence in the publish. If the test run failed, then don't publish.

example

How can I achieve this?

There are many ways to build this. In my prototype I combined a node API server with a small update to the publish dialog. I'm sure it can be improved upon but it works fairly nicely.

The node server

https://github.com/daveaftershok/cypress-node-api-windows

Once up and running exposes a number of endpoints to execute Cypress tests. If you don't know how to use Cypress it is great piece of software please try it out.

The publish script

https://github.com/daveaftershok/sitecore-test-run-on-publish

Feel free to edit this code as you wish.

Dave Leigh

Web, and long time Sitecore developer based in Bristol, UK, working at Valtech - valtech.co.uk - @valtech.
I occasionally do other things too.