Gherkin Parser

Gherkin Parser

Maven Central Quality Gate Status Coverage Reliability Rating Security Rating

A lightweight Java library for parsing Gherkin feature files into a structured AST (Abstract Syntax Tree). Built with Java 21 and designed for easy integration with testing frameworks and tooling.

Features

Requirements

Installation

Maven

<dependency>
    <groupId>org.myjtools</groupId>
    <artifactId>gherkin-parser</artifactId>
    <version>1.0.0</version>
</dependency>

Gradle

implementation 'org.myjtools:gherkin-parser:1.0.0'

Usage

Basic Parsing

import org.myjtools.gherkinparser.DefaultKeywordMapProvider;
import org.myjtools.gherkinparser.GherkinParser;
import org.myjtools.gherkinparser.elements.GherkinDocument;

// Create parser with default English keywords
var keywordMapProvider = new DefaultKeywordMapProvider();
var parser = new GherkinParser(keywordMapProvider);

// Parse from InputStream
GherkinDocument document = parser.parse(
    getClass().getResourceAsStream("/myFeature.feature")
);

// Parse from Reader
try (var reader = new FileReader("path/to/feature.feature")) {
    GherkinDocument document = parser.parse(reader);
}

Accessing Parsed Elements

var document = parser.parse(inputStream);
var feature = document.feature();

// Feature properties
String name = feature.name();
String description = feature.description();
String language = feature.language();
List<Tag> tags = feature.tags();
List<Comment> comments = feature.comments();

// Iterate scenarios
for (ScenarioDefinition scenario : feature.children()) {
    if (scenario instanceof Scenario s) {
        System.out.println("Scenario: " + s.name());
        for (Step step : s.children()) {
            System.out.println("  " + step.keyword() + step.text());
        }
    }
}

Custom Language Support

You can provide custom keyword mappings for different languages:

// Using a custom KeywordMapProvider
var parser = new GherkinParser(locale -> {
    if (locale.getLanguage().equals("es")) {
        return Optional.of(spanishKeywordMap);
    }
    return Optional.empty();
});

// Or combine multiple providers
var parser = new GherkinParser(List.of(
    new DefaultKeywordMapProvider(),
    new CustomKeywordMapProvider()
));

Supported Elements

ElementRecord ClassDescription
FeatureFeatureThe root element containing scenarios
ScenarioScenarioA single test scenario
Scenario OutlineScenarioOutlineA parameterized scenario template
BackgroundBackgroundSteps executed before each scenario
ExamplesExamplesData table for Scenario Outline
StepStepGiven/When/Then/And/But steps
Data TableDataTableTabular data attached to a step
Doc StringDocStringMulti-line string attached to a step
TagTagMetadata annotations (@tag)
CommentCommentLine comments (#)

Keyword Types

The parser supports all standard Gherkin keywords:

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

Please make sure to update tests as appropriate and follow the existing code style.

Authors

See also the list of contributors who participated in this project.

License

This project is licensed under the MIT License - see the LICENSE file for details.