1

Well, I am creating a test automation framework with Cucumber and Java. I am able to create the step definitions and launch the tests. However, in my Gherkin feature in Intellij, it always show's me that the step is not recongised. Because of this, I cannot navigate to the implemented step definition.

I literally tried every solution that I came across Stackoverflow

// POM.xml - All my dependencies are in this file that are needed to support my solution. I used the latest version of all dependencies from the Maven repository

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>execute_auto</groupId>
    <artifactId>execute_auto</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-core -->
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-core</artifactId>
            <version>4.7.1</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-java -->
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-java</artifactId>
            <version>4.5.3</version>
            <scope>test</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/io.cucumber/gherkin -->
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>gherkin</artifactId>
            <version>5.0.0</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-jvm-deps -->
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-jvm-deps</artifactId>
            <version>1.0.6</version>
            <scope>provided</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.141.59</version>
        </dependency>

        <!-- Web driver manager dependency -->

        <dependency>
            <groupId>io.github.bonigarcia</groupId>
            <artifactId>webdrivermanager</artifactId>
            <version>3.6.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.github.bonigarcia</groupId>
            <artifactId>webdrivermanager</artifactId>
            <version>3.6.2</version>
            <scope>compile</scope>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

// StepDefinition.java - This is where I get the extracted step definitions which appear on the console when I run my test

package Steps;
import io.cucumber.java.en.*; //I replaced cucumber.api.java.
import Pages.*;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.WebDriverWait;

public class MyStepdefs {


    @Given("The user login to the application")
    public void the_user_login_to_the_application() {
        LoginPage login = new LoginPage(driver);
    }

    @When("the credentials are entered")
    public void the_credentials_are_entered() {
        // Write code here that turns the phrase above into concrete actions
        System.out.println("Print when steps");
    }

    @Then("the homepage is viewed")
    public void the_homepage_is_viewed() {
        // Write code here that turns the phrase above into concrete actions
        System.out.println("print then steps");
    }
}

// Myfeature - this is the sample feature that I am using to automate my test case

Feature: LoginFeature
Scenario: Log in
Given The user login to the application
When the credentials are entered
Then the homepage is viewed
JeffC
  • 22,180
  • 5
  • 32
  • 55
automaticien
  • 153
  • 5
  • 14
  • It is advisable to post the code as text rather than image. – coderpc Aug 08 '19 at 16:52
  • @JeffC, well you're right. I have not been using SO for long time. Hence, I am not quite aware of the rules. Hopefully, will get better with time. As far as the research is concerned, I went through almost all the links in Google that showed up after I typed "undefined step reference java in intellij feature file " – automaticien Aug 08 '19 at 18:05
  • What happens if you modify for ex @Given("The user login to the application") to @Given("^The user login to the application$") ? Is this step recognized? – Mr Cas Aug 08 '19 at 19:20
  • @MrCas the step remains unrecognised in the .feature file! – automaticien Aug 09 '19 at 06:44
  • Is the plugin Cucumber for Java enabled? (IntelliJ->Settings->Plugins)? This plugin enables Cucumber support with step definitions written in Java. – Mr Cas Aug 09 '19 at 11:10
  • @Mr Cas Yes. That was one of the first thing that I did when I started with my solution. I selected the Cucumber for Java plugin and not the Groovy one! – automaticien Aug 09 '19 at 17:23
  • The step definitions are created by you? Normally when you write the step in the feature file and you click alt + enter on the newly created step, you have the option to create automatically the step definition(you got the context menu with 2 options -> Create step definition and Create all step definitions ) and for ex the first step would be -> @Given("^The user login to the application$") public void theUserLoginToTheApplication() { //your code here }. The step definitions in your file look a lil' bit different and I am wondering if it could be the issue in your case ... – Mr Cas Aug 09 '19 at 17:38

2 Answers2

1

That's because, Intellij expects you to do some configurations before it'll automatically pick up your Step definitions. This link will the show you step by step setup process you need (That's if you've not already seen it). I had the same issue when I ran my first cucumber feature file in Intellij. Also after it works, you can do some more tweaks which I showed in this link.

Sammy65
  • 627
  • 2
  • 12
  • 28
  • Actually I already tried the steps that you mentioned in your first link. It still shows me that the the steps are not recognised. When I create all the new step definitons from the feature file, it genereates me a step something like this: @cucumber.api.java.en.Then("I am able to see the generated steps in the step definition file") public void iAmAbleToSeeTheGeneratedStepsInTheStepDefinitionFile() { } – automaticien Aug 08 '19 at 16:41
  • 1
    I think it is because that the import dependency of the Cucumber package has evolved from cucumber.api.java.en.Then to io.cucumber.java.en.Then ( if I am correct). – automaticien Aug 08 '19 at 16:42
  • @mpkorstanje I tried including the plugins stated in the link, it does not work still! – automaticien Aug 09 '19 at 08:24
  • That's because IDEA doesn't support this yet. – M.P. Korstanje Aug 09 '19 at 09:24
  • From the comment of @mpkorstanje, it seems Intellij has trouble with the new import location for Cucumber like you suspected. Just to be sure though, have you tried with the old import statement to see if that works fine? I've used Cucumber for Java with it's annotations in Intellij recently and had no problems. – Sammy65 Aug 10 '19 at 01:58
  • @Sammy65 with the old annotations, it works but it doesn't identify the steps when I run them! – automaticien Aug 17 '19 at 08:49
0

This was fixed in the latest IntelliJ update:

https://youtrack.jetbrains.com/issue/IDEA-217391.

Mate Mrše
  • 7,997
  • 10
  • 40
  • 77