Tuesday, 27 February 2018

Behavior Driven Development(BDD) Behave with Selenium and Python

In this post, we will look at how to execute tests with Behave BDD, Selenium and Python. Behave is one of the most popular BDD tools which can be integrated with Python. Behave uses Gherkin keywords to create the feature files. Behavior-driven development (or BDD) is an agile software development technique that encourages collaboration between developers, QA and non-technical or business participants in a software project.

First, we will install  Selenium Webdriver and behave BDD framework which is backed up by python.
We will execute our tests with the combination of behave BDD, python and Selenium Webdriver. Behave use feature files to run tests.
We will execute the tests for the login functionality of Facebook. We will create a feature file in Gherkin language with its keywords.
After that, we need to create a simple python steps file which will trigger the Selenium web driver to perform given tasks in feature files.

What is BDD? 

BDD focuses on obtaining a clear understanding of desired software behavior through discussion with stakeholders. It extends TDD by writing test cases in a natural language that non-programmers can read. Behavior-driven developers use their native language in combination with the ubiquitous language of domain-driven design to describe the purpose and benefit of their code. This allows the developers to focus on why the code should be created, rather than the technical details and minimizes translation between the technical language in which the code is written and the domain language is spoken by the business, users, stakeholders, project management, etc.
Why is BDD Testing used ? 
 The practices of BDD include:
  • Establishing the goals of different stakeholders required for a vision to be implemented
  • Drawing out features which will achieve those goals using feature injection
  • Involving stakeholders in the implementation process through outside–in software development
  • Using examples to describe the behavior of the application, or of units of code
  • Automating those examples to provide quick feedback and regression testing.
So let's get started with the installation of Behave and Selenium...

Execute the following command to install Selenium web driver with pip:

$ pip install selenium

For chrome driver use the following link to download it:


after downloading put the chrome driver in  "usr/local/bin"

Execute the following command to install behave with pip:

$pip install behave

To update an already installed behave version, use:

$ pip install -U behave

As an alternative, you can also use easy_install to install behave:

$easy_install behave         # CASE: New installation.

$easy_install -U behave      # CASE: Upgrade behave.

Creating feature file:
Now make a directory called “dir-name”. In that directory create a file called “filename.feature” containing:


Feature: Login feature of facebook

Scenario: check Login functionality

Given Open browser visit application

When the page is loaded fill the  login id and blank password

Then click on login button and verify the page

Creating Step file:
Make a new directory called “dir-name/steps”. In that directory create a file called “file-name.py” containing:


from behave import *
@given('Open application and  visit application')
def step_impl(context):

@when('the page is loaded fill login id and  blank password ')
def step_impl(context):
 assert True is not False

@then('click on login button and verify the page')
def step_impl(context):
    assert context.failed is False

Running behave:
Now go to the feature file directory and run this command:
$ behave


Feature: Login feature of the facebook  #dir-name/steps/file-name.py:1

Scenario: check login functionality       #dir-name/steps/file-name.py:3

Given   Open browser and visit application #dir-name/steps/file-name.py:3

When the page is loaded fill the username and password      #dir-name/steps/file-name.py:7

Then click on login button and verify the page #dir-name/steps/file-name.py:11

1 feature passed, 0 failed, 0 skipped

1 scenario passed, 0 failed, 0 skipped
3 steps passed, 0 failed, 0 skipped, 0 undefined

No comments:

Post a Comment

Amazon EKS - Kubernetes on AWS

By Komal Devgaonkar Amazon Elastic Container Service for Kubernetes (Amazon EKS), which is highly available and scalable AWS service....