Python With Behave Framework | Prerequisites, Features, Setting up, Features File – DS
What Is Behave?
Behave is BDD (behavior-driven development), BDD is an agile software development technique that encourages collaboration between developers, QA, and non-technical in a software project.
Behave is written in plain English, coded in python.
Prerequisites for Behave
- First, choose the code editor (I will choose PyCharm).
- Download Behave and Install Gherkin
- For Behave, Create New Project and Go to Settings > Click on Project > Click on Python Interpreter > Click on (+) Icon > Enter behave in the search field > Select behave and click on Install Package.
- For Gherkin Plugin, Go to Settings > Click on Plugins > Go to Marketplace > Type Gherkin and Click on Install.
Feature File
- A feature film is written by Business Analyst/whoever with your behavior scenarios in it.
Feature files contain scenarios and steps written in plain English in the “Given when, and then” format.
- Given write a step with a precondition.
- When we take key actions the user performs.
- Then we observe the outcomes
- And or But these are renamed by behave to take the name of the preceding step.
- In the “steps” directory, scenarios implement in python files. Steps are identified by matching the predicate from the feature file: given, when and then.
Scenario Outlines
When we need to run with a number of variables giving a set of known states
Example:
Feature file:
Corresponding Step File
from behave import * @given('user enters "{name}" and "{password}"') def step_implpy(context, name, password): print("Username for login: {}".format(name)) print("Password for login: {}".format(password)) @then('user should be logged in') def step_implpy(context): pass
Output
Good to Read:- Guide On Behaviour Driven Development Testing
Setting Up the Behave Project
Make a directory “features”. In that create a file called “demo. feature” and create one scenario as shown in the image:
Make a New directory called “features/steps”. In that create a file called “search.py” and enter the following code:
from behave import * from selenium import webdriver from selenium.webdriver import Keys from selenium.webdriver.common.by import By @given(u'I navigate to google.com') def step_impl(context): context.driver = webdriver.Chrome(executable_path="C:/Users/HP/Downloads/Drivers/chromedriver.exe") context.driver.get("http://google.com") context.driver.implicitly_wait(5) @when(u'I validate the page title') def step_impl(context): title = context.driver.title print(title) assert "Google" in title @then(u'I enter the text as python behave') def step_impl(context): context.driver.find_element(By.NAME, "q").send_keys("Python Behave"+Keys.ENTER) @then(u'I click the search button') def step_impl(context): context.driver.close
To run the above Scenario, Open the Terminal and enters the command:
Behave Features
It’s a command to run the features file, After pressing enter we’ll get the following output –
Recommended to Read:-