Installation of Cucumber in Eclipse and IntelliJ

 

Installing and configuring Cucumber in Eclipse and IntelliJ IDEA involves setting up a Java + Maven or Gradle project, adding Cucumber dependencies, and configuring the environment to recognize .feature files and step definitions. Below is a detailed guide for both IDEs.


For Eclipse IDE:

Step 1: Install Eclipse IDE


Step 2: Install Cucumber Eclipse Plugin (Optional)

This plugin enables syntax highlighting and snippets for .feature files.

  • Open Eclipse → Help → Eclipse Marketplace

  • Search for: Cucumber Eclipse Plugin

  • Click Install, then Restart Eclipse


Step 3: Create Maven Project

  • File → New → Maven Project
  • Choose template: maven-archetype-quickstart
  • Set GroupIdArtifactId, and click Finish


Step 4: Add Cucumber Dependencies in pom.xml
 
<dependencies>
    <!-- Cucumber Core -->
    <dependency>
        <groupId>io.cucumber</groupId>
        <artifactId>cucumber-java</artifactId>
        <version>7.14.0</version>
    </dependency>

    <!-- Cucumber JUnit -->
    <dependency>
        <groupId>io.cucumber</groupId>
        <artifactId>cucumber-junit</artifactId>
        <version>7.14.0</version>
        <scope>test</scope>
    </dependency>

    <!-- JUnit -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.13.2</version>
        <scope>test</scope>
    </dependency>
</dependencies>


Step 5: Create Folder Structure

src
└── test ├── java │ └── stepDefinitions │ └── runners └── resources └── features



features → store .feature files
stepDefinitions → write step implementation
runners → write test runner using JUnit


Step 6: Sample Files


Feature File (Login.feature)

Feature: Login Functionality

  Scenario: Successful login with valid credentials
    Given User is on Login page
    When User enters valid username and password
    Then User should be redirected to homepage




Step Definition (LoginSteps.java)

package stepDefinitions;

import io.cucumber.java.en.*;

public class LoginSteps {
    @Given("User is on Login page")
    public void user_on_login_page() {
        System.out.println("User is on Login page");
    }

    @When("User enters valid username and password")
    public void user_enters_credentials() {
        System.out.println("Entered valid credentials");
    }

    @Then("User should be redirected to homepage")
    public void user_redirected_homepage() {
        System.out.println("Redirected to homepage");
    }
}




Test Runner (TestRunner.java)

package runners;

import org.junit.runner.RunWith;
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;

@RunWith(Cucumber.class)
@CucumberOptions(
    features = "src/test/resources/features",
    glue = "stepDefinitions"
)
public class TestRunner {
}


Step 7: Run Test

  • Right-click on TestRunner.java → Run As → JUnit Test



For IntelliJ IDEA:

Step 1: Install IntelliJ IDEA

Step 2: Install Cucumber Plugin

  • Open IntelliJ → Settings → Plugins

  • Search: Cucumber for Java

  • Click Install and restart



Step 3: Create a Maven Project

  • File → New → Project
  • Choose Maven
  • Set name and location, click Finish


Step 4: Add Cucumber Dependencies to pom.xml as mentioned above


Step 5: Create Folder Structure & Add Files

Same as explained above. IntelliJ will automatically recognize resources and .feature files if placed correctly.

You can use IntelliJ features like:

  • Alt+Enter on undefined steps → to auto-create step definitions.

  • Right-click on .feature file → Run to execute.



Important Points for both IDEs:
  • Glue in @CucumberOptions must point to the package containing step definitions.

  • .feature files must be UTF-8 encoded.

  • You can also use Cucumber with TestNG, just add relevant dependencies and use AbstractTestNGCucumberTests.

No comments:

Post a Comment