Cucumber and Drupal 7
One of our customers required us to implement Cucumber testing in our Drupal project. In this blog post I will share my experiences and help you through the difficult yet satisfying process of cucumber testing. In later blog posts I will talk about the implementation into the CI and maintaining your test suite.
Testing is a significant part of the continuous integration environment (CI). Although Drupal has a very extensive support for unit testing (e.g. SimpleTest), very few documentation exists on using the CMS for acceptance testing. There is drucumber, but it doesn't even come near to what cucumber is capable of.
Cucumber will test your website from an end-user perspective. This way you have reliable testing results and a more efficient CI pipeline.
You could also record your own set of selenium tests using the selenium IDE, but this solution is often prone to defects, such as changes in the UI and even the performance of your local development machine.
In the video below, you'll see what happens if you trigger a cucumber test with selenium. I have put some "sleeps" in the code so that you can see what actually happens.
Cucumber Crash Course
Cucumber takes a human readable test scenario and turns each line into a function using regular expressions. Those functions are then executed. If a test does not return the expected result, then the test scenario fails.
There are two files involved if you're testing a feature:
The feature file
This file has a yaml-like syntax. It contains the human-readable testing scenarios. The syntax used here is Gherkin. The advantage of those files is that anyone can write it and it can be done in any language (including lolspeak).
A great advantage is that this way the lesser "tech-savvy" business analysts can still explain what they want the website to do. An introductory example is shown below:
Feature: Ensure users can create content Scenario: Ensure an administrator can create a page Given an editor logs in When he creates a page Then the page exists in the CMS
The ruby file
Cucumber is a Ruby framework and this has many advantages
- Gems: Ruby has a large set of useful gems (software packages) and cucumber uses a lot of them.
- Readability: Most of the written code is very easy to understand.
- Flexibilty: In ruby, almost everything is an object, even code blocks!
Underneath is an example block of code that will execute the feature file we just created. For each line of each scenario in the feature file there is a method that executes a few lines of code.
Given /^(an|a)* (.)* logs in$/ do |article,role| visit "user/login" fill_in "name", :with => "cucumber_#{role}" fill_in "pass", :with => "cucumber_#{role}" click_button "edit-submit" end When /^he creates a page$/ visit "node/add/page" fill_in "edit-title", :with => "Hello, World!" fill_in "edit-body", :with => "Lorem ipsum dolor sit amet (...)" click_button "edit-submit" end Then /^the page exists in the CMS$/ visit "admin/content/node" page.should have_content("Hello, World!") end
Set up a clean local installation of the latest version of Drupal 7 with the "Standard" installation profile. Clone the module I've created in this repository in your sites/all/modules folder and install it on the site. Follow the guide that I've posted here (Cucumber and Selenium on ubuntu link comes here). Clone the cucumber mini framework outside of your Drupal installation. Then go to the root of that directory in your terminal and type "rake test:run" from inside the directory of this framework. This will trigger Selenium and run the test.
Let's hope everything went smooth! You now ran your first cucumber test. More information on how to test your application you'll find on the project page of capybara or on the rdoc documentation.
Add new comment