Skip to content
Advertisement

Cucumber feature file isn’t bound to glue path

I am trying to set up a simple Cucumber project to run with Selenium (The Selenium bits are irrelevant so I removed them). The structure is as follows:

├── main
│   └── ...
└── test
│   └── java
│       └── automated
│           └── sayHiTest.feature
│           └── SayHiTestStepdefs.java
│           └── testRunner.java

sayHiTest.feature:

Feature: Can I say hello?
  Scenario: Say Hello
    Given I visit "https://google.com"
    Then I enter "Hello World"
    Then I should get "Google"

testRunner.java:

@RunWith(Cucumber.class)
@CucumberOptions(
        features = "src/test/java/automated/sayHiTest.feature",
        glue = {"automated.SayHiTestStepdefs"}
)
public class testRunner { }

SayHiTestStepsdefs.java:

public class SayHiTestStepdefs {
    // @Before methods to set up Selenium driver. 
    @Given("I visit {string}")
    public void i_visit(String string) {
        driver.get(string);
    }
    // more @Then methods to assert "Google".
}

Trying to run testRunner yields the following error:

io.cucumber.junit.UndefinedStepException: The step "I visit "https://google.com"" is undefined. 

For some reason, it doesn’t see automated.SayHiTestStepdefs (It prints the same error if I changed its name to say, abc.SayHiTestStepdefs). I installed a cucumber extension for IntelliJ and it detects that it’s properly bound to the i_visit method, seen below:

ide view

I followed the @CucumberOptions section in the docs, I also checked other examples and the syntax seems to check out. Am I missing something?

Maven dependencies:

<dependencies>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.141.59</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.1</version>
            <scope>compile</scope>
        </dependency>

        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-java</artifactId>
            <version>6.9.0</version>
        </dependency>

        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-junit</artifactId>
            <version>6.9.0</version>
        </dependency>
</dependencies>

IntelliJ version & About:

IntelliJ IDEA 2021.1.1 (Community Edition)
Build #IC-211.7142.45, built on April 30, 2021
Runtime version: 11.0.10+9-b1341.41 amd64
VM: Dynamic Code Evolution 64-Bit Server VM by JetBrains s.r.o.
Linux 5.8.0-53-generic
GC: G1 Young Generation, G1 Old Generation
Memory: 750M
Cores: 2
Non-Bundled Plugins: gherkin (211.6693.111), cucumber-java (211.7142.13)
Kotlin: 211-1.4.32-release-IJ7142.27
Current Desktop: ubuntu:GNOME

Cheers.

Advertisement

Answer

As stated in the Cucumber docs:

glue should be a comma separated list of package names.

The provided value was a class name that is part of the automated package. The following code should work:

@RunWith(Cucumber.class)
@CucumberOptions(
        features = "src/test/java/automated/sayHiTest.feature",
        glue = {"automated"}
)

Cucumber searches all classes in the provided packages for step methods, this way any class placed in the automated package will do.

Advertisement