0% found this document useful (0 votes)
40 views

Cucumber BDD

Framework concept doc

Uploaded by

thotaroopa461
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)
40 views

Cucumber BDD

Framework concept doc

Uploaded by

thotaroopa461
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/ 16

1 of 15

Cucumber – BDD

Cucumber---is a framework that supports the implementation of BDD (behavior-


driven development)
Deferent BDD tools in the market--Cucumber, Specflow, Behave, Jasmine.
Cucumber supports Java, Python, .Net, PHP, JavaScript, Perl languages.

BDD—Behavior-driven development TDD--Traditional development

This is driven by developers, QAs, BRS-Business requirements


product owners, customers and specifications
business analysts.
This is mostly focused on the This is driven by the developers.
business scenarios of the product. Mainly used for unit testing.
BDD is used for projects involving This is mostly focused on the coding
the end users interaction implementation of the
functionalities of the application.
BDD does not require team members Test cases are written with the help
with technical knowledge. of any programming language.
The probability of having defects in TDD requires team members to have
the application is more compared to technical knowledge.
TDD.
Test cases are written in plain TDD is used for projects involving
English third-party tools and APIs.

http://tutorialsninja.com/demo/
Step 1—Create a maven project using Archetype –‘maven-archetype-apache’.
Step 2—Go to eclipse marketplace and install the ‘cucumber eclipse plugin’.
Then restart the eclipse.
Step 3—Add cucumber libraries to the maven project

 Cucumber-java
 Cucumber-Junit
 Cucumber-core
 Selenium-java
 JUnit
Step 4—Create package for ‘Feature files’ and create feature files with
.feature extension.

Feature file: A feature file is usually a common file which stores feature,
scenarios, and feature description to be tested. The feature file is an entry
point, to write the cucumber tests and used as a live document at the time of
2 of 15

testing. The product owner will review these feature files, provide feedback,
and approve these files. Feature file in a project only recognized by the
cucumber tool.

Feature: login
The user should be able to access his account using login functionality

Background:
Given User has opened the application URL
And Navigated to Login Page

Scenario Outline: Login with valid credentials


When Enters valid email address <username>
And User Enters valid password <password>
And Clicks on Login button
Then User should be able to successfully login
Examples:
| username | password |
| [email protected] | Arjun@123 |
| [email protected] | Arjun@124 |

Scenario: Login with Invalid credentials


When User enters invalid email address "[email protected]"
And Enters invalid password "Arjun@1233"
And Clicks on Login button
Then User should not be able to login
And get proper warning message

Scenario: Login with valid username and invalid password


When User enters valid email address "[email protected]"
And Enters invalid password "Arjun@12dw3"
And Clicks on Login button
Then User should not be able to login
And get proper warning message

Scenario: Login with invalid username and valid password


When User enters invalid email address "[email protected]"
And Enters valid password "Arjun@13"
And Clicks on Login button
Then User should not be able to login
And get proper warning message

Scenario: Login without username and password


When User don’t enters email address
And User don’t enters password
And Clicks on Login button
Then User should not be able to login
And get proper warning message
3 of 15

Step 5—Run the feature file and get copy the implementations methods which
thrown by console.
Step 6—Create package ‘stepDefs’ and under the package create class (class
name must match with feature file name. Every step in feature file will be
implemented as method in stepDef file.

 Use Tidy Gherkin plug-in


 Run feature file (step code will get in console)
 Manually can implement step code by mapping)
Step 7—Create ‘testRunner’ class to run tests

@RunWith(Cucumber.class)
@CucumberOptions(plugin = { "pretty", "html:target/CucumberHTMLReport.html" })

public class TestRunner {

Background:
The Background keyword is applied to replicate the same steps before all
Scenarios within a Feature File.
Scenario Outline:
The Scenario Outline keyword can be used to run the same Scenario multiple
times, with different combinations of values.
HTML Report:
@CucumberOptions(plugin= {"html:target/CucumberHTMLReport.html"})
Regular Expressions:

We cannot use regular expressions and cucumber expressions in one single


method
https://github.com/cucumber/cucumber-expressions#readme
Pretty Plugin:
It generates the report in the same way as it is a feature file, so tracing
is also made easy.
To implement this, just specify plugin = "pretty" in CucumberOptions.
@CucumberOptions( plugin = { "pretty" } )
4 of 15

DuplicateStepDefinitionException
When Cucumber finds multiple Step Definitions that are the same, it throws a
Duplicate Step Definitions exception.
Sol: remove the duplicate stepdef in implementation
AmbiguousStepDefinitionsException
When Cucumber Finds a Step matches more than one Step Definitions using
regular expressions, it throws an Ambiguous Step Definitions exception.
Sol: remove the duplicate stepdef in implementation
Comments in feature file
# is used to comments any steps in feature file
Ctrl+/ Shortcut for commenting multiple line in feature file
Tags in cucumber

tags="@search and @smoke"


--will execute only Scenario’s which have both the tags

tags="@validcredentials or @search or @smoke"


-- will execute the Scenario’s which have the any one tag of these

tags="not @smoke"
-- will execute the Scenario’s which is not having the these tag

tags="@regression and not @smoke"


-- will execute the Scenario’s which is having the @regression tag and not
having the @smoke tag

Hooks
@Before and @After Hooks in cucumber

import io.cucumber.java.After;
import io.cucumber.java.Before;

@Before
public void setup() {
System.out.println("browser got opened");
}
@After
public void teardown() {
System.out.println("Browser got closed");
}
5 of 15

These tags will get executed before and after for every scenario while
execution time
Tagged Hooks in cucumber

We can tag the Hooks for particular scenarios by addressing with their tags.
Then Hooks tags will get execute for that particular tags only.

@Before("@smoke")
public void setup() {
System.out.println("browser got opened");
}

@After("@smoke")
public void teardown() {
System.out.println("Browser got closed");
}

@BeforeStep and @AfterStep Hooks in cucumber


BeforeStep this will get executed before execution of every step in scenario

AfterStep this will get executed after finishing of every step execution in
scenario

@BeforeStep
public void beforeStep() {
System.out.println("before every step");
}

@AfterStep
public void afterStep() {
System.out.println("after every step");
}
To be run with particular tags then follow below

@BeforeStep("@register")
public void beforeStep() {
System.out.println("before every step");
}

@AfterStep("@register")
public void afterStep() {
System.out.println("after every step");
}
6 of 15

Multiple Hooks and their order


By using order attribute as parameter we can specify the order of tags which
needs to be get execute first and what next. As mentioned below code snippet.

@Before(order=1)
public void setupone() {
System.out.println("setup one got opened");
}
@Before(order=0)
public void setuptwo() {
System.out.println("set up two got opened");
}
@Before(order=2)
public void setupthree() {
System.out.println("setup three got opened");
}

Hooks and value attribute


Value attribute can be useful when we need to pass multiple parameters to the
tags

@Before(value="@register" , order=0)
public void setup() {
System.out.println("Browser got opened");
}

Data Tables
When user need to fill multiple fields with data that time data tables will
useful to reduce to steps in feature file
E.g.:- Forms filling, registration
Feature file

Scenario: Register with mandatory fields


When User enters below details in to the fields
| firstname | arjun |
| lastname | yalapalli |
| email | [email protected] |
| telephone | 950392023 |
| password | Arjun@123 |
And user select privacy polocy check box
When user click on continue button
Then user verify Expected result "Your Account Has Been Created!"
And user close the browse
7 of 15

StepDefinition

@When("User enters below details in to the fields")


public void User_enters_below_details_in_to_the_fields(DataTable dataTable)
{
Map<String, String> map = dataTable.asMap(String.class,
String.class);
System.out.println("user enter firstname " + map.get("firstname"));
System.out.println("user enter lastname " + map.get("lastname"));
System.out.println("user enter email " + map.get("email"));
System.out.println("user enter telephone " + map.get("telephone"));
System.out.println("user enter password " + map.get("password"));
}

Organizing the cucumber project


8 of 15

Running a specific feature file from testRunner

Organizing feature files in Maven project


Move the feature files to below location

Running cucumber tests using Maven


To run tests using maven we should follow the below rules
9 of 15

 Runner class name should have the word ‘test’. So that maven identify
the test class.

 Should be added Maven surefire plugin in POM.XML file

Overriding tags in Runner class using Maven Command line command

mvn test-Dcucumber.filter.tags=”@login”

Default tags in cucumber


@dev @wip @ignore

@RunWith(Cucumber.class)
@CucumberOptions(
features="src/test/resources/features",
glue="stepDefinitions",
tags="@all and not @div and not @wip and not @ignore",
plugin = { "pretty", "html:target/CucumberHTMLReport.html"
}
)

public class TestRunner {

}
10 of 15

Retrieving scenario names in to hooks

@Before()
public void setup(Scenario scenario) {
System.out.println("Browser got opened--" + scenario.getName());
}

@After()
public void teardown(Scenario scenario) {
System.out.println("Browser got closed--" + scenario.getName());
}

ClassNotFoundException in cucumber

When will occur:


No idea
What to do:
Update your maven project
Organizing Hooks in cucumber project
Create new package and create a ‘Hooks’ class. Then write the setup() method
and annotate with @Before.
Write teardown() method and annotate with @After.
Write beforeStep() method and annotate with @BeforeStep.
Write afterstep() method and annotate with @AfterStep.
As shown below image
11 of 15

public class Hooks {

@Before()
public void setup(Scenario scenario) {
System.out.println("Browser got opened--" + scenario.getName());
}

@After()
public void teardown(Scenario scenario) {
System.out.println("Browser got closed--" + scenario.getName());
}

@BeforeStep()
public void beforeStep() {
System.out.println("before every step");
}

@AfterStep()
public void afterStep() {
System.out.println("after every step");
}

Make changes in TestRunner class as shown in the below to run with hooks.

@RunWith(Cucumber.class)
@CucumberOptions(
features="src/test/resources/features",
glue={"stepDefinitions","hooks"},
tags="@all",
plugin = { "pretty", "html:target/CucumberHTMLReport.html" }
)

public class TestRunner {

Cucumber XML and JSON Reports


JSON-- json:target/CucumberReports/CucumberJSONReport.json
XML-- junit:target/CucumberReports/CucumberXMLReport.xml

dryRun
Cucumber dry run is used for compilation of the Step Definition and Feature
files and to verify the compilation errors. The value of dry run can be
either true or false. The default value of dry run is false and it is a part
of the Test Runner Class file.
12 of 15

@RunWith(Cucumber.class)
@CucumberOptions(features = "src/test/resources/features",
glue = { "stepDefinitions", "hooks" },
tags = "@forgetpassword",
dryRun = true,
plugin = {
"pretty","html:target/CucumberReports/CucumberHTMLReport.html" })

public class TestRunner {

}
Publish attribute to generate cucumber reports on cloud
@RunWith(Cucumber.class)
@CucumberOptions(features = "src/test/resources/features",
glue = { "stepDefinitions", "hooks" },
tags = "@all",
//dryRun = true,
publish=true,
plugin = {
"pretty","html:target/CucumberReports/CucumberHTMLReport.html" })

public class TestRunner {

}
Will get in console after running TestRunner file

From cucumber.properties
13 of 15

Publishing Cucumber reports on cloud using Environment Variable

1. Start cmd ‘Run as administrator’


2. Enter the command setx /M CUCUMBER_PUBLISH_ENABLED "true" and enter.
3. Then close everything and restart the system once.
4. After restart open cmd run as administrator then setx /M
CUCUMBER_PUBLISH_ENABLED “81271061-804b-494c-b7f2-1be23645f43b”.
5. Then press enter.
6. Will get ‘specified value get saved’.
7. Then restart machine again.
8. Open Eclipse now and run your TestRunner class.
9. Check the Reports with the help of URL which is provided in console.
10.

Integrating TestNG in cucumber


We need to remove cucumber-junit, junit dependencies from pom.xml file and
add cucumber-testng, testng dependencies as shown I below image
14 of 15

Then “TestRunner” class should be extends with “AbstractTestNGCucumberTests”


as shown below image

Then press Ctrl+Shift+O to import required packages


15 of 15

Then go to Eclipse marketplace and install and restart your eclipse IDE

Then check ‘RunAs’ options in ‘TestRunner’ class. You should get run as
option ‘1 TestNG Test’.

Run the ‘TestRunner’ class with TestNg test.


16 of 15

Finally TestNG plugin is integrated with Cucumber project

You might also like