Last Updated On
This tutorial explains the Glue code in Cucumber.
Table of Contents
- What is Glue code?
- Cucumber Glue Example
- How Glue Locate Step Definitions
- Multiple StepDefinitions in Glue Code
What is Glue code?
The glue option helps Cucumber locate the step definition file. In the glue cucumber option, we specify the package to load the glue code. Where glue code is our step definitions or hooks.
The glue code in Cucumber performs the following tasks:
– Scans the project for step definitions and associates them with the corresponding steps in the feature files.
– Executes the automation code defined in the step definitions when the corresponding step is encountered during test execution.
– Provides a way to configure Cucumber runtime options, such as defining test hooks, plugins, and other settings.
Cucumber Glue Example
import org.junit.runner.RunWith;
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
@RunWith(Cucumber.class)
@CucumberOptions(plugin = "pretty", features = "src/test/resources/features",
glue = "com.example.customer")
public class CucumberRunnerTest {
}
This option specifies the package where the step definition classes are located. In this example, the step definitions are expected to be present in the “com.example.customer” package.
How Glue Locate Step Definitions
1. By default, Cucumber expects the step definitions to be in the same package as the Test Runner class.
2. If the “glue“ option is not specified in the Cucumber options, Cucumber will automatically assume that the step definitions are located in the same package as the test runner. Therefore, if your test runner class is “com.example.runner“, Cucumber will consider the “com.example.runner“ package as the default package for locating the step definitions.
You can comment or remove the “glue“ line in the Cucumber options if your test runner and step definitions are in the same package.
Multiple StepDefinitions in Glue Code
If the step definitions are present in 2 different packages, you can separate packages using a comma and double quotes.
glue = {"com.example.customer", "com.example.agents"}
Without proper glue settings, Cucumber will not be able to connect the feature files’ scenarios with the corresponding implementation, resulting in unimplemented step errors.
We are done! Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!!