Automation Framework design using Selenium, Behave and Allure reports in Python
Introduction
In this article i will be designing a framework for testing web applications in popular BDD style, integrated python, behave, allure, a non-mainstream route. This framework is based on Page Object Model and has highest level of flexibility, you can play together.
Overview of Stack
- Behave — BDD testing https://behave.readthedocs.org
- Selenium — web applications testing https://selenium-python.readthedocs.org
- PyHamcrest — matchers and assertions https://pyhamcrest.readthedocs.org
- Allure — Used reporting part https://pypi.python.org/pypi/pytest-allure-adaptor
- Jira — Python library for interacting with JIRA via REST APIs. https://pypi.org/project/jira/
Pre- requisite
You need below Software to be installed on your system.
- Python 3.0 or above
- Pycharm- An IDE for Python
- Selenium, html-runner and Other Libraries
Installation
Use below command to install Selenium.
> pip install selenium
Same way install html-testrunner library
>pip install html-testrunner
use “pip freeze” command to check the list of Python libraries you have installed.
> pip freeze
Incase, you want to get information for a particular library you can use pip show <libraryname>. e.g.
pip show selenium
Application Under Test
We are going to automate some of the sample flow in Salesforce CRM application accessible from https://login.salesforce.com/?locale=au
Framework
Here is the folder structure for framework:
driver.py- contains methods related to browser like launching browser, navigating to URL, closing browser, capturing browser screenshots.
reuasblelibrary.py- This file contains all the reusable methods to increase reusability and reduce the maintenance of code. The most useful among all is perform_action_on_element that has action as parameter and can perform any action on the web objects like click, type, clear, scroll etc.
Python uses Behave to write the tests in BDD style. The two most important items of Behave are feature files and steps.
Feature files are the files where test scenarios are written. Gherkin language is used to write the keywords in feature files. Feature files are placed inside features directory. Following is an example of a feature file. Feature files have .feature extension.
steps directory is where the python test files will be placed. These test files will contain the python implementation of the feature files. Following is an example of the python test file.
steps will call methods available in the pages folder
Test data can be stored in .csv files inside testData folder.
The only locator’s type for now — Xpath, because of it is flexible and widespread. They are available in locators.py file.
This framework also has environment.py file which contains methods to be executed before or after a step / scenario in feature file.
Test Execution
$ behave features/Patrons/01_verify_view_patrons.feature
If you only mention the features directory and not mention any feature file, all the feature files inside features directory will be executed.
To run a specific scenarios inside a feature file you need to use tags. And to run with allure reports, use below commands.
$ behave -f allure_behave.formatter:AllureFormatter -o allure/results ./features
$ allure generate allure/results/ -o allure/reports — clean
$ allure open allure/reports
Debug Configuration
Git
Following is the link of the GitHub repository of the framework. Interested people can clone and look at it.