Tuesday, 24 November 2015

How to use Cucumber with JAVA







Contents
1    What is BDD framework?
2    Detail explanation for Cucumber framework with BDD approach
2.1     Folder structure and steps to be followed to create the feature file(in Eclipse IDE):
2.2     How to write the feature file, syntax and keywords to be used:
2.3     How to write step definitions for the feature steps:
2.4     Repositories, Actions and Functions to reuse:
2.5     How to run the feature file using junit:
2.6     Test results after running the script:
3    Conclusion:




i.              

1       What is BDD framework?

BDD stands for Behavior driven development. It is a framework that can be used as a communication and collaboration framework for developers, testers and non-technical or business participants in a software project.
This document covers the approach of BDD in developing the scripts using Selenium webdriver in IP project. BDD approach all deals with simple semi formal language consists of Given, When and Then (which Cucumber can understand). This language is referred as Gherkin language.
Given: When some prior condition specified
When: Some action happens or encountered
Then: What should be the outcome?
In other words, BDD can be defined as an approach that can be followed for all frameworks such as Jbehave, rbehave, Cucumber, Cucumber JVM etc., in our project, the framework we have used is Cucumber framework with BDD approach. Cucumber framework lets us to write the testable scenarios in simple and understandable English sentences using business domain specific words. This kind of framework with BDD approach helps the business analysts and the product owners also in understanding what actually the requirement is about and what actually going to be tested. This framework also helps in understanding the requirement coverage with respect to testing. This reduces the effort required in acceptance testing.

2       Detail explanation for Cucumber framework with BDD approach

The files which consist of the set of scenarios to be tested in simple semi formal language are called as “Feature files”. The feature file is a normal text file with an extension “.feature”.

2.1     Folder structure and steps to be followed to create the feature file(in Eclipse IDE):

1.   Go to feature files folder (in which you are going to create the feature file) in the project which is usually under project source folder. Please see the below snapshot for sample folder path:
Example:


2.  Right click on the folder -> New -> Other -> Expand ‘General’ -> Select ‘File’ -> Click on Next -> Name the feature file and specify the extension “.feature” and click on finish.
3.   The feature that was created will looks as follows as normal text file:
 












Note: When the eclipse IDE is added with Cucumber plug-in, then the file will look like as below:

2.2     How to write the feature file, syntax and keywords to be used:

The feature file can be prepared once the tester is ready with test cases. As the automation went in Agile methodology, I’m explaining the process based on the agile methodology. In Agile methodology, we have followed scrum framework. Scrum framework deals everything in form of User stories. A user story is nothing but an individual requirement or can be a part of huge requirement. User story will have the following information (please see the below snapshot):
Sample story card:
User story 001
Capability
User should be able to Login
Feature
Login to application
Functional requirements covered
Req_01
Narrative
In order to view the Home page and click on the Help link in an application
As a valid user
I want to login to the application
Assumptions
N/A
Constraints
N/A
Dependencies
N/A
Risks
N/A

Sample acceptance criteria:
Test No.1
Valid user is able to login to the application and able to click the Help link
Given
When user is having valid credentials
When
When user entered the valid credentials on the login page
Then
User should be able to login to the application. User should be able to see the home page. User must be able to click on the Help link

After going through the story card and acceptance criteria, tester can write the conditions followed by cases. Once this is done, tester should be able to find which test cases can be automated and which can’t be automated. After finished with this analysis, tester will be ready with the automatable test cases. Now the scripting process begins:
Tester need to prepare a feature file as below:

1.   The feature file must have a opening tag starts with @ symbol
Syntax:
 @Screenname_TabName (Note: Name of the tag need not be same always)

2.   The opening tag should be followed by Feature(Functionality) name
 Syntax:
Feature: Screenname_Functionality_To_Verify

3.   Write the Scenario that you are going to test
Syntax:
Scenario: Verify the Login functionality and Help link access in Home page

4.   Scenario must be followed by the steps to be followed to test that scenario
Syntax and sample feature steps:
Given I’m a valid user and I’m in application login page
When I entered the valid details in the login page and clicked on login button
Then I should navigate to the home page and I should be able to click on Help link

5.   If tester needs to have multiple pre conditions or multiple actions to perform or multiple out comes to be verified, then “And” can be used to meet this requirement.
Syntax and sample feature steps:
Given I’m a valid user and have the valid credentials
And I’m on Application’s Login page
When I enter the valid credentials
And I click on the Login button
Then I should be able to see the Home page

And I should see the Help link on the Home page
And I should be able to click the Help link

2.3     How to write step definitions for the feature steps:

After finishing off the feature file preparation, next step is to provide definitions for each of the steps we have written in the feature file. Step definition file is a java class file where tester can provide the definitions for each step. The definition should be unique for each step across the project. This means, even though we have same step for multiple features or for multiple scenarios within the same feature file, tester should give single definition across the project. Below is the syntax to define a step in class file:
Syntax:
@Given/@When/@Then(“^----Feature Step which we are defining---- $ ”)

Java method to define the above step()
{
//actual code
}

2.4     Repositories, Actions and Functions to reuse:

Now tester is ready with Feature file and respective step definition file. In order to give definition for each of the step, testers will user repositories, actions and functions.
Repository: Repository is a java class file where tester can keep paths to access all the web elements that are present on the web page to be tested, so that tester can make reuse everywhere. Maintaining a separate repository is for code reusability.
Actions: Coming to actions part, actions are nothing but the element level actions such as clicking on a button or entering some text in to the text field. Actions is also a java
class file where tester can write all the actions that can be performed on the web page elements to be tested, so that he can reuse them everywhere.
Functions: Function is a sequence of actions that will perform some functionality (ex: login to the application). Functions file is a java class file where tester can write all the function as that can be performed on the page. We can have common function java class file as well where tester can write the functions which can be used across the project. Maintaining Functions file is for code reusability.

2.5     How to run the feature file using junit:




Here, tag name which was given in the feature file needs to be specified. The system will execute the feature file with the given tag. If tester wants to run few scenarios from a single feature file or from different feature files, all the scenarios should be grouped by specifying a unique tag for all the scenarios.

2.6     Test results after running the script:

After running the script, the test results will be displayed in an html format as below:
Scenarios with green color represent successful scenarios and scenarios with red color represent failed scenarios.


3       Conclusion:

The BDD approach along with Cucumber framework is the best approach for agile users. With the help of BDD approach along with Cucumber framework implementation, it will be easy and clear for the Business Analysts, Product owners to understand what the actual requirement is? And what is actually tested. This in turn helps in reducing the acceptance testing effort.

No comments:

Post a Comment