Cucumber
Cucumber is a software tool that supports behavior-driven development (BDD). Central to the Cucumber BDD approach is its ordinary language parser called Gherkin. It allows expected software behaviors to be specified in a logical language that customers can understand. As such, Cucumber allows the execution of feature documentation written in business-facing text. It is often used for testing other software.It runs automated acceptance tests written in a behavior-driven development (BDD).
Cucumber is an open-source testing framework that supports Behavior Driven Development for automation testing of web applications. The tests are first written in a simple scenario form that describes the expected behavior of the system from the user's perspective.
Largely used for acceptance tests, Cucumber is written in Ruby, while the tests are written in Gherkin, a non-technical and human-readable language.
Cucumber is a software tool used by the testers to develop test cases for the testing of behavior of the software
Cucumber use in the development of acceptance test cases for automation testing. It is mainly used to write acceptance tests for web applications as per the behavior of their functionalities.
The Cucumber Framework
Cucumber framework mainly consists of three major parts
1.Feature File
2.Step Definitions
3.Test Runner File

1. Feature File
A standalone unit or a single functionality (such as a login) for a project can be called a Feature. Each of these features will have scenarios that must be tested using Selenium integrated with Cucumber. A file that stores data about features, their descriptions, and the scenarios to be tested, is called a Feature File.

Cucumber tests are written in these Feature Files that are stored with the extension – “.feature”. A Feature File can be given a description to make the documentation more legible.

Example: The Login function on a website
Feature File Name: Login.feature
Description: The user shall be able to login upon entering the correct username and password in the respective fields. The user should be directed to the homepage if the username and password entered are correct.
Keywords such as GIVEN, WHEN, and THEN used to write the test in Cucumber are called Annotations.
GIVEN user navigates to login page by opening Firefox
WHEN user enters correct AND values
THEN user is go to the homepage

2. Step Definitions
Now that the features are written in the feature files, the code for the related scenario has to be run. To know which batch of code needs to be run for a given scenario, Steps Definitions come into the picture. A Steps Definitions file stores the mapping data between each step of a scenario defined in the feature file and the code to be executed.
Step Definitions can use both Java and Selenium commands for the Java functions written to map a feature file to the code.
Example:

package StepDefinition;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
public class Steps
{
@Given("^user navigates to the login page by opening Firefox$")
//Code to Open Firefox Browser and launch the login page of application to define the GIVEN step of the feature
@When("^user enters correct username and password values$")
//take inputs for username and password fields using find element by xpath. Put the correct username and password values as inputs to define the WHEN step of the feature
@Then (“^user gets directed to homepage$”)
//Direct to the Homepage of the application as a result of correct username and password inputs in the WHEN step. This would define the THEN step of the feature

3. Test Runner File
To run the test, one needs a Test Runner File, which is a JUnit Test Runner Class containing the Step Definition location and the other primary metadata required to run the test.
The Test Runner File uses the @RunWith() Annotation from JUnit for executing tests. It also uses the @CucumberOptions Annotation to define the location of feature files, step definitions, reporting integrations, etc.

Example:
Test Runner Class in cucumberTest package, with the feature files in “src/test/Feature” location and Step Definition files in “src/main/stepDefinition” folder.
package cucumberTest;
import org.junit.runner.RunWith;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
@RunWith(Cucumber.class)
@CucumberOptions(
features = "src/test/Feature"
,glue={"src/main/stepDefinition"}
)
public class TestRunner {
}

Advantages of Cucumber Over Other Tools
  • Cucumber supports different languages like Java.net and Ruby.
  • It acts as a bridge between the business and technical language. We can accomplish this by creating a test case in plain English text.
  • It allows the test script to be written without knowledge of any code, it allows the involvement of non-programmers as well.
  • It serves the purpose of end-to-end test framework unlike other tools
  • It serves the purpose of end-to-end test framework unlike other tools
  • Due to simple test script architecture, Cucumber provides code reusability.

www.TechSearhWeb.com