With any website or app, you usually do some amount of testing, wether that be from a QA team or just giving your app a once over. Testing can greatly improve the quality of an app and ensure a smoother launch of a product. After all the more eyes on something the more likely it is you’ll find a problem before a potential user does.

However testing can be time consuming, expensive and even boring. You can only run through the login process so many times before you wonder if there is a better way or if it’s even worth the time.

To answer these rhetorical questions, testing is worth the time, and there is a better way. For much of your testing, you can automate the process using Cucumber.

cucumber

What is Cucumber?

Cucumber is a Behavior Driven Development tool used to automate repetitive test actions taken by the user. Cucumber uses the browser to complete user actions, such as clicking submitting a form and entering text. It does so by finding elements by their classes, ids, and values (ect). Once found, it then completes a given action on that element.

Cucumber runs through tests on a step by step basis, where if a single step in a feature fails, it will skip the remaining steps in that feature but then continue on with the remaining tests. This lets you quickly run through repetitive tests without worrying if some of them fail.

While we will go over these in a little more detail later, here is an example of a basic feature file and it’s matching step definition file.

Feature: Search the Web

  As a human
  I want to test cucumber
  So I can work

  @watch
  Scenario: Learn this
    Given I have visited Google
    When I search for "cucumber.io"
    And I click on the first result
    Then I see "cucumber.io"

And here is the matching step definition file. Each step in the above feature has a matching step definition which Cucumber runs when it sees the step in the feature file.

module.exports = function() {
  this.Given(/^I have visited Google$/, function () {
    client.url('http://blog.capsulecat.com/');
  });

  this.When(/^I search for "([^"]*)"$/, function (searchTerm) {
    client.setValue('input[name="q"]', searchTerm);
    client.keys([U+E006]);
    client.keys(['Enter']);
    client.pause(3000);
  });

  this.When(/^I click on the first result$/, function () {
    client.keys('Return');
    client.pause(3000);
  });

  this.Then(/^I see "([^"]*)"$/, function (link) {
    var currentURL = client.getUrl();
    if(currentURL != 'http://blog.capsulecat.com/') {
      console.log('failed');
    }
  });
};

Now before you go looking at Cucumber’s site for more information, know their documentation is terrible. Chimp JS actually has a nice tutorial on both Cucumber and Chimp, which is a great tool that I’d recommend using along with Cucumber.

Installing Cucumber

Cucumber is relatively easy to install as it is on npm. So if you have both Node.js and NPM, you can install a global version of Cucumber JS by running this command in your terminal:

npm install -g cucumber

If you want a tool to test your apps with, but do not know or are not a fan of Javascript, Cucumber supports and can be written in many other languages, however we will only be going over Javascript.

Cucumber is not the be all end all for automation testing, like with most things programing, there are always other methods and tools you could use. However it is easy to learn, fun to do and it has built in documentation features that makes it maintainable.

If you’re interested in QA and want to advance your technical knowledge using tools that pertain to QA, this is a great technology to learn and use.