0% found this document useful (0 votes)
131 views45 pages

BDD & Cucumber for Developers

1. Behavior Driven Development (BDD) is an extension of Test Driven Development (TDD) that focuses on defining features and scenarios of a system. BDD uses tools like Cucumber to write tests in a business-readable language called Gherkin. 2. Cucumber is an open source tool used for automating acceptance tests and behavior driven tests. It supports writing feature files using the Gherkin language and mapping steps to code using step definitions. 3. The key steps to run a Cucumber test are: write feature files using Gherkin, create step definition classes to define the steps, and run the tests from a runner class that specifies the feature locations and outputs reports.

Uploaded by

nevoloc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
131 views45 pages

BDD & Cucumber for Developers

1. Behavior Driven Development (BDD) is an extension of Test Driven Development (TDD) that focuses on defining features and scenarios of a system. BDD uses tools like Cucumber to write tests in a business-readable language called Gherkin. 2. Cucumber is an open source tool used for automating acceptance tests and behavior driven tests. It supports writing feature files using the Gherkin language and mapping steps to code using step definitions. 3. The key steps to run a Cucumber test are: write feature files using Gherkin, create step definition classes to define the steps, and run the tests from a runner class that specifies the feature locations and outputs reports.

Uploaded by

nevoloc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 45

(BDD and Cucumber Tool)

Dr. K Sridhar Patnaik


Associate Professor
Dept. of Computer Science & Engineering
BIT Mesra, Ranchi
kspatnaik@[Link]

ATAL FDP
Development Frameworks

Traditional Development Process


Test Driven Development(TDD)
Behavior Driven Development(BDD)
Acceptance Test Driven Development(ATDD)
TDD Vs BDD

TDD BDD
• Development Centric approach • Extension of TDD, Works at higher level of
• Write TCs before code is abstraction.
written(author-Developer) • Always ask have we covered all possible
• Test first Approach(Focus: Is my code scenarios.
working correctly?) • Test first Approach(authors-Dev/QA/PO)
• More Collaboration
[Link] 2.(Make the
TCs, code works) [Link] 2.(Write
Test Fails Test passed TCs, Features/Scenarios)
Test Fails Test passed

[Link]

[Link]
ATDD
• Higher level of abstraction as compared to BDD/TDD
• Focus on high level [Link](BA/PO/Dev/QA)
customer
User Stories
Demo(PO/BA)

AT(PO/BA)
BDD
Develop Code

Write and Expand TCs

TDD
Refactor
UT to fail
CUCUMBER
Coding Testing
What are BDD and ATDD? Test Last Development

In the TDD (also called test first development)Framework of Agile


methodology, we normally write the test cases first and then execute them.
This is good to attain Test Coverage and make sure the build is ready for
production.

However, we can enhance the TDD framework by introducing Behaviors,


Features, etc. to the test and then execute the same. This enhancement to TDD
is termed as BDD (Behavior Driven Development).

BDD(Describes overall behavior of the system ,and Customer focused)

ATDD(Acceptance test driven development)-Define test which as requirements


for the system and development focused)

Testing Coding

TDD
BDD Example: Login, Register ,(withdraw and deposit w.r.t ATM software). USER Stories

To make the stories to have a consistent structure and model ,Cucumber uses Gherkin language :
Cucumber is an open source tool for test automation
Feature: Check Login Functionality
Scenario
Given(Prerequisite): User is on login screen
When(Action): User enters valid username and password
And(Some more Action): Clicks on login button
Then(Expected) : User is navigated to home page
Advantages:
• Helps to breakdown story into atomic actions
• Keeps the format consistent
• Makes it easier to understand for all
• Helps in automating the scenario
• Tools for BDD
Cucumber(java/Ruby Framework),SpecFlow(.NET framework)
JBehave(java)
Behat(Php)
Behave(Python)
Lettuce(Python)
Easy B
Fitness
TestLeft
JDave
Steps:
• [Link] maven project in Eclipse(Open Eclipse,Click ->File->New->
Other->(In Wizards type :maven ->Select maven project)->Next-
>Select create simple project->Next->In Artifact(Write in GroupId-
anyname like cucumber_atal,In artifactId- cucumber_atal->click Finish

Or add maven-
archetype-
quickstart
[Link] maven dependencies(cucumber
java+Cucumber Junit+Junit+Selenium java)
• Google- maven central->select maven repository->in search
type(cucumber java)->click cucumber JVM-java of [Link]
Select latest version-6.10.4, click
[Link] and Open [Link] ,
add tag;<dependencies></dependencies>
[Link] on cucumber java dependency (or select the
dependency)and copy between
;<dependencies></dependencies> in [Link] ,save(ctrl+s)
Now you will find Maven dependencies
(for indentation-select the contents of [Link] by Ctrl A then Ctrl I)
[Link] a Folder Features_atal under src/test/resources
• Write click on - src/test/resources->New->Folder->Type folder name(Features_atal)->Finish
[Link] Features_atal Folder create a new feature file-
[Link](right click (Features_atal) ->New->File->In File name
type -[Link]->Finish
Now ,go to help ->eclipse marketplace->in Find , type cucumber(you will
see cucumber eclipse plugin)->click install->restart eclipse,Now you will
see icon of cucumber in [Link] (sample feature file)
[Link] the contents of feature file and add new contents. Then right
click on contents->Run As->Run Configuration
After executing the Feature file you will get :
Right Click(src/test/java)->New->Package->(Give any name :[Link])->RightClick on
[Link]->New->Class-> Name([Link])->Finish->copy the contents from
console to [Link] and remove
throw new [Link]();and replace with
•[Link]() /short cut ,type syso+ctrl+space bar
Open [Link] and type the Step Definition Functions as
shown and save (Hover mouse on annotations to import packages)
Open [Link] and copy the cucumber-testNG dependency from maven
repository
right click([Link])->New->class->(give name:TestRunner)->Finish-
>now open [Link] and add @cucumberOptions()as shown
and add public class TestRunner extends AbstractTestNGCucumberTests {}
RightClick([Link])->Run As->TestNG Test->
Note:We can use Junit Also by adding the required plugins
1)Feature File
2)[Link]
3)[Link]
Right Click [Link]->Run As Junit
Html,json and xml reports can be generated
Tags can also be added like =@smoketest
Cucumber+ Selenium(Steps)
1)Create Feature file-→Right Click->Feature-New->File->([Link])-
>Finish->delete the contents
Delete the contents and create new feature file as shown :
Go to StepDef package and create a java class-
[Link]
Run the Feature file([Link])(Run As-> Cucumber
feature or Run Configuration) and copy the missing steps(methods)
into [Link]
[Link] with step definitions
Hover mouse on @Given (click on import [Link]),Now
replace with import [Link].*;Also replace When with And
wherever And is not found.(refer feature file and verify)
Delete contents of methods and so that we
can add new required functions
New print Methods added
([Link])
Now Run the feature file to verify (features
are connected to stepdefinitions i.e mapping)
Download chrome driver from google (version of your current chrome browser
should match with the one you download)
to check browser version(click on three dots on top right hand side of the browser
then settings then help the about chrome browser).(Download chromedriver_win32)
• Create a new folder (Drivers) under src/test/resources and copy/drag
the [Link]
Write the code
• public class SeleniumSteps {
• WebDriver driver=null;
• @Given("Browser is open")
• public void browser_is_open() {
• [Link]("Inside this step browser is open");
• [Link]("[Link]","C:/Users/HP/eclipse-
workspace/CTNG/src/test/resources/Drivers/[Link]");
• driver= new ChromeDriver();
• [Link]().timeouts().implicitlyWait(30, [Link]);
• [Link]().timeouts().pageLoadTimeout(40, [Link]);
• [Link]().window().maximize();
• }
Note-When you copy the path of the driver the double backwards slash are to be replaced
with single forward slash.
If the page has loaded and due to slow network some of the page objects are not uploaded
than if u take action on those objects ,it will result failure so we put some wait time.
pageLoad time- we can increase the page load time if the page loading takes more time in
order to avoid failures
Cont.

• Relative path: String projectpath=[Link]("[Link]");


• [Link]("Project path is:"+projectpath);
• [Link]("[Link]",projectpath+
"/src/test/resources/Drivers/[Link]");

• @And("user is on search page")


• public void user_is_on_search_page() {
• [Link]("Inside this step user is on search page");
• [Link]().to("[Link]
• }
Now open [Link],right click in search bar, then click
inspect,the shaded code belongs to the properties of the
search box(description about the web elements),select the
property name=‘q’
Cont.
add name(“q”)as property of search box and in sendKeys ()
type any text that you want to search
@When("user enters a text in a search box")
public void user_enters_a_text_in_a_search_box() throws
InterruptedException {
[Link]("Inside this step user enters text in search box");
[Link]([Link]("q")).sendKeys("ATAL FDP");
[Link](2000);
}
Next after entering text in search box you hit enter
(sendKeys([Link])

@And("hits the Enter Key on keyboard")


public void hits_the_enter_key_on_keyboard() throws InterruptedException
{
[Link]("Inside this step user hits enter");
[Link]([Link]("q")).sendKeys([Link]);
[Link](2000);
}
Save the file([Link]),then right click
on [Link]->Run as->Cucumber
feature OR configuration(check path)
Make changes in the runner class
Now run using the [Link](Run as Junit)
How to see reports?
Go to project/target->right click->refresh
Then right click on any report(html,json,xml)open in any editor

You might also like