HappiomHappiom
  • Self-Improvement
  • Relationship
  • AI for Life
  • Apps
  • Tech
  • More
    • Online Diary
    • Glossary
  • Learn
    • Book
    • >> Soft Skills
    • Time Management
    • >> Tech Skills
    • R
    • Linux
    • Python
  • Our Apps
    • Download Diary App
    • Write Your First Diary
    • Login to Online Diary App
    • 100K+ Famous Quotes Site
  • Resources
    • Self-Improvement Guide
      • 21-Days to Self-Improvement
      • Creating a Habit
      • Learn Life Experiences
      • Easily Prioritizing Tasks
      • Learning from Mistakes
      • Doing Regular Exercises
      • Setting Priority for Success
      • Avoiding Common Mistakes
      • Eating Healthy Food Regularly
    • Journaling Guide
      • Online Diary
      • Best Diary Apps
      • Diary Writing Ideas
      • Diary Writing Topics
      • Avoid Writing in Diary
      • Diary Writing as Hobby
      • Reasons to Write a Diary
      • Types of Feelings In Diary
      • Improve Diary Writing Skills
  • Self-Improvement
  • Relationship
  • AI for Life
  • Apps
  • Tech
  • More
    • Online Diary
    • Glossary
  • Learn
    • Book
    • >> Soft Skills
    • Time Management
    • >> Tech Skills
    • R
    • Linux
    • Python
  • Our Apps
    • Download Diary App
    • Write Your First Diary
    • Login to Online Diary App
    • 100K+ Famous Quotes Site
  • Resources
    • Self-Improvement Guide
      • 21-Days to Self-Improvement
      • Creating a Habit
      • Learn Life Experiences
      • Easily Prioritizing Tasks
      • Learning from Mistakes
      • Doing Regular Exercises
      • Setting Priority for Success
      • Avoiding Common Mistakes
      • Eating Healthy Food Regularly
    • Journaling Guide
      • Online Diary
      • Best Diary Apps
      • Diary Writing Ideas
      • Diary Writing Topics
      • Avoid Writing in Diary
      • Diary Writing as Hobby
      • Reasons to Write a Diary
      • Types of Feelings In Diary
      • Improve Diary Writing Skills
Expand All Collapse All
  • Python Examples
    • Basic Syntax
      • Python Example Code to Concat N Strings
      • Python Example Code to Concat 2 Numbers
      • Python Code to Find Perimeter of a Circle
      • Python Code to Convert CSV file to Parquet format
      • Python Code to Get Current Day of Week
      • Python Code to Convert Binary String to Decimal Number Vice versa
      • Python Code to Find Difference Between 2 Strings
      • Python Example Code to Remove Duplicates from a List
      • Python Example Code to Calculate Height of Triangle
      • Python Code to Generate Complex Random Password
    • File Handling
      • Python Code to Read Specific Line from a File
      • Python Code to Clear Contents of a File
      • Python Code to Count and List Files in a Directory
      • Python Code to Write & Read Key Value Pair in File
      • In Python File is Not Opening (How to Fix)
    • Modules and Libraries
      • Python Code to Load .SO File (and Invoke a Function)
      • Python Code for Automation using BDD
    • Object-Oriented Programming
      • Python Code to Create a Class with Attributes
      • Python Code to Define Methods in a Class
    • Python Example Code to Check Internet Connection
    • Example Python Code to Send an Email
    • Python Code to Fetch Data from an API (e.g., OpenWeatherMap)
    • Example Python Code to Extract Text from PDF
    • Python Code to Perform Web Scraping (e.g., Scraping Wikipedia)
    • Example Python Code to Plot Data Using Matplotlib
    • Python Code to Perform Data Analysis with Pandas
    • Example Python Code to Train a Simple Machine Learning Model (e.g., Linear Regression)
    • Python Code to Handle User Authentication in Flask
    • Example Python Code to interact with databases using libraries like SQLAlchemy

Python Code for Automation using BDD

Python is a powerful tool for automating tasks, and when paired with Behavior-Driven Development (BDD), it becomes even more effective. BDD allows teams to collaborate closely by writing tests in a human-readable format.

Using Python with BDD, testers can describe behavior in plain language, translating directly into executable tests.

In this article, I’ll show you an example Python code which uses BDD for automation. The example code automatically opens the chrome browser and navigates to the specified URL.

The following is the BDD code, on example of how you could write a Behavior-Driven Development (BDD) test using a framework like Cucumber and Selenium to test opening a browser window and navigating to Google.

Feature: Open and navigate to Google

  Scenario: User opens a browser window and navigates to Google
    Given the user opens a browser window
    When the user navigates to "https://www.google.com"
    Then the user should see the Google homepage

Let’s implement the step definitions using Python and Selenium.

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from behave import given, when, then
import time

@given('the user opens a browser window')
def open_browser(context):
    context.driver = webdriver.Chrome()  # You can use any other browser driver as per your preference

@when('the user navigates to "{url}"')
def navigate_to_url(context, url):
    context.driver.get(url)
    time.sleep(2)  # Adding a brief pause for demonstration purposes

@then('the user should see the Google homepage')
def verify_google_homepage(context):
    assert "Google" in context.driver.title

    # Optionally, you can check for other elements on the page to confirm the navigation
    # For example:
    # assert context.driver.find_element_by_name("q").is_displayed()  # Checking if the search bar is displayed

    context.driver.quit()

Make sure you have the necessary dependencies installed (selenium, behave, and the appropriate browser driver such as ChromeDriver). My code sets up a basic BDD scenario using Cucumber syntax and implements the step definitions in Python using Selenium for browser automation.

Let’s understand how to run the Python code with BDD.

1. Make sure you have Python installed on your system. Then install the necessary libraries using pip. You also need to download the appropriate WebDriver for your browser (e.g., ChromeDriver for Google Chrome).

pip install selenium behave

2. Copy the feature code into a file with the `.feature` extension, for example, `google.feature`.

3. Copy the Python step definitions code into a Python file, for example, `steps.py`.

4. Open a terminal or command prompt, navigate to the directory containing your feature file and step definitions file, and run the following command:

behave

This command will execute the BDD test using the feature file and step definitions in the current directory. If everything is set up correctly, you should see the browser open, navigate to Google, and the test should pass.

Writing automation code is simple, if you are using the Python + BDD combination.

Related Articles
  • Python Example Code to Calculate Height of Triangle
  • Python Example Code to Remove Duplicates from a List
  • Example Python Code to interact with databases using libraries like SQLAlchemy
  • Python Code to Handle User Authentication in Flask
  • Example Python Code to Train a Simple Machine Learning Model (e.g., Linear Regression)
  • Python Code to Perform Data Analysis with Pandas

No luck finding what you need? Contact Us

Previously
Python Code to Load .SO File (and Invoke a Function)
Up Next
Object-Oriented Programming
  • About Us
  • Contact Us
  • Archive
  • Hindi
  • Tamil
  • Telugu
  • Marathi
  • Gujarati
  • Malayalam
  • Kannada
  • Privacy Policy
  • Copyright 2025 Happiom. All Rights Reserved.