There are quite a few snippets and posts about setting up the basics for running Cucumber tests with watir-webdriver or selenium-webdriver. This post describes the way I usually set up a new test project.
I typically use the following directory structure for tests.
features\ support\ step_definitions\ pages\ config\
In the structure above, Cucumber feature files go into the features directory. The feature files can (should) be further organised in subdirectories in a way that best fits the project.
support contains the code starting the browser and other environment related customisations, for example Cucumber hook definitions, custom matcher definitions and utilities related to taking screenshots during tests.
step_definitions folder contains the step definition files mapping scenario steps to code.
pages is a place to store page object related code, which is completely optional but a nice way to make tests easier to maintain.
I usually keep Cucumber configuration in the config directory and other configuration files can be added there too.
The full details of Cucumber hooks can be found on the Wiki page.
Hooks can be based on the execution status or on tags set on scenarios and there are some good use cases for both. I typically use the following three basic hook types in my tests:
# Checking scenario status after the scenario is run After do |scenario| if scenario.failed? puts "Do something that helps debugging test failures" end end # Doing something after every step, for example, checking for errors on page AfterStep do puts "Add a useful action here" end # Doing test setup before a scenario tagged with a specific tag Before('@tag') do puts "Do something to setup the test" end
When having test code in multiple directories outside of the features folder, cucumber needs to be told to require the files first. This can be done with the following command
$ cucumber -r support -r step_definitions -r pages
It gets quickly tedious to add the same parameters on every test run and a better way to do it is to add defaults in the config/cucumber.yml file
default: -r support -r step_definitions -r pages
Having the default configuration defined enables running the tests in a simpler way:
These are some of the basic things I do to make tests more organised and easier to run. I'll go into more detail in future posts.