You are on page 1of 20

Python

Python – Behave - Selenium


What is Python?
• Python is a simple, free (open source) and portable programming
language that lets you work more quickly and integrate your
systems more efficiently.
• According to the traditional definition, Python is an interpreted,
high-level, general programming language that helps programmers
to write manageable and logical code for small as well as large-
scale projects.

Why use Python?

Introduction to • fast,
• scalable,
• versatile,

Python • popular,
• ever-growing,
• widely supported,
• easy to write,
• intuitive to read,
• quick to develop,
• simple to maintain.
Automated testing is a well-known context in the
world of testing. It is where the test plans are
being executed using script instead of a human.

Why use Python Python comes with the tools and libraries that
support automated testing for your system.
for Automation
Testing? Python Test cases are comparatively easy to write.
With the increased use of Python, Python-based
test automation frameworks are also becoming
popular.
Robot

PyTest

PyUnit
Python Testing
Frameworks: Behave

Lettuce
What is Behave?
Behave is Python style behavior-driven development. Behavior-
driven development (or BDD) is an agile software development
technique that uses tests written in a natural language style,
backed up by Python code.

Introduction Why use Behave?


It encourages collaboration between developers, QA and non-

to Behave
technical or business participants in a software project.

What is Behave Format?


Feature: Title of the Scenarios

Scenario: A Business situation


Given some precondition
And some other precondition
When some action is performed
And some other action is performed
Then some result is achieved
Download Python from
Download https://www.python.org/downloads/windows/

Download PyCharm from


Download https://www.jetbrains.com/pycharm/download/#section=wind
ows

Downloads and Install Install Behave in Command line using pip install behave

Installations Install Install selenium in Command line using pip install selenium

Download Chromedriver using


Download https://chromedriver.chromium.org/downloads
What is
Selenium
WebDriver?
Add ‘Path’ for Chromedriver in Advanced System Settings:
What is a Virtual Environment?

Python has different versions such as Python 2x and Python 3x and


each version has different libraries or packages that anyone can use
in their project. To support multiple versions of Python on the same
machine or some packages which work only with certain versions of

Virtual
Python, we can setup different environments, called Virtual
Environments in our system.

Environment
Each environment will be its own virtual space. All packages
installed within that space would not interfere with packages
outside the environment and will be contained only inside this
space.

What are the different types of Virtual Environments?

Virtualenv
Pipenv
Pew
Venv
‘Pipenv’ helps in installing Python packages using Pip and helps to
keep Python packages of different projects isolated from one
another.
 

Why use
It is a combination of three tools such as Pyenv, pip and Virtualenv.

• Pyenv allows users to install and use specific versions of Python

‘Pipenv’
and its related tools.
• Pip fetches and installs Python packages.
• Virtualenv allows users to keep the Python packages of different

Virtual
projects isolated from one another.
• Pipenv is a tool that puts all these tools together. It takes care of
calculating the complete set of dependencies. In installation

Environment?
mode, it tells virtualenv and pip where to install the
dependencies. In runtime mode, it makes an environment
available with the right version of the Python interpreter (via
pyenv) and the right set of packages (via virtualenv).
 
 
Install ‘pipenv’ Virtual
Environment:
How to install Pip install pipenv

and activate Activate the Virtual


‘pipenv’? Environment:
pipenv shell
Set ‘pipenv’ in your New Python Project:
Add Behave in BDD Framework in Settings:
Add package ‘Behave’ in Project Interpreter in your Python Project:
Project creation

Feature file creation for gherkins


Steps for (*.feature)

Writing Creating environmental controls file


(environment.py)
Automation Creating Step Definitions for each
tests in Scenario Step (*.py)

PyCharm: Execution/Run the Feature files


The environment.py file defines code to run before and after certain
events during Automation testing. It is commonly used to setup and
clean up the Selenium WebDrivers. The different tags that can be used
in this file are:

Why use 1. before_step (context, step), after_step (context, step) - These run
before and after every step.

Environment
2. before_scenario (context, scenario), after_scenario (context,
scenario) - These run before and after each scenario is run.
3. before_feature (context, feature), after_feature (context, feature) -

Controls file? These run before and after each feature file is executed.
4. before_tag (context, tag), after_tag (context, tag) - These run
before and after a section tagged with the given name. They are
invoked for each tag encountered in the order they’re found in the
feature file.
5. before_all (context), after_all (context) - These run before and after
all the tests are executed.
Behave Project Structure:

You might also like