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?
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')
@when('the page is loaded fill login id and blank password ')
assert True is not False
@then('click on login button and verify the page')
assert context.failed is False
Now go to the feature file directory and run this command:
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