An Introduction to BDD Cucumber

An Introduction to BDD Cucumber

BDD stands for “Behavior Driven Development”. And Cucumber is a framework available to write and test the application by using the high level description of the software. We find usage of the BDD approach to test and develop software in projects which follow Agile practices. One of the main benefits of cucumber is that it allows a common collaborative document and platform in which the application and end user behavior is captured. This document can be used by any team member and it will provide all the relevant information. The approach at any given time allows active participation of the stakeholders from different teams of developers, business analysts, testers, end user etc.
Apparatus of Cucumber
The cucumber framework has two important aspects-
– Business aspect
– Technical aspect
The following diagram depicts the representation of the components-

The framework is based on the concept of DRY, which stands for do not repeat yourself. This basically means the duplicate lines of business feature need not be implemented in technical scripts. The business feature of Cucumber is made of feature files, which are written using a DSL [ domain specific language] called as Gherkin, where as the technical files are created using a programming language example Ruby, Java, Python etc.
Gherkin
The feature files of the cucumber framework are written using the language gherkin.The behavior of the system and the user is captured in these feature files. Components of the Gherkin language are as follows-
– Feature
– Background
– Scenario
– Scenario Outline
– Given
– When
– Then
– Examples
A sample gherkin feature file example is as follows –

Traditional Test case vs BDD Scenario
There is a paradigm shift in the way a traditional test case is written vs the BDD scenario. To understand it, let us take a sample scenario of logging into our demosite application. The URL of the application is – http://5elementslearning.com/demosite/index.php

Traditional Test case vs BDD Scenario
We will write down steps for the manual test case for login into the application.
Testcase name Steps Data
Login Logout 1. Open the application
2. Click on My Account link
3. Type username
4. Type password
5. Click sign in button
6. Verify that log off link is available on the page
7. Click Log off link
8. Click continue button
9. Verify “Welcome” is available on the page user@user.com
USER123

The BDD Scenario for the above will be as follows
@login
Feature: Login Logout to the osCommerece Application
Scenario: Login the application and then logout
Given I am on the osCommerce homepage
When I will click on the “My Account” link
When I will type the username “user@user.com”
When I will type the password “USER123”
When I will click on the sign in button
Then I should see “Log Off” link on page
When I will click on the “Log Off” link
When I will click on the Continue button
Then I should see “Welcome” on the home page

Corresponding to the above feature file, we will have a step definition file in which the steps for automation of the above scenario will be implemented. The step definition file for the above scenario is as follows

require 'capybara/cucumber'
Capybara.default_driver = :selenium

Given(/^I am on the osCommerce homepage$/) do
visit 'http://www.carguruji.com/shop/'
end

When(/^I will click on the "([^"]*)" link$/) do |inp|
click_link(inp)
end

When(/^I will type the username "([^"]*)"$/) do |uname|
fill_in 'email_address', :with => uname
end

When(/^I will type the password "([^"]*)"$/) do |pwd|
fill_in 'password', :with => pwd
end

When(/^I will click on the sign in button$/) do
click_button("Sign In")
end

Then(/^I should see "([^"]*)" link on page$/) do |arg1|
#save_and_open_page
find_link(arg1).visible?
end

When(/^I will click on the Log Off link$/) do
click_link("Log Off")
end

When(/^I will click on the Continue button$/) do
click_link("Continue")
end

Then(/^I should see "([^"]*)" on the home page$/) do |arg1|
expect(page).to have_content arg1
end

The above program shows a working implementation of the BDD Cucumber scenario with Ruby as a programming language for the step definition file. Similarly we can have a step definition file available in other programming languages as well. If you are interested in taking our BDD Cucumber course with Ruby and Java, feel free to drop us an email on – info@5elementslearning.com

 

No comments

Leave a reply

Your email is never published nor shared. Required fields are marked *