Home > front end >  You're not allowed to extend classes that define Step Definitions or hooks
You're not allowed to extend classes that define Step Definitions or hooks

Time:11-09

I trying to incorporate pico-container DI into my framework so I can use the @Before and @After tags across multiple step definitions. Please see the error below. Any advice would be great.

You're not allowed to extend classes that define Step Definitions or hooks. class steps.hotelBookingFormPage extends class resources.hooks

import io.cucumber.java.en.Given;
import org.openqa.selenium.internal.ShutdownHooks;
import resources.hooks;

public class hotelBookingFormPage extends hooks {

    public hooks base;
    hotelBookingFormPage (hooks base) {
        base = hooks.startBrowser();
    }

    @Given("I navigate to the hotel booking form page")
    public void iNavigateToTheHotelBookingFormPage() {
        base.driver.get("http://hotel-test.equalexperts.io/");
    }

CodePudding user response:

io.cucumber.java.InvalidMethodException: You're not allowed to extend classes that define Step Definitions or hooks.

Cucumber creates a new instance of all classes defining StepDefinition before each scenario. It then invokes StepDefinition methods on one of those instances whenever it needs to run a step. If I declare a method test in a class and extended to StepDefinition extended to class then two instances will be created and test method will be available on both instances, and cucumber would not be able to decide what instance to invoke the method on. Incase of inheriting to class(which is having hook methods) then you can use composition.

Current way:

class hotelBookingFormPage extends hooks{
 public hooks base;
    hotelBookingFormPage (hooks base) {
        base = hooks.startBrowser();
    }
}

Updated way:

class hotelBookingFormPage{
    hotelBookingFormPage () {
        hooks base = new hooks();
        base.startBrowser();
    }
}

CodePudding user response:

I believe you are trying to use hooks in wrong way. Hooks are not the classes but the annotated methods within any class under your glued path. You should not call them directly.

Where a hook is defined has no impact on what scenarios or steps it is run for.

(c) Cucumber documentation

All the hooks are called each time your scenario/step finishes. If you need to have more flexible control over than you need to use conditional hooks

  • Related