Continuous Test Orchestration And Execution Platform Online
Perform automated and live-interactive testing on 3000+ real desktop and mobile devices online.
Selenium Python Tutorial: Getting Started With BDD In Behave
Posted On: April 29, 2020
22 Min Read
This article is a part of our Content Hub. For more in-depth resources, check out our content hub on Selenium Python Tutorial.
As technology becomes more complex, the number of people who actually understand it decreases. With test automation, the story is not so different. Key stakeholders in a project, with the non-technical background but with more insight into customer demand and use cases, might find it difficult to contribute to the process.
Wouldn’t it be great, if the testers, developers, product managers, business managers, and other stakeholders in a project could sit under one roof to unearth new test cases, user stories, and bugs for ensuring awesome product quality?
Well, this is not that far fetched! With Python Behave, a BDD (Behavior Driven Development) framework, written in plain language, you can help stakeholders to easily understand the logic in the test scripts.
In this Selenium Python tutorial, I’ll give you a detailed look at performing Selenium test automation with Python Behave, a behavior-driven test automation framework.
Below are the sub-topics covered as a part of this Selenium Python tutorial:
TABLE OF CONTENT
- What is BDD in Python Behave?
- Getting Started With Behave In Python For Behaviour Testing
- What is Behave in Python?
- How to Run tests on Behave and Selenium WebDriver ?
- Using Python Behave With Cloud Selenium Grid
- Pros and Cons of Python Behave Framework
What is BDD in Python Behave?
Behavior is Driven Development (BDD) is an extension of TDD (Test Driven Development) used for automated browser testing. As per BDD, the implementation of functionality comes at a later stage as tests should be created first. Insightful discussions and conversations form the base of BDD as all the stakeholders (technical and non-technical) work towards creating effective Selenium test automation cases that are in-line with the feature requirements.
As a developer or tester, you may have a doubt about the scenarios when you need to select BDD over TDD. TDD is ideal when a single unit has to be tested and you are not looking at performing regression testing anytime sooner. Complexities multiply and working implementation might fail if different modules are integrated. This is where BDD can be used as it is best-suited for integration testing or behavior testing.
The best part about behavior tests is that the tests are derived from features and business specifications, unlike other testing methodologies where technical specifications form the base of the test code. BDD stands on the pillar of effective communication/conversation and everything apart from that is optional.
BDD is also considered as another variation of ATDD (Acceptance Test Driven Development); the fundamental difference is that in BDD, the major focus is on behavior rather than tests.
Test Scenarios In BDD
The main advantage of using BDD is that the language used in writing the test scenarios is simple in nature. Once you have an in-depth understanding of creating Selenium test automation scenarios, you can easily understand a BDD test that is written with some other BDD test framework e.g. SpecFlow, Cucumber, etc.
Before writing Selenium test automation scripts, developers first have to come up with user stories. A good user story will be vital in documenting the feature and executing the acceptance tests.
Scenarios in BDD indicates how a particular feature should behave depending on the input parameters supplied to the test. Irrespective of the BDD test framework being used, the overall format of a feature file still remains the same.
For creating scenarios, Gherkin, a domain-specific language, is used for implementing the Selenium test automation scenarios. Unlike other testing methodologies that are based on technical specifications, scenarios in BDD are based on business and feature requirements as they can be well understood by technical as well as the non-technical crowd. BDD is not restricted to Python Behave but is also used in other frameworks such as Cucumber for Selenium testing.
Advantages of BDD
There are several benefits of using BDD; some of the major ones are listed below:
- BDD ensures that all the necessary project stakeholders are on the same page and collectively work towards making the product better via testing.
- As the tests are written in Gherkin i.e. a simple language, every team member can participate in test creation, as the Selenium test automation scenarios written in plain language in Gherkin.
- BDD tests are more reusable and modular when compared to TDD tests as changes in the business or feature specification will lead to minimal changes in the corresponding BDD features and scenarios.
- As the emphasis is laid on business and feature specification, BDD tests have an improved shelf-life in comparison to TDD
BDD Scenarios and Feature Files In Gherkin
Tests in BDD are based on the principles of ‘Given, When, Then’. The simplified syntax of Gherkin is below:
Suppose, you want to search for LambdaTest on the search engine DuckDuckGo, the test scenario will be,‘Performing search for LambdaTest’ on DuckDuckGo, and the user story will include the ideal usage of Gherkin keywords to achieve the end result.
The example is shown above in this Selenium Python tutorial makes use of the most frequently used Gherkin keywords i.e. Given, When, and Then. Detailed information about the keywords being used is below in this Selenium Python tutorial:
- Feature keyword – Provides a high-level description of the software feature.
- Scenario keyword – Indicates the title of the test case.
- Given keyword – Describes a set of pre-conditions for the Selenium test automation scenario. In the above example for the Selenium Python tutorial, the precondition is that the user should be on the DuckDuckGo homepage.
- When keyword – Describes the scenario steps. This is where the execution takes place. As shown above, the user should enter the search-term before performing the search operation.
- Then keyword – Describes the Scenario outcome. Validation is also done in this step. Search results for LambdaTest should appear in the search window.
- And – Used to provide additional steps. It is used along with other keywords such as Given, When, and Then.
Feature files in Gherkin are plain simple text files that have a .feature extension and can be pivotal in business driven development. A feature file can contain one or more scenarios. Relevant Tags (@Tag) are used to differentiate between different Scenarios. Shown below in this Selenium Python tutorial, is a simple feature file that consists of two Scenarios – Search for the keyword LambdaTest on Google & Search for the keyword LambdaTest on DuckDuckGo.
Getting Started With Behave In Python For Behaviour Testing
In this Selenium Python tutorial, I’ll show you how to use the BDD test framework called Behave with Selenium & Python particularly for automated browser testing related scenarios.
What is Behave In Python?
Behave is a behavior-driven test framework that is largely similar to other BDD test frameworks such as Cucumber, SpecFlow, Cucumber-JVM, etc. Being a BDD test framework, Python Behave is fundamentally different from other popular Selenium Python test frameworks such as pytest, pyunit, etc.
Context In Python Behave
Context is a very important feature in Python Behave where the user and Behave can store information to share around. It holds the contextual information during the execution of tests. It is an object that can store user-defined data along with Python Behave-defined data, in context attributes. It runs at three levels (feature, scenario, and test) that is automatically managed by Python Behave.
A new layer is added to the context whenever Python Behave launches into a new feature or scenario. This allows the new activity level to add new values or overwrite the ones that were previously defined for the duration of that activity. This can be called scopes.
Values can be defined in the environmental controls file i.e. environment.py that may be set at a feature level and then overridden for some scenarios. Changes made at a scenario level do not permanently affect the value set at the feature level. Context variable in all cases is an instance of behave.runner.Context.
Environmental Controls (environment.py) In Python Behave
Environment.py is an environment file with Python Behave hooks. It can be used to define code that should be executed before and after the occurrence of certain events during the Selenium test automation cycle.
Some of the common environmental controls that can be used in Python Behave are below:
- before_step(context, step), after_step(context, step) – Executed before and after every step.
- before_scenario(context, scenario), after_scenario(context, scenario) – Executed before and after every scenario.
- before_scenario(context, feature), after_scenario(context, feature) – Executed before and after every feature.
- before_all(context), after_all(context) – Executed before and after the execution of the entire test cycle. In the Selenium test automation examples demonstrated below, we would make use of before_all & after_all for allocating and deallocating resources required for the test execution.
How To Install Python Behave & Other Dependencies For Selenium Test Automation
Before installing Python Behave, the pre-requisites should be installed on the machine. The official support for Behave is for Python 2 but it works perfectly fine with Python 3.x. For demonstration of automated browser testing with Behave and Selenium, we would be using the test machine with Windows 10 operating system.
As the Selenium framework is used with Python Behave, you also need to install the Selenium WebDriver for the web browser on which Selenium test automation is performed. Selenium WebDriver for popular web browsers can be downloaded from the following locations:
After installing the required prerequisites completed, you can now install Python Behave framework. There are a number of ways using which you can install Python Behave on your machine.
a. Using pip command
Execute the following command on the terminal
pip install behave
b. Using the Python Behave source distribution
Once the behave source distribution is unpacked, enter the newly created “behave-< version >” directory and execute the following command:
python setup.py install
c. Using the Github repository
Run the following command on the terminal to install the latest version of behave from the GitHub repository.
pip install git+https://github.com/behave/behave
This certification is for professionals looking to develop advanced, hands-on expertise in Selenium automation testing with Python and take their career to the next level.
Here’s a short glimpse of the Selenium Python 101 certification from LambdaTest:
Directory (Project) Structure
As files of different types are present in any project that uses Python behave, the framework has a stringent directory structure. This opinionated project structure comes handy when adding new features to an existing project as files are placed in specific folders in the structure. If the project requirement is not complex, you can still do away with the structure and use a single folder for all the file types. However, maintaining a uniform project structure helps in maintainability of the project.
- The entire implementation should be present under the ‘features’ directory.
- Feature files (*.feature) should be present in the ‘features’ directory, including ‘environment.py’ (that contains the necessary hooks for initialization and de-initialization).
- The implementation of step definitions should be present in the features/steps directory.
- In this Selenium Python tutorial, I’ll demonstrate Selenium test automation scenarios hence; we would have configuration settings that will be located either in .ini/.cfg files.
The overall directory structure for this Selenium Python tutorial is shown below:
You can see the snapshot of the project directory below, to be used in this Selenium Python tutorial.
How To Run Tests On Behave and Selenium WebDriver?
In this section of the Selenium Python tutorial, we will look into the usage of Python Behave framework with Selenium WebDriver for scenarios related to automated browser testing.
ToDoApp: Testing With Behave & Selenium WebDriver
To demonstrate the usage of Python Behave with local Selenium WebDriver, I’ll take the example of a simple to-do app. Shown below in this in this Selenium Python tutorial are more details about the overall test:
- Navigate to the to-do app using the Chrome WebDriver.
- Mark the first two items as Done i.e. Check those two items.
- Add a new item – Yey, Let’s add it to list.
- Click Add button to add that new item to the list.
The overall implementation process for this Selenium Python tutorial is subdivided into simple steps:
- Project creation
- Feature file creation
- Creating the Configuration Settings (behave.ini | setup.cfg)
- Creating Helper Functions
- Creating environmental controls file (environment.py)
- Creating Step Definitions for each Scenario Step
Let’s look into each of these sub-tasks for the Selenium Python tutorial in more detail:
1. Projection Creation – Create a new project in PyCharm using the File -> New Project option. Before creating the project, you have to ensure that the prerequisites i.e. Python, Behave are installed on the machine.
2. Feature File Creation – All the Gherkin feature files should be present in the features\steps directory. We make use of Then, When, and Then keywords to come up with ToDoApp.feature.
The prerequisite is that the user has to be present on the ToDo app. Hence, the Scenario step starts with the Given keyword.
Given that I am on the LambdaTest Sample app
Shown below is the Feature file for the ToDo app – Scenario steps are created based on the tasks that have to be performed in the test i.e. Click on the first checkbox & second checkbox and mark as Done.
3. Creating the Configuration Settings (behave.ini | setup.cfg) – Configuration files (behave.ini, setup.cfg, tox.ini, and .behaverc) in Python Behave do not have problems like fixtures and can be used to setup the environment.
In behave.ini, the values under [behave.userdata] tag are user-defined environment variables. In setup.cfg, user-defined environment variables are under the [Environment] tag.
In our example, the browser on which testing is performed is Chrome and a new environment variable ‘Browser’ is defined in setup.cfg.
4. Creating Helper Functions – Interaction with the web elements on a web page is performed through the corresponding browser’s Selenium WebDriver. You could directly use the Selenium WebDriver APIs for Python in the Selenium test automation implementation however it would lead to repetitive code. Also it makes the code a lot less maintainable.
Hence, I created Helper functions (or wrapper functions) that internally calls the corresponding Selenium WebDriver APIs for Python. As all of the elements on a web page might not be loaded simultaneously, an explicit wait in Selenium has to be performed before any action is performed on the web element.
WebDriverWait class is used to define wait depending on certain conditions before proceeding further in the code. Along with wrapper for Selenium WebDriverWait, we create wrapper functions for performing actions such as creating a Selenium WebDriver (open), releasing the resources allocated by Selenium WebDriver (close), locating elements by XPath, locating elements by ID, etc.
In the constructor, an instance of the variable self._driver_wait has reference to an instance of the Selenium WebDriverWait class (i.e. class selenium.webdriver.support.wait.WebDriverWait). self._driver_wait is used in all the helper functions to ensure that the corresponding web element is loaded before any operation to search for the element via XPath, ID, etc. is performed on the page.
To start the Selenium WebDriver, we can make use of Behave fixtures. Like fixtures in Pytest, fixtures in behavior are also functions that are mainly used to execute code related to initialization, configuration, and cleanup activity in the test cycle. Fixtures have a scope that is defined using the tag @fixture. Fixtures, when used in scenarios related to automated browser testing, have a huge disadvantage – browser instances will open & close for every scenario/feature with the @fixture tag. It is not a good option when the test has to be performed across different types & versions of web browsers.
In this Selenium Python Tutorial, the environment function is used to start the WebDriver. The access to the Web instance is done through the before_all environment function in environment.py
5. Creating environmental controls file (environment.py) – The required hooks that have to be executed before and after the Selenium test automation cycle are defined in environment.py. As a configuration file (setup.cfg) is used to define user-defined environment variables, ConfigParser module has to be used to parse the contents of the .cfg file. PyCharm will prompt to install the necessary dependencies once the project is loaded.
The browser on which tests have to be performed are read from the ‘Browser’ variable defined in setup.cfg in [Environment] section
Resources allocated for Selenium test automation are freed as part of the after_all control. As seen in the implementation below, the environment functions (or helper functions) are used to perform the clean-up activity.
6. Creating Step Definitions for each Scenario Step – Each scenario step is mapped to a decorated Python function called a step definition. Shown below in this Selenium Python tutorial is a snippet of a scenario step bound with step definition:
Scenario Step –
Given I go to 4davanceboy to add item
Step Definition –
The complete implementation of step definitions for this Selenium Python tutorial is below:
As seen in the above implementation for this Selenium Python tutorial, the Context object is used to access the helper/environment functions. The respective helper functions are used for invoking the web browser, opening the test URL, locating respective elements on the page, and performing necessary actions on those elements after they are loaded on the page.
The elements can be located using the Inspect tool in Chrome/Firefox browser.
As a part of the step definition for the step I Click on first checkbox and second checkbox, the two checkbox elements are located using the Name locator. Once located, a click operation is performed on those elements.
7. Execution – For executing the newly implemented BDD tests, we use the command-line tool behave that can be configurable using configuration files and also has a bunch of command-line arguments.
For execution, your current directory should be the one that contains the source code, folders – features, helper, etc.
Python Behave supports a number of command-line arguments. Execute the help command to know more about those options.
The feature file is located in features\ToDoApp.feature. Run the following command on the terminal to trigger the execution of the feature file.
Shown below is the execution snapshot of the Selenium test automation performed using the local WebDriver:
Using Python Behave With Cloud Selenium Grid
Selenium test automation with local Selenium Grid can be used for testing on countable number of browser and OS combinations. Having an in-house infrastructure that houses machines with different browser types, browser versions, and operating systems can be very costly. It is also not a scalable approach.
Automated browser testing for web products is extremely important as it helps improve the test coverage, in turn the product quality. A more scalable approach is to use cross browser testing on the cloud as tests can be performed across a large number of browser and platform combinations. LambdaTest is a cloud-based cross browser testing platform that lets you perform Selenium test automation across 3000+ different combinations of browsers, operating systems, and devices.
The effort involved in porting an existing implementation on the local Selenium grid to a cloud-based remote Selenium grid is minimal as the changes are majorly related to the infrastructure. Once you have created an account on LambdaTest, you have to make a note of the user-name & access-key from the Profile Section since that combination is used for accessing the Cloud Selenium Grid on LambdaTest. The Dashboard is used to view all the details related to the tests performed on the remote Selenium grid. The LambdaTest capabilities generator is used to generate desired browser & platform capabilities that will be used for automated browser testing.
ToDoApp: Testing with Python Behave & Remote Selenium WebDriver
To demonstrate the usage of Python Behave with remote Selenium WebDriver, we use the same test scenario of ToDoApp. The steps that have to be performed in the tests also remain the same.
- Navigate to the to-do app https://lambdatest.github.io/sample-todo-app/ using the Chrome WebDriver.
- Mark the first two items as Done i.e. Check those two items.
- Add a new item – Yey, Let’s add it to list.
- Click Add button to add that new item to the list.
The existing code has to be changed to make it work on the Cloud Selenium grid. The core logic of the test scenarios remains unchanged. Below are the major changes in the implementation in this Selenium Python tutorial:
- For demonstrating the usage of behave.ini, setup.cfg will not be used for creation of the configuration settings. Like setup.cfg, behave.ini will also be present in the parent directory that contains the test code.
- As a different configuration setting is used, corresponding changes have to be made in the implementation. The files that have to be added/changed are below:
The tests have to be performed on Chrome (version – 76.0) for Windows 10 platform. The required capabilities can be generated using the LambdaTest capabilities generator.
The capabilities will be a part of the behave.ini file and the corresponding entries from the ini file are read in the get_browser API (defined in helper\helper_web.py).
1. Creating the Configuration Settings (behave.ini)
Shown below are the contents of behave.ini:
Instead of just the browser name (i.e. Chrome) which was included in the configuration setting for test using local Selenium WebDriver, here the other browser capabilities along with the LambdaTest credentials are present. The user-name and access-key combination can be obtained from the Profile Section on LambdaTest.
Hence, the context object contains more information when compared to the corresponding test that used local Selenium WebDriver.
2. Creating appropriate Helper Functions (helper_web.py)
Along with the browser, four more arguments are added to the get_browser API. These additional arguments are in-line with the parameters present under [behave.userdata] section of behave.ini.
For simplification, we have used the test browser as Chrome. Instead of using local Selenium WebDriver, remote Selenium WebDriver for Chrome is invoked. The browser capabilities are passed along with the remote_url that contains the location of the remote Selenium grid.
3. Creating environmental controls file (environment.py)
Apart from the currently set browser variable, we defined the browser_version and the platform in the [behave.userdata] section of behave.ini.
The values from the [behave.userdata] section (in behave.ini) are read using config.userdata[‘attribute_name’] in before_all hook. These values are also added to the context object as they would be used during the entire testing cycle.
4. Creating Step Definitions for each Scenario Step (features\steps\ToDoApp_steps.py)
Though there are no major changes in the file that contains the step definitions, a small change is done to suppress urllib3 warnings.
For executing the tests on the remote Selenium grid, we trigger the same Python behave command on the terminal
Shown below is the execution snapshot on LambdaTest for the test performed using the remote Selenium WebDriver:
The Automation Dashboard contains detailed information about the test, including the summary, network logs, Selenium logs, exceptions, and more. Below is the snapshot of the execution of feature file (i.e. features\ToDoApp.feature) in this Selenium Python tutorial:
Pros and Cons of Python Behave Framework
Every framework has its own share of advantages (pros) and shortcomings (cons), the same is the case with the Python behave framework. Though it is a popular framework for BDD it does not have its fair share of shortcomings.
Pros Of Using Python Behave Framework
- Excellent online documentation and tutorials.
- Supports PyCharm (Professional Edition).
- Full support of the Gherkin language.
- Easy to ramp-up with prior knowledge of any BDD framework.
- Easy setup & cleanup due to availability of environmental functions, configuration settings, fixtures, and more.
- Supports integration with Django and Flask.
Cons Of Using Python Behave Framework
- Not supported with PyCharm (Community Edition).
- Sharing steps between different feature files is not straightforward.
- No in-built support for parallel test execution. Workarounds for parallel test execution require additional software, popular discussion threads on StackoverFlow here and here.
- Popular framework behave-parallel that earlier facilitated parallel test execution on Python behave is now deprecated.
It’s A Wrap!
Behave is one of the popular BDD frameworks that is preferred by experienced Python practitioners. In this Selenium Python tutorial, I explained in detail, how to use Python Behave for Selenium test automation. The upside of using Python Behave is that there is plenty of documentation and support available on the internet that can be helpful to get started.
As Gherkin is used, creation of feature files does not require any technical know-how. Parallel test execution is the most vital feature when it comes to Selenium test automation as numerous tests have to be performed on ‘N’ combinations of browsers, platforms, and devices.
Python Behave is best-suited for serial automated browser testing. Even if we shift the testing to a powerful Selenium grid, you might not be able to get the expected throughput as parallel testing is a hassle to behave. To summarize, Python has the right set of features (with a few shortcomings) which makes it useful for BDD.
That was all for now, I hope you found the article informative. Feel free to retweet this article and share it with your peers! Happy Testing☺
Got Questions? Drop them on LambdaTest Community. Visit now