Ora

How Do I Run a Cucumber File in IntelliJ?

Published in Cucumber Test Automation 6 mins read

Running Cucumber files in IntelliJ IDEA is streamlined when you have the correct environment set up, including necessary plugins and run configurations. This guide will focus on running Cucumber.js feature files, as well as providing a general understanding of how to execute Cucumber tests within the IDE.

1. Essential Prerequisites & Setup

Before running your Cucumber tests, ensure your IntelliJ IDEA environment and project are properly configured.

Node.js Installation

For projects utilizing Cucumber.js, installing Node.js is fundamental. Cucumber.js versions 6.0 and above rely on Node.js to execute your tests. You can download the installer from the official Node.js website and ensure it's installed on your system.

IntelliJ IDEA Plugin Configuration

IntelliJ IDEA leverages specific plugins to provide robust support for Cucumber feature files (Gherkin syntax) and JavaScript/TypeScript step definitions.

Plugin Name Purpose Check Status
Gherkin Provides syntax highlighting, code completion, and navigation for .feature files. Settings/Preferences > Plugins > Installed
JavaScript and TypeScript Essential for JavaScript and TypeScript language support, including step definitions. Settings/Preferences > Plugins > Installed
Cucumber (General Support) Ensures comprehensive integration for Cucumber, often aiding Gherkin processing and various runners. Settings/Preferences > Plugins > Installed

To enable these plugins:

  1. Navigate to File (or IntelliJ IDEA on macOS) > Settings/Preferences....
  2. Go to Plugins > Installed tab.
  3. Search for Gherkin, JavaScript and TypeScript, and Cucumber.
  4. Ensure their checkboxes are selected. If a plugin is not installed, go to the Marketplace tab, search for it, and click Install.
  5. Restart IntelliJ IDEA if prompted to apply the changes.

Project-Specific Setup

Within your project, if you're using Cucumber.js:

  • Ensure @cucumber/cucumber is installed as a development dependency. You can install it using npm or yarn:
    npm install --save-dev @cucumber/cucumber
    # OR
    yarn add --dev @cucumber/cucumber
  • You should have .feature files (written in Gherkin syntax) and corresponding JavaScript/TypeScript step definition files that implement the steps.

2. Running Your Cucumber Feature Files

IntelliJ IDEA offers several convenient ways to execute your Cucumber tests, catering to different scenarios and project types.

Method 1: Running from a .feature File (Gutter Icon)

The quickest way to run a single feature or scenario is directly from the editor:

  1. Open the .feature file you wish to run in the IntelliJ editor.

  2. Look for the green 'Play' icon in the left-hand gutter next to the Feature keyword, or alongside an individual Scenario or Scenario Outline.

  3. Click the green icon. This will present options like Run 'Feature: [Your Feature Name]' or Run 'Scenario: [Your Scenario Name]'. Select the appropriate run option.

    • Practical Insight: IntelliJ will automatically create a temporary run configuration, often inferring the correct runner based on your project's dependencies (e.g., a Node.js run configuration for Cucumber.js projects, or a JUnit/TestNG configuration for Java projects).

Method 2: Creating a Dedicated Run Configuration

For more control, to run multiple features, or to specify particular tags or profiles, creating a persistent run configuration is ideal.

For Cucumber.js Projects (Using Node.js Run Configuration)

This method allows you to configure specific cucumber.js commands to run your tests.

  1. Go to Run > Edit Configurations....
  2. Click the + icon in the top left corner to add a new configuration.
  3. Select Node.js from the list.
  4. Configure the settings in the dialog:
    • Name: Give your configuration a descriptive name (e.g., Run All Cucumber.js Features or Cucumber Smoke Tests).
    • Node interpreter: Select your installed Node.js interpreter (IntelliJ usually detects it automatically).
    • Working directory: Set this to your project root or where your package.json resides.
    • JavaScript file: Browse and select the cucumber executable from your project's node_modules directory. This is typically located at [Your Project Root]/node_modules/@cucumber/cucumber/bin/cucumber.js.
    • Application parameters: Here you specify the arguments for the cucumber.js command. Common parameters include:
      • Paths to your feature files (e.g., features/**/*.feature).
      • Paths to your step definitions (e.g., --require step-definitions/**/*.js).
      • Formatters (e.g., --format progress, --format html:reports/cucumber-report.html).
      • Tags (e.g., --tags "@smoke").
    • Example Parameters: --require features/step_definitions/*.js features (assuming your step definitions are in features/step_definitions and your feature files are in the features folder).
  5. Click Apply, then OK.
  6. You can now select this configuration from the run configurations dropdown menu in the top toolbar and click the Run button.

For Java/JVM Projects (Brief Mention)

While the focus of this guide is on Cucumber.js, for Java-based Cucumber projects, you would typically create a Cucumber Java run configuration (provided by the Cucumber for Java plugin) or a standard JUnit / TestNG run configuration that points to your test runner class.

Method 3: Running via IntelliJ's Integrated Terminal

You can always execute cucumber-js commands directly within IntelliJ's built-in Terminal tool window:

  1. Open the Terminal tab at the bottom of the IntelliJ IDEA window.

  2. Ensure you are in your project's root directory or the directory where your package.json is located.

  3. Execute your Cucumber commands. If you have defined scripts in your package.json (e.g., "test:cucumber": "cucumber-js --require step-definitions/*.js features"), you can use npm run or yarn:

    # Example using npm script:
    npm run test:cucumber
    
    # Or direct cucumber.js command (ensure 'cucumber' is in your PATH or specify full path):
    ./node_modules/.bin/cucumber-js --require features/step_definitions/*.js features/**/*.feature

3. Practical Tips and Troubleshooting

  • Step Definition Mapping: IntelliJ often highlights unmatched steps in yellow or provides a quick-fix option. Ensure your step definition files are correctly located and associated with your feature files.
  • Node.js Version: Verify your project's package.json specifies a compatible Node.js version, especially for Cucumber.js 6.0 and above.
  • Paths in Run Configurations: Double-check all file and directory paths in your run configurations, especially for feature files and step definitions (--require arguments). Incorrect paths are a common source of errors.
  • Output Window: Always check the Run tool window for output messages, errors, and stack traces if your tests fail. This provides crucial debugging information.

By following these steps, you can effectively run and manage your Cucumber feature files within IntelliJ IDEA, leveraging its powerful integration features to streamline your testing workflow.