CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-io-cucumber--cucumber-java8

Provides lambda-based step definitions for Cucumber BDD testing framework with Java 8+ functional interfaces

Pending
Overview
Eval results
Files

hooks.mddocs/

Hooks

Test lifecycle hooks for scenario and step-level setup, teardown, and monitoring. Provides comprehensive control over test execution with support for execution order, tag-based filtering, and access to test context through the Scenario object.

Capabilities

Before and After Scenario Hooks

Hooks that execute before and after each scenario, with optional access to scenario context.

/**
 * Before hooks with Scenario parameter - execute before each scenario
 * Hooks with higher order values execute first (default order: 1000)
 */
default void Before(HookBody body) { ... }
default void Before(String tagExpression, HookBody body) { ... }
default void Before(int order, HookBody body) { ... }
default void Before(String tagExpression, int order, HookBody body) { ... }

/**
 * Before hooks without parameters - execute before each scenario
 */
default void Before(HookNoArgsBody body) { ... }
default void Before(String tagExpression, HookNoArgsBody body) { ... }
default void Before(int order, HookNoArgsBody body) { ... }
default void Before(String tagExpression, int order, HookNoArgsBody body) { ... }

/**
 * After hooks with Scenario parameter - execute after each scenario
 * Hooks with higher order values execute first (default order: 1000)
 */
default void After(HookBody body) { ... }
default void After(String tagExpression, HookBody body) { ... }
default void After(int order, HookBody body) { ... }
default void After(String tagExpression, int order, HookBody body) { ... }

/**
 * After hooks without parameters - execute after each scenario
 */
default void After(HookNoArgsBody body) { ... }
default void After(String tagExpression, HookNoArgsBody body) { ... }
default void After(int order, HookNoArgsBody body) { ... }
default void After(String tagExpression, int order, HookNoArgsBody body) { ... }

Usage Examples:

import io.cucumber.java8.En;
import io.cucumber.java8.Scenario;

public class HookDefinitions implements En {
    private WebDriver driver;
    private Database database;
    
    public HookDefinitions() {
        // Basic before hook with scenario access
        Before((Scenario scenario) -> {
            System.out.println("Starting scenario: " + scenario.getName());
            initializeTestData();
        });
        
        // Before hook without scenario parameter
        Before(() -> {
            setupTestEnvironment();
        });
        
        // Before hook with execution order (lower numbers execute first)
        Before(10, (Scenario scenario) -> {
            // This executes before hooks with higher order values
            setupDatabase();
        });
        
        // Before hook with tag filtering - only runs for scenarios tagged with @web
        Before("@web", (Scenario scenario) -> {
            driver = new ChromeDriver();
            driver.manage().window().maximize();
        });
        
        // Before hook with both order and tags
        Before(5, "@api", () -> {
            setupApiClient();
        });
        
        // After hook with cleanup logic
        After((Scenario scenario) -> {
            if (scenario.isFailed()) {
                // Capture screenshot on failure
                if (driver != null) {
                    byte[] screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES);
                    scenario.attach(screenshot, "image/png", "failure-screenshot");
                }
                
                // Log failure details
                scenario.log("Test failed with status: " + scenario.getStatus());
            }
            
            cleanupTestData();
        });
        
        // After hook for specific tags
        After("@web", () -> {
            if (driver != null) {
                driver.quit();
            }
        });
        
        // After hook with execution order (higher numbers execute first)
        After(1000, (Scenario scenario) -> {
            // This executes before hooks with lower order values
            generateTestReport(scenario);
        });
    }
}

Before and After Step Hooks

Hooks that execute before and after each individual step, useful for detailed monitoring and step-level setup/teardown.

/**
 * BeforeStep hooks with Scenario parameter - execute before each step
 */
default void BeforeStep(HookBody body) { ... }
default void BeforeStep(String tagExpression, HookBody body) { ... }
default void BeforeStep(int order, HookBody body) { ... }
default void BeforeStep(String tagExpression, int order, HookBody body) { ... }

/**
 * BeforeStep hooks without parameters - execute before each step
 */
default void BeforeStep(HookNoArgsBody body) { ... }
default void BeforeStep(String tagExpression, HookNoArgsBody body) { ... }
default void BeforeStep(int order, HookNoArgsBody body) { ... }
default void BeforeStep(String tagExpression, int order, HookNoArgsBody body) { ... }

/**
 * AfterStep hooks with Scenario parameter - execute after each step
 */
default void AfterStep(HookBody body) { ... }
default void AfterStep(String tagExpression, HookBody body) { ... }
default void AfterStep(int order, HookBody body) { ... }
default void AfterStep(String tagExpression, int order, HookBody body) { ... }

/**
 * AfterStep hooks without parameters - execute after each step
 */
default void AfterStep(HookNoArgsBody body) { ... }
default void AfterStep(String tagExpression, HookNoArgsBody body) { ... }
default void AfterStep(int order, HookNoArgsBody body) { ... }
default void AfterStep(String tagExpression, int order, HookNoArgsBody body) { ... }

Usage Examples:

public class StepHookDefinitions implements En {
    
    public StepHookDefinitions() {
        // Log each step execution
        BeforeStep((Scenario scenario) -> {
            System.out.println("About to execute step in scenario: " + scenario.getName());
        });
        
        // Capture step timing
        BeforeStep(() -> {
            stepStartTime = System.currentTimeMillis();
        });
        
        // Log step completion and timing
        AfterStep((Scenario scenario) -> {
            long duration = System.currentTimeMillis() - stepStartTime;
            scenario.log("Step completed in " + duration + "ms");
        });
        
        // Screenshot after each step for UI tests
        AfterStep("@ui", (Scenario scenario) -> {
            if (driver != null) {
                byte[] screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES);
                scenario.attach(screenshot, "image/png", "step-screenshot");
            }
        });
        
        // Monitor for step failures
        AfterStep((Scenario scenario) -> {
            if (scenario.isFailed()) {
                logStepFailure(scenario);
            }
        });
    }
}

Hook Body Functional Interfaces

Functional interfaces defining the lambda signatures for hook implementations.

/**
 * Hook body with Scenario parameter providing access to test context
 */
@FunctionalInterface
public interface HookBody {
    /**
     * Execute hook with access to scenario information
     * @param scenario Current scenario context with metadata and utilities
     * @throws Throwable Any exception during hook execution
     */
    void accept(Scenario scenario) throws Throwable;
}

/**
 * Hook body without parameters for simple setup/teardown operations
 */
@FunctionalInterface
public interface HookNoArgsBody {
    /**
     * Execute hook without scenario context
     * @throws Throwable Any exception during hook execution
     */
    void accept() throws Throwable;
}

Tag Expressions

Hooks support Cucumber tag expressions for conditional execution based on scenario tags.

Tag Expression Syntax:

// Single tag
Before("@web", () -> { ... });

// Multiple tags with AND
Before("@web and @slow", () -> { ... });

// Multiple tags with OR  
Before("@web or @api", () -> { ... });

// Tag negation
Before("not @skip", () -> { ... });

// Complex expressions with parentheses
Before("(@web or @mobile) and not @skip", () -> { ... });

Usage Examples:

public class TaggedHooks implements En {
    
    public TaggedHooks() {
        // Run only for web UI tests
        Before("@web", () -> {
            setupWebDriver();
        });
        
        // Run for API tests but not integration tests
        Before("@api and not @integration", () -> {
            setupApiMocks();
        });
        
        // Run for either database or performance tests
        Before("@database or @performance", () -> {
            setupDatabaseConnection();
        });
        
        // Complex conditional setup
        Before("(@smoke or @regression) and not @skip", (Scenario scenario) -> {
            setupTestEnvironment(scenario.getSourceTagNames());
        });
    }
}

Hook Execution Order

Hooks with the same trigger point (Before/After/BeforeStep/AfterStep) are executed in order based on their order parameter:

  • Before hooks: Higher order values execute first (descending order)
  • After hooks: Higher order values execute first (descending order)
  • Default order: 1000 if not specified
  • Same order: Execution order is undefined for hooks with identical order values
public class OrderedHooks implements En {
    
    public OrderedHooks() {
        // Execute first (order 2000 - highest)
        Before(2000, () -> {
            setupBasicInfrastructure();
        });
        
        // Execute second (default order 1000)
        Before(() -> {
            setupApplicationServices();
        });
        
        // Execute third (order 100 - lowest)
        Before(100, () -> {
            setupTestData();
        });
        
        // Cleanup in reverse order
        
        // Execute first after scenario (order 2000 > 1000 > 100)
        After(2000, () -> {
            generateReports();
        });
        
        // Execute second (default order 1000)
        After(() -> {
            cleanupTestData();
        });
        
        // Execute last (order 100)
        After(100, () -> {
            shutdownApplicationServices();
        });
    }
}

Hook Constants

/**
 * Constants for hook configuration
 */
public interface LambdaGlue {
    /** Empty tag expression - matches all scenarios */
    String EMPTY_TAG_EXPRESSION = "";
    
    /** Default execution order for Before hooks */
    int DEFAULT_BEFORE_ORDER = 1000;
    
    /** Default execution order for After hooks */
    int DEFAULT_AFTER_ORDER = 1000;
}

Install with Tessl CLI

npx tessl i tessl/maven-io-cucumber--cucumber-java8

docs

hooks.md

index.md

step-definitions.md

test-context.md

transformers.md

tile.json