logo logo

Introduction to Cucumber Framework

Introduction to Cucumber Framework

Cucumber is an automation framework that implements the BDD (Behavior Driven Development) method. Originally, Cucumber framework was written in Ruby language but nowadays supports a variety of programming languages. Cucumber is the equivalent of SpecFlow in the .NET world.

But before we dive deep down into the framework and code, let’s first understand: what does Behavior Driven Development even mean?

BDD is a development methodology that came as a result of another process called TDD (Test Driven Development) – a process mostly from the Agile world (short cycles of development and testing), where the tests are written before writing the product’s code.

The main idea of the​ BDD methodology is to define tests/actions in the product by business processes written by project managers/product managers/analysts/sales/testers or any type of manager that has some sort of technical skills.

What is the Gherkin Language?

In fact, BDD improves the communication between different teams in the organization that are responsible for releasing the best product possible to the customer. By using this methodology, those project managers, for example, can verify that every business process of the product is covered by the development team and the QA team.

In the world of BDD, examples that represent business processes are called Scenarios. The Scenarios are written in a language with a special syntax called Gherkin. The scenarios describe a particular product feature behavior, in certain situations, and given a list of various parameters. This description is also called: Specification. Dan North created the BDD methodology as a follow-up of Unit Testing and the AAA model (Arrange, Act, Assert).

The Gherkin language is actually a type of DSL, built from keywords such as:

  • Given – describes a situation given a particular state (the initial context of the system).
  • When – describes a state in which I perform a certain action.
  • Then – describes the system’s behavior that follows as a result from the same action defined under the When.
  • And – used when you want to concatenate a couple of actions.
  • But – a negative keyword which enables to check non-existence of conditions.

Take a look at this example of a Scenario that tests a coffee vending machine ☕:

Feature: Serve coffee
Coffee should not be served until paid for
Coffee should not be served until the button has been pressed
If there is no coffee left then money should be refunded

Scenario: Buy last coffee

Given there are 1 coffees left in the machine

And I have deposited $1

When I press the coffee button

Then I should be served a coffee

 

When working with BDD there are a couple of additional concepts we need to be familiar with:

  • FeatureFile – the same Spec document that describes the system’s behavior. In this file, we will write the Gherkin language with the Feature, the Scenarios, and with the keywords mentioned above.
  • Step Definitions – a class or collection of classes in which we translate the Gherkin language into code that executes automatic actions on the tested product.

Working with Cucumber Framework

Now that we’ve understood what BDD is, how does Cucumber come into the picture? 🧐 Cucumber is an automation framework that implements the BDD methodology. Let’s first see how to install the framework.

First, you will need to download and install it by downloading the dependencies in Maven:

Cucumber Dependencies in Maven

 

This is how I built the project. Pay attention to the resources library, where I created the FeatureFiles, StepDefinitions, and the runners (that we will talk about shortly):

Cucumber Framework library resources library

 

In our test case, we will enter to the Wikipedia site, and enter in our search the term “Selenium WebDriver“:

Selenium Webdriver Wiki Search

And we will validate that there are no search results for this term

Selenium WebDriver Wiki Search Results

Then the FeatureFile will be written as such:

@RegressionTests
Scenario: WikiSearch
Given user is on wikipedia page
When user search for “Selenium WebDriver”
Then user can not find Reference

And I will implement the StepDefinitions as follows:

@Given("user is on wikipedia page")
public void userWikiPage()
{
  session();
  driver.get("http://wikipedia.org");
}

@When("user search for \"([^\"]*)")
public void userSearch(String searchVlue)
{
  session();
  driver.findElement(By.id("searchInput")).sendKeys(searchVlue);
  driver.findElement(By.xpath("//*[@id=\"search-form\"]/fieldset/button")).click();
}

@Then("user can not find Reference")
public void userNoFindReference()
{
  session();
  assertTrue(driver.findElement(By.className("mw-search-createlink")).getText().contains("does not exist"));	
  driver.quit();
}

This class inherits from another class – baseSteps where I defined the driver and its initialization under the Session function:

public class baseSteps
{
  static WebDriver driver;
  static boolean initFlag = false;

  public void session()
  {
    if(!initFlag)
    {
      System.setProperty("webdriver.chrome.driver", "C:/Projects/Drivers/chromedriver.exe"); 
      driver = new ChromeDriver(); 
      driver.manage().window().maximize();
      driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
      initFlag = true;
    }
  }
}

Advantages of Cucumber Framework:

  • Cucumber supports different languages like Java, Ruby and .net.
  • It is the intermediate between the business and technical language.
  • Nonprogrammers can also involve in the Cucumber framework.
  • Reusability of code.

Conclusion

Cucumber brings with it lots of flexibility and is easy to understand & read using its Gherkin syntax (“Given-When-Then”) written in plain English. Working with Cucumber and BDD contains a lot more topics (which I did not cover here, this was just an intro 😉) like Tags, Data Tables, Arguments, Outline Scenario, Hook Methods, Dependency Injection… Each of these topics has a bunch of online documentation that you can research more about and experiment with to enjoy the full power of Cucumber! 🥒💪

Reference: https://atidcollege.co.il

About the author

Yoni Flenner

A software engineer who likes to test things programmatically, and he’s doing it for a living for the past 10 years: https://atidcollege.co.il

Comments

3 1 comment

Leave a Reply

FacebookLinkedInTwitterEmail