BDD with Behave: Gherkin Features and Step Definitions

Selenium 16 min min read Updated: Nov 16, 2026 Intermediate
BDD with Behave: Gherkin Features and Step Definitions
Intermediate Topic 1 of 2

What Is BDD?

Behavior-Driven Development describes tests in plain English that business people, testers and developers can all read. The language is called Gherkin. The sentences are then connected to Selenium code.

Where it helps: When non-programmers (product owners, manual testers) must read and review the tests. If only developers read your tests, plain pytest is simpler.
bash pip install behave selenium

Project Structure

text features/ login.feature environment.py steps/ login_steps.py pages/ login_page.py

A Feature File

gherkin # features/login.feature @login Feature: Login to the shop As a customer I want to log in So that I can see the products Background: Given I am on the login page @smoke Scenario: Successful login When I log in with "standard_user" and "secret_sauce" Then I should see the products page Scenario: Locked out user When I log in with "locked_out_user" and "secret_sauce" Then I should see the error "locked out" Scenario Outline: Invalid logins When I log in with "<user>" and "<password>" Then I should see the error "<message>" Examples: | user | password | message | | standard_user | wrong | do not match | | | | Username is required | | standard_user | | Password is required |
KeywordMeaning
FeatureThe functionality being tested
ScenarioOne example/test
GivenStarting situation
WhenThe action
ThenThe expected outcome
And / ButContinue the previous step type
BackgroundSteps repeated before every scenario
Scenario Outline + ExamplesSame scenario with a data table
@tagLabel for filtering

Step Definitions

python # features/steps/login_steps.py from behave import given, when, then from selenium.webdriver.common.by import By @given("I am on the login page") def step_open_login(context): context.driver.get("https://www.saucedemo.com") @when('I log in with "{username}" and "{password}"') def step_login(context, username, password): context.driver.find_element(By.ID, "user-name").send_keys(username) context.driver.find_element(By.ID, "password").send_keys(password) context.driver.find_element(By.ID, "login-button").click() @then("I should see the products page") def step_products(context): title = context.driver.find_element(By.CLASS_NAME, "title").text assert title == "Products", f"Expected Products but saw {title}" @then('I should see the error "{message}"') def step_error(context, message): error = context.driver.find_element(By.CSS_SELECTOR, "[data-test='error']").text assert message in error, f"'{message}' not in '{error}'"

Hooks: environment.py

python # features/environment.py from selenium import webdriver def before_scenario(context, scenario): context.driver = webdriver.Chrome() context.driver.maximize_window() def after_step(context, step): if step.status == "failed": context.driver.save_screenshot(f"screenshots/{context.scenario.name}.png") def after_scenario(context, scenario): context.driver.quit()

Run

bash behave behave --tags=@smoke behave --tags=@login --tags=-@slow behave features/login.feature behave -f allure_behave.formatter:AllureFormatter -o allure-results

Writing Good Gherkin

  • Describe behavior, not clicks: write "I log in as an admin", not "I click the button with id=login"
  • One scenario tests one rule
  • Keep steps reusable and independent from the browser details
  • Use Scenario Outline for repeated logic with different data
  • Business-readable language, no locators in the feature file

Using Page Objects in Steps

python from pages.login_page import LoginPage @when('I log in with "{username}" and "{password}"') def step_login(context, username, password): context.login_page = LoginPage(context.driver) context.login_page.login(username, password)

Conclusion

Behave connects readable Given-When-Then scenarios to Selenium step functions. Keep features in business language, put browser code in steps and page objects and use hooks for browser setup.

Quick Summary: feature file (English) + steps (Python) + environment.py (hooks).

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators