Get started with BDD and Test Automation in 5 minutes using Docker and Selenium


Photo by Shane Aldendorff on Unsplash
10 September 2018

Behaviour Driven Development (or BDD) ensures everyone has the same understanding of how an application should work and gives you a clear indication when a feature is finished. In general, any test-automation dramatically speeds up acceptance and regression testing.

Plus, it feels like magic.

Unfortunately, getting started can be tough. You need a browser, a testing framework like Cucumber, a web driver like Selenium, and something like Capybara to glue it all together (which requires Ruby).

For more advanced BDD, check out things like the Java-based Serenity framework which uses the popular Screenplay Pattern. For something simple to get you started, read on…

Get started in 5 minutes

Here’s a quick and easy way to set up a BDD environment using Docker and standalone Selenium containers. In the video below we create a browser container, connect to it with VNC so we can see what’s happening, then create another container to run Ruby/Capybara and then we test Google to make sure that we see the word “dog” when we search for “puppies”.


Step by step

Here’s a text-based summary of the video above. It assumes you have docker installed, you’ve logged in to docker hub at least once and you have a basic knowledge of BDD.

To begin, clone this repository from a terminal like so:

git clone git@github.com:peterkappus/selenium-cucumber-docker.git

Now move into the directory you just cloned…

cd selenium-cucumber-docker

Start the two containers (one for running ruby, and one with the browser). We specify that we want a bash session (so we can run cucumber interactively), and this container specifies the browser container as a dependency so it will start automagically.

docker-compose run ruby bash

Now you can see into the browser container by using VNC to connect:

open vnc://:secret@0.0.0.0:5900

This should open a VNC viewer and show you an empty Ubuntu desktop.

Returning to the original terminal window, we should have a bash session where we can start cucumber:

cucumber

Looking at the VNC window, we’ll see a browser open and perform a Google search, and then close. In the Ruby container, we can see the tests running and see the results.

We can now extend this by writing tests in our “feature” folder and step definitions in our “features/step_definitions” folder. The repo above includes a few generic step definitions for seeing things and clicking on things. You’ll no doubt want to write your own but this will get you started.

Take a look at the capybara documentation and this cheat sheet for help writing step definitions.

When you’ve finished testing, you can exit the ruby container like so:

exit

And finally stop both containers by running this from the host:

docker-compose down

I hope this helps you to get started exploring test automation and BDD and makes it easier to standardise your test environment among your development team.

Let me know how it works for you!

Thanks for reading! I'm always looking to raise my game and meet like-minded people so if you have questions, ideas, or suggestions, please contact me.