Behavior Driven Development (BDD)
The most effective and stable framework for BDD in Python is 'behave'.
You just need to install behave using pip3 (If you are using Python3)
--> pip3 install behave
As stated above in point #9 in 'How to build an automation framework' section, BDD related stuff i.e. feature file, steps declaration, and actual code files are typically organized in below hierarchy.
Feature file : login.feature
feature file will contain the features you want to test e.g. in this case feature is 'Verify login functionality' or it could be just 'login'
Scenario: Scenarios pertaining to feature Login e.g. login with valid credentials, login with invalid credentials.
@given --> This is nothing but a pre-requisite to run your test.
@When --> Any action performed on UI.
@Then --> End result to verify after any action is performed.
Feature: Verify login functionality Scenario: Login with invalid credentials Given The user is on login page When User enters an invalid username And User enters an invalid password And clicks on login button Then Login is unsuccessful Scenario: Verify hide and show button functionality Given User is on practice home page When user clicks on Hide button Then text box will be hidden When user clicks on Show button Then text box will be visible Scenario: Verify mouse hover functionality Given User is on Practice home page of letskodeit When User mouse hover an element Then mouse hover action is completed
Steps declaration: steps.py
from behave import * from BDD.login import TestDriver from BDD.HideShow import testHideShow from BDD.MouseHover import MouseHover # This is a pre-requisite to access login page @given('The user is on login page') def step_impl(context): context.driver = TestDriver() context.driver.test_launchBrowser('chrome') # This is a pre-requisite where user enters username @when('User enters invalid username') def step_impl(context): context.driver.test_uname('letskodeit@gmail.com') # This is a pre-requisite where user enters password @when('User enters invalid password') def step_impl(context): context.driver.test_pwd('admin') # This is a pre-requisite where user clicks on login button @when('clicks on login button') def step_impl(context): context.driver.test_click() @then('Login is unsuccessful') def step_impl(context): context.driver.test_unsuccessful() #This is another scenario @given('User is on practice home page') def step_impl(context): context.obj = testHideShow() context.obj.test_HomePage() @when('user clicks on Hide button') def step_impl(context): context.obj.test_ActHide() @then('text box will be hidden') def step_impl(context): context.obj.test_HideBtn() @when('user clicks on Show button') def step_impl(context): context.obj.test_ActShow() @then('text box will be visible') def step_impl(context): context.obj.test_ShowBtn() # ****** This is mouse hove scenario ****** @given('User is on Practice home page of letskodeit') def step_impl(context): context.driver = MouseHover() context.driver.test_homePage() @when('User mouse hover an element') def step_impl(context): context.driver.test_mouseHover() @then('mouse hover action is completed') def step_impl(context): context.driver.test_complete()
Step declaration above contains a method that is nothing but an implementation of your actual test script for each statement defined in your feature file.
E.g.
@given('The user is on login page') def step_impl(context): context.driver = TestDriver() context.driver.test_launchBrowser('chrome')
Code above shows for @given statement from feature file 'The user is on login page', test method 'test_launchBrowser' is defined in step_impl method here which has all the code required to launch a browser shown below.
Method 'test_launchBrowser':
def test_launchBrowser(self, browserType): if browserType=='chrome': self.driver.maximize_window() self.driver.get('url')
Note: Just to show you how the actual test method looks like, I have not copied the actual URL above and just referred it as 'url' in self.driver.get('url'). You need to pass actual url (https://*****.com)
Above code can be run using behave command along with the path as an argument where you feature file resides
(venv) pravin-a:Selenium_Projects pravin.a$ behave BDD/features/
(venv) pravin-a:Selenium_Projects pravin.a$ behave BDD/features/ Feature: Verify login functionality # BDD/features/login.feature:1 Scenario: Login with invalid credentials # BDD/features/login.feature:2 Given The user is on login page # BDD/features/steps/steps.py:7 5.906s When User enters an invalid username # BDD/features/steps/steps.py:13 2.188s And User enters an invalid password # BDD/features/steps/steps.py:18 2.076s And clicks on login button # BDD/features/steps/steps.py:23 3.123s Then Login is unsuccessful # BDD/features/steps/steps.py:27 0.163s Scenario: Verify hide and show button functionality # BDD/features/login.feature:9 Given User is on practice home page # BDD/features/steps/steps.py:32 6.472s When user clicks on Hide button # BDD/features/steps/steps.py:37 4.422s Then text box will be hidden # BDD/features/steps/steps.py:41 0.000s When user clicks on Show button # BDD/features/steps/steps.py:45 0.465s Then text box will be visible # BDD/features/steps/steps.py:49 0.000s Scenario: Verify mouse hover functionality # BDD/features/login.feature:16 Given User is on Practice home page of letskodeit # BDD/features/steps/steps.py:54 0.001s When User mouse hover an element # BDD/features/steps/steps.py:59 8.105s Then mouse hover action is completed # BDD/features/steps/steps.py:63 0.000s 1 feature passed, 0 failed, 0 skipped 3 scenarios passed, 0 failed, 0 skipped 13 steps passed, 0 failed, 0 skipped, 0 undefined Took 0m32.920s (venv) pravin-a:Selenium_Projects pravin.a$
As you can see above it will give you all the information after once all the scenarios are executed successfully.
E.g. How many features are executed/passed, how many scenarios in total are executed and passed/failed/skipped and the total no. of steps executed/passed/skipped.
You can also generate a report called 'allure' report for your test run using below command
(venv) pravin-a:Selenium_Projects pravin.a$ behave -f allure_behave.formatter:AllureFormatter -o %allure_result_folder% BDD/features
View the generated allure-report
Put It All Together
To put it together, you can make use of this hybrid automation test framework to automate UI's of your application and you can customize this framework as per your need.
Comments
Post a Comment