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

ATAL FDP - Cucumber

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)
68 views45 pages

ATAL FDP - Cucumber

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
[email protected]

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
1.Write 2.(Make the
TCs, code works) 1.Write 2.(Write
Test Fails Test passed TCs, Features/Scenarios)
Test Fails Test passed

3.Refactor

3.Refactor
ATDD
• Higher level of abstraction as compared to BDD/TDD
• Focus on high level requirements.Authors(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:
• 1.Create 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
2.Add 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 io.cucumber
Select latest version-6.10.4, click
3.Click and Open pom.xml ,
add tag;<dependencies></dependencies>
4.Click on cucumber java dependency (or select the
dependency)and copy between
;<dependencies></dependencies> in pom.xml ,save(ctrl+s)
Now you will find Maven dependencies
(for indentation-select the contents of pom.xml by Ctrl A then Ctrl I)
5.Create a Folder Features_atal under src/test/resources
• Write click on - src/test/resources->New->Folder->Type folder name(Features_atal)->Finish
6.Under Features_atal Folder create a new feature file-
login.feature(right click (Features_atal) ->New->File->In File name
type -login.feature->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 login.feature (sample feature file)
7.Delete 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 :com.Demo)->RightClick on
Com.Demo->New->Class-> Name(StepDefLogin.java)->Finish->copy the contents from
console to StepDefLogin.java and remove
throw new io.cucumber.java.PendingException();and replace with
•System.out.println() /short cut ,type syso+ctrl+space bar
Open StepDefLogin.java and type the Step Definition Functions as
shown and save (Hover mouse on annotations to import packages)
Open pom.xml and copy the cucumber-testNG dependency from maven
repository
right click(com.Demo)->New->class->(give name:TestRunner)->Finish-
>now open TestRunner.java and add @cucumberOptions()as shown
and add public class TestRunner extends AbstractTestNGCucumberTests {}
RightClick(testRunner.java)->Run As->TestNG Test->
Note:We can use Junit Also by adding the required plugins
1)Feature File
2)StepDefine.java
3)TestRunner.java
Right Click TestRunner.java->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->(SeleniumTest.feature)-
>Finish->delete the contents
Delete the contents and create new feature file as shown :
Go to StepDef package and create a java class-
SeleniumSteps.java
Run the Feature file(seleniumTest.feature)(Run As-> Cucumber
feature or Run Configuration) and copy the missing steps(methods)
into SeleniumSteps.java
SeleniumSteps.java with step definitions
Hover mouse on @Given (click on import io.cucumber.java.Given),Now
replace with import io.cucumber.java.en.*;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
(System.out.println)
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 chromedrive.exe
Write the code
• public class SeleniumSteps {
• WebDriver driver=null;
• @Given("Browser is open")
• public void browser_is_open() {
• System.out.println("Inside this step browser is open");
• System.setProperty("webdriver.chrome.driver","C:/Users/HP/eclipse-
workspace/CTNG/src/test/resources/Drivers/chromedriver.exe");
• driver= new ChromeDriver();
• driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
• driver.manage().timeouts().pageLoadTimeout(40, TimeUnit.SECONDS);
• driver.manage().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=System.getProperty("user.dir");


• System.out.println("Project path is:"+projectpath);
• System.setProperty("webdriver.chrome.driver",projectpath+
"/src/test/resources/Drivers/chromedriver.exe");

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


• public void user_is_on_search_page() {
• System.out.println("Inside this step user is on search page");
• driver.navigate().to("https://www.google.com");
• }
Now open google.com,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 {
System.out.println("Inside this step user enters text in search box");
driver.findElement(By.name("q")).sendKeys("ATAL FDP");
Thread.sleep(2000);
}
Next after entering text in search box you hit enter
(sendKeys(Keys.ENTER)

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


public void hits_the_enter_key_on_keyboard() throws InterruptedException
{
System.out.println("Inside this step user hits enter");
driver.findElement(By.name("q")).sendKeys(Keys.ENTER);
Thread.sleep(2000);
}
Save the file(SeleniumSteps.java),then right click
on SeleniumTest.feature->Run as->Cucumber
feature OR configuration(check path)
Make changes in the runner class
Now run using the TestRunner.java(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