Using Behave and Selenium WebDriver in Python

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.

Getting ready

You need to install Behave using the following command:

pip install behave

How to do it...

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:

  1. Create a folder named fundtransfer and then create two subfolders, features and steps, in the fundtransfer folder.
  2. In the feature folder, create a plain text file named 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
  3. Next, we need to create a step definition file for the feature file created earlier. This file maps each step from the feature file to Python method, which then calls the Selenium WebDriver API. Create a new Python script file in the 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
  4. Finally, we need to create a configuration file that provides required support to run the features with 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()
  5. We need to copy all of these files together in the directory named fundtransfer and use the Behave command, as follows:
    behave
    

How it works...

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:

How it works...

See also

  • The Using Cucumber-JVM and Selenium WebDriver in Java for BDD recipe
  • The Using SpecFlow.NET and Selenium WebDriver in .NET for BDD recipe
  • The Using Capybara, Cucumber, and Selenium WedDriver in Ruby recipe
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.190.160.221