Behave Before Scenario Example using Selenium - Codoid
Select Page
Selenium

Behave Before Scenario Example using Selenium

In this blog, you will learn how to setup and tear down using Python Behave framework and ‘Before’ Scenario Example using Selenium.

Behave Before Scenario Example using Selenium

Running prerequisite and cleanup snippets are necessary to make your BDD scenarios independent. In this blog, you will learn how to setup and tear down using Python Behave framework and ‘Before’ Scenario Example using Selenium.

We, as a test automation services company, use Python and behave for multiple automation testing projects.

Launching & Quitting Browser Before and After Scenario

Inside the environment.py file, create a fixture to launch a browser before scenario and quit it after the scenario.

Setup & Tear Down Code

@fixture
def launch_browser(context):
    #Launch Browser
    context.driver = webdriver.Chrome(executable_path='driverschromedriver.exe')
    yield context.driver

    #Clean Up Browser
    context.driver.quit()
    print("=============>Browser is quit")
  

If you notice the above code, you can find both Setup & Tear-down in the same method. It reduces your scripting efforts to a great extend and eases the script debugging & maintenance. The ‘yield’ statement provides the webdriver driver instance. After that the test run executes each steps in the scenario and resumes the remaining statements (i.e. the clean-up steps) which are after the yield statement.

Before Scenario Method Call

After defining the fixture, you need a method to call the fixture (i.e. before_scenario).

def before_scenario(context,scenario):
    the_fixture1 = use_fixture(launch_browser, context)
  

Full Code

from behave import fixture, use_fixture
from selenium import webdriver

@fixture
def launch_browser(context):
    #Launch Browser
    context.driver = webdriver.Chrome(executable_path='driverschromedriver.exe')
    yield context.driver

    #Clean Up Browser
    context.driver.quit()
    print("=============>Browser is quit")


def before_scenario(context,scenario):
    the_fixture1 = use_fixture(launch_browser, context)
  

How to get the scenario status?

Behave has four statuses for each Scenario namely: untested, skipped, passed, failed. To retrieve the status, use the below statement.

print(context.scenario.status)
  

Scenario Duration

In behave framework, you can get the scenario duration in the clean-up section as shown below.

@fixture
def launch_browser(context):
    #Launch Browser
    context.driver = webdriver.Chrome(executable_path='driverschromedriver.exe')
    print("=============>Browser is launched")
    yield context.driver

    #Clean Up Browser
    context.driver.quit()
    print(context.scenario.duration)
    print("=============>Browser is quit")
  

In Conclusion

We hope the snippets which are shared in this blog article are useful. In our upcoming blog articles, we will through light on some of the most useful Python automation testing snippets. Subscribe to our blogs to get latest updates.

Written By

Submit a Comment

Your email address will not be published. Required fields are marked *


Running prerequisite and cleanup snippets are necessary to make your BDD scenarios independent. In this blog, you will learn how to setup and tear down using Python Behave framework and ‘Before’ Scenario Example using Selenium.

We, as a test automation services company, use Python and behave for multiple automation testing projects.

Launching & Quitting Browser Before and After Scenario

Inside the environment.py file, create a fixture to launch a browser before scenario and quit it after the scenario.

Setup & Tear Down Code

@fixture
def launch_browser(context):
    #Launch Browser
    context.driver = webdriver.Chrome(executable_path='driverschromedriver.exe')
    yield context.driver

    #Clean Up Browser
    context.driver.quit()
    print("=============>Browser is quit")
  

If you notice the above code, you can find both Setup & Tear-down in the same method. It reduces your scripting efforts to a great extend and eases the script debugging & maintenance. The ‘yield’ statement provides the webdriver driver instance. After that the test run executes each steps in the scenario and resumes the remaining statements (i.e. the clean-up steps) which are after the yield statement.

Before Scenario Method Call

After defining the fixture, you need a method to call the fixture (i.e. before_scenario).

def before_scenario(context,scenario):
    the_fixture1 = use_fixture(launch_browser, context)
  

Full Code

from behave import fixture, use_fixture
from selenium import webdriver

@fixture
def launch_browser(context):
    #Launch Browser
    context.driver = webdriver.Chrome(executable_path='driverschromedriver.exe')
    yield context.driver

    #Clean Up Browser
    context.driver.quit()
    print("=============>Browser is quit")


def before_scenario(context,scenario):
    the_fixture1 = use_fixture(launch_browser, context)
  

How to get the scenario status?

Behave has four statuses for each Scenario namely: untested, skipped, passed, failed. To retrieve the status, use the below statement.

print(context.scenario.status)
  

Scenario Duration

In behave framework, you can get the scenario duration in the clean-up section as shown below.

@fixture
def launch_browser(context):
    #Launch Browser
    context.driver = webdriver.Chrome(executable_path='driverschromedriver.exe')
    print("=============>Browser is launched")
    yield context.driver

    #Clean Up Browser
    context.driver.quit()
    print(context.scenario.duration)
    print("=============>Browser is quit")
  

In Conclusion

We hope the snippets which are shared in this blog article are useful. In our upcoming blog articles, we will through light on some of the most useful Python automation testing snippets. Subscribe to our blogs to get latest updates.