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 ScenarioInside the file, create a fixture to launch a browser before scenario and quit it after the scenario.

Setup & Tear Down Code

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).

Full Code

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.

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

In conclusionWe 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.

Leave a Reply

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