Getting Started with Cucumber Behavior Driven Data -Framework

Chaya Thilakumara
Chaya Thilakumara
Published in
6 min readSep 29, 2018

This approach is widely used in Agile projects. Here you don’t have any business requirements to start the development or to start testing your application. The business requirement is in the form of test case. Developer and the tester will access the same document which define the test case.

In traditional waterfall model : Developers will have their own perspective and testers will have their own perspective. Developers developed based on his understanding and tester will test based on his understanding.So their might be a mismatch in two perceptions.

How communication usually happens

Business analyst design requirements for a project and that document will share to the developers as well as to the testers. Business analysts have their terminology in defining their requirement but when it is come to a developer he may understand that in a different way. That is because people have their own perception in defining things.

So what is the solution we have for this !!!!

Use an ubiquitous language : Business Analysts write the requirement in the form of standard template (test case). Language with a common terminology which every body can understand. It is a semi formal language that is shared by all members of a software development team-both software developers and non-technical people.

Common idea between Business Analyst, QA team and Developers

Let’s see how this template look like with very simple example,

Syntax:

In order to Business Outcome (To achieve something)As a (Role)I want to Do this

(In order to/As a/I want to) Bold text are pre-define common text in the template.

Example:

In order to Buy APPL sharesAs a active buyerI want to login to the account, add APPL symbol to the watch-list, enter the number of shares and the price I willing to buy and click on buy button

In writing a test case you have to use a proper syntax.

Syntax for writing TEST CASES:

Given What you need to have, to perform the action : prerequisiteWhen ActionThen Outcome for the user : validation

Example -01:

Given : Account with zero cashWhen: I login to my account, input the number of shares and the price I willing to buy APPL shares, click on buy buttonThen: Throw an error message — Account with zero cash balance

Example -02:

Given : Inactive account with sufficient cashWhen: I try login to my accountThen: Throw error message — You don’t have access as your not eligible to login to your account

Example -03:

Given : Active user with sufficient cashWhen: I login to my account, click on buy button leaving quantity and price field emptyThen: Throw error message — Please enter the quantity

Example -04:

Given : An account with an active user with sufficient cashWhen: I login to my account,click on buy button giving quantity as zeroThen: It should throw error message — Please enter a valid quantity

Advantages of using CUCUMBER:

Let’s automate your BDD scenarios using CUCUMBER framework

  1. Create java project in eclipse
Navigate to file > new >Project
  • Select Java project, click Next
  • Enter the project name and then click Finish

2. Configure cucumber jars into it

Use below links to download cucumber jar files

http://www.java2s.com/Code/Jar/c/Downloadcucumberhtml022jar.htmhttp://www.java2s.com/Code/Jar/c/Downloadcucumberjava111jar.htmhttp://www.java2s.com/Code/Jar/c/Downloadcucumberjunit111jar.htmhttp://www.java2s.com/Code/Jar/h/Downloadhamcrestcore11jar.htmhttp://www.java2s.com/Code/Jar/j/Downloadjchronic026jar.htmhttp://www.java2s.com/Code/Jar/j/Downloadjunit410jar.htmhttp://www.java2s.com/Code/Jar/c/Downloadcucumbercore111jar.htm

Right click on the project. Go to “Properties” > select “Libraries” and Add external jars and cick OK.

Now you have successfully configured jars and your project will work as CUCUMBER framework which can automate your behavior driven scenarios.

3. Create a feature file

First create a package : Right click your project select Package and named the package as “futurefiles”. You have to create a file with a dot extension.

Test.feature file

Right click the package and select “Other”, go to “General” and select “File” and named it as “test.feature” and click Finish button.

Let’s see how we can derived test cases from a scenario :

Scenario 1: Go to “findmyfare” site and login with correct credentials

Given I login to the accountWhen I login to the account providing correct username and the passwordThen  I login to my account successfully

Scenario 2: Go to “findmyfare” site and login with incorrect credentials

Given I login to the accountWhen I login to the account providing incorrect username and the passwordThen Throw an error message — Incorrect UserID or Password

Let’s see how we can tag these scenarios with automation test cases.

When you run your test case cucumber automatically comes to this feature file and it interpret line by line and start automating it.

@tester

Scenario : Go to “findmyfare” site and login with credentials

Given I login to the accountWhen I login to the account providing correct username and the passwordThen I login to my account successfully

Scenario : Go to “findmyfare” site and login with incorrect credentials

Given I login to the accountWhen I login to the account providing incorrect username and the passwordThen Throw error message — Incorrect UserID or Password

Now you have created your feature file with two sample test cases. Now you need to create step definition java file. That means we are tagging each line to a test case.

test.feature.java
login.java
testrunner.java

Create another Package for step definition, then create a java class. (right click the package go to new > class and name that class as “Login” and click on Finish button.

First automation start to read steps in the feature file and then match tags with the step definition file. We can use regular expression which is stand for any sentence . If you use regular expression you can cover lot of scenarios.

4. Initialize TestRunner Script

Create java file (right click “stepdefinition” package, new> class) and you can named it as “testrunner” and click “Finish” button.

Test execution flow
Test results in console panel

A simple example before moving to the selenium example:

Why use Cucumber with Selenium?

Cucumber and Selenium are two popular technologies and cucumber makes easy to read and to understand the application flow.

You can go through the below project to get an idea to combine cucumber with selenium.

testfeature.java
login.java
testrunner.java

I hope that this post was able to assist you to start CUCUMBER. Let’s meet with another exciting article…Till then, Happy Coding!!!!

--

--

Chaya Thilakumara
Chaya Thilakumara

Pursue your passion, and everything else will fall into place.