Skip to Content

UI5ers Buzz #06: Using OPA5 with Gherkin

Have you ever heard of Behavior Driven Development (BDD)? This is a technique for synchronizing your requirements documentation and automated tests. It will help you reduce your development and testing effort and focus on testing what offers the most business value. SAPUI5’s Gherkin library allows you to do BDD, and is based off of a project called Cucumber.

How does Gherkin work?

You write a plain-text Feature file that describes the business process you are trying to support. Let’s say that we are building an army of Linux-based bodyguard robots with mounted laser turrets. The requirements document for the factory control program might look like this:

Feature: build Open Source bodyguard robots

    The bodyguard robots will crush your enemies with libre software.
    Ah, the delicious irony.

  Scenario: build one more bodyguard robot
    Given that my bodyguard robot factory is fully operational
    When I click on the "Build one robot" button 1 time
    Then 1 new bodyguard robot is constructed

As you can see, the Feature file is written in plain English. The scenario captures a single concrete requirement that can be tested and automated (and full automation will be required if you want to conquer the bodyguard robot market).

“But,” you say, “computers can’t understand English.” This is true. Linux-based bodyguard robots are very bright (in the sense that their laser turrets output 12 million candelas), but not smart enough to understand verbal commands–yet. Instead, to implement automated testing we will need to write a JavaScript Steps file that interprets the plain English of the Feature file, and captures the tests that we want to execute. For example:

this.register(/^that my bodyguard robot factory is fully operational$/i, function() {
  oOpa5.iStartMyAppInAFrame(getResourcePath());
  Opa5.assert.ok(
    this.factory.isFullyOperational(),
    "Verified that robot factory is fully operational"
  );
  this.iInitialRobotCount = getRobotCount()
});

this.register(/^I click on the "(.*?)" button (\d+) times?$/i, function(sButtonName, sNumTimes) {
  var iNumTimes = parseInt(sNumTimes, 10);
  for (var i=0; i<iNumTimes; ++i) {
    oOpa5.waitFor({
      id: sButtonName,
      actions: new Press()
    });
  }
});

this.register(/^(\d+) new bodyguard robots? (?:is|are) constructed$/i, function(sNumNew) {
  oOpa5.waitFor({
    id: "bodyguard-robot-collection",
    success: function(oObj) {
      Opa5.assert.strictEqual(
        oObj.getRobotCount(),
        this.iInitialRobotCount + parseInt(sNumNew, 10),
        "Verified that we have the correct number of bodyguard robots"
      );
    }.bind(this)
  });
});

Then at test execution time (not robot execution time, that’s what happens when a robot misbehaves), Gherkin will stitch together the Feature file and Steps file and run the result. If the test result is green like a cucumber, then all is well. If the test results are ruby red like a laser then (assuming that you haven’t been vaporized) you have work to do!


Sample of what Gherkin looks like at runtime

In the future, if your requirements change then you can update your Feature file. Gherkin will helpfully require you to change your tests too to keep them up to date with the changes. Your requirements and tests will stay synchronized, and your tests will focus on providing business value.

More to Explore

Afterword

The next post of our UI5ers Buzz blog series will cover the Theme Toolbox.

How are we doing? Let us know by leaving a comment here or contacting us in the slack channel.

Previous Post: UI5ers Buzz #05

Talk to you soon,

Jonathan Benn

Jonathan is a Developer and User Interface Designer working at SAP. He likes making computers more accessible to people, and promoting improved software engineering techniques. On evenings and weekends, Jonathan moonlights as a robopsychologist at U.S. Robots and Mechanical Men, Inc.

 

To report this post you need to login first.

2 Comments

You must be Logged on to comment or reply to a post.

Leave a Reply