Behave is a BDD framework available in Python. It provides a very similar approach to Cucumber.
In this recipe, we will see how to use Behave and Selenium WebDriver to test a fund transfer application.
In Behave, we need to create a features file for the stories under test. These stories are written in the Gherkin language with the Given
, When
, and Then
structures in the Cucumber format. Perform the following steps to create a feature and step definition file with Behave:
fundtransfer
and then create two subfolders, features
and steps
, in the fundtransfer
folder.fundtransfer.feature
, as follows:Feature: Customer Transfer's Fund As a customer, I want to transfer funds so that I can send money to my friends and family Scenario: Valid Payee Given the user is on Fund Transfer Page When he enters Jim as payee name And he enters 100 as amount And he Submits request for Fund Transfer Then ensure the fund transfer is complete with $100 transferred successfully to Jim!! message Scenario: Invalid Payee Given the user is on Fund Transfer Page When he enters Unmesh as payee name And he enters 100 as amount And he Submits request for Fund Transfer Then ensure a transaction failure message Transfer failed!! 'Unmesh' is not registered in your List of Payees is displayed Scenario: Account is overdrawn past the overdraft limit Given the user is on Fund Transfer Page When he enters Tim as payee name And he enters 1000000 as amount And he Submits request for Fund Transfer Then ensure a transaction failure message Transfer failed!! account cannot be overdrawn is displayed
steps
folder with the following code, and name it fundtransfer_steps.py
:from behave import given, when, then @given('the user is on Fund Transfer Page') def step_user_is_on_fund_transfer_page(context): context.driver.get("http:// cookbook.seleniumacademy.com/fundTransfer.html") @when('he enters {name} as payee name') def step_he_enters_payee_name(context, name): context.driver.find_element_by_id("payee").send_keys(name) @when('he enters {amount} as amount') def step_he_enters_amount(context, amount): context.driver.find_element_by_id("amount").send_keys(amount) @when('he Submits request for Fund Transfer') def step_he_enters_amount(context): context.driver.find_element_by_id("transfer").click() @then('ensure the fund transfer is complete with {text} message') def step_ensure_fund_transfer_is_complete(context, text): assert context.driver.find_element_by_id("message").text == text @then('ensure a transaction failure message {text} is displayed') def step_ensure_fund_transfer_is_complete(context, text): assert context.driver.find_element_by_id("message").text == text
Behave
. Create a new file with the following code in the fundtransfer
folder and name it environment.py
:from selenium import webdriver def before_all(context): context.driver = webdriver.Chrome() def after_all(context): context.driver.quit()
fundtransfer
and use the Behave command, as follows:behave
Similar to Cucumber, for Behave we need to write feature files in the Gherkin language. The feature files are then linked to step definitions using the step definition file.
To run these features with Behave, we need a configuration file that provides access to the Selenium WebDriver instance:
from selenium import webdriver def before_all(context): context.driver = webdriver.Chrome() def after_all(context): context.driver.quit()
When Behave runs the features, a default report is generated in the following format:
3.21.46.78