'Serenity BDD Hooks implementation returns error

I am trying to implement some hooks into the serenity BDD implementation with intellij.

My hooks file looks like this:

package steps;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import io.cucumber.java.Before;
import io.cucumber.java.After;

public class Environment {

    private WebDriver driver;

    @Before
    public void setup(){
        System.setProperty("webdriver.chrome.driver","<pathToDriver>");
        driver = new ChromeDriver();
    }

    @After
    public void tearDown() throws Exception {
        driver.quit();
        Thread.sleep(1000);
    }
    public WebDriver getDriver(){
        return driver;
    }
}

And my step definition file like this:

package steps;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import static org.junit.Assert.fail;

public class EbayHome_steps {

   WebDriver driver;
   
   public EbayHome_steps(Environment environment){
       this.driver = environment.getDriver();
               
   }

    @Given("I am on ebay homepage")
    public void i_am_on_ebay_homepage() {
        driver.get("https://www.ebay.com");
    }

    @When("I click on Advanced Link")
    public void i_click_on_advanced_link()  {
        driver.findElement(By.linkText("Advanced")).click();
    }

    @Then("I navigate to Advanced Search page")
    public void i_navigate_to_advanced_search_page() {
        String expURL = "https://www.ebay.com/sch/ebayadvsearch";
        String actURL = driver.getCurrentUrl();
        if (!expURL.equals(actURL))
        {
            fail("Page does not navigate to expected page");
        }
    }

    @When("I search for Iphone 11")
    public void i_search_for_iphone() {
        driver.findElement(By.xpath("//input[@id='gh-ac']")).sendKeys("iPhone 11");
        driver.findElement(By.xpath("//input[@id='gh-btn']")).click();
    }
    @Then("I have at least 1000 search items returned")
    public void i_have_at_least_search_items_returned() {
        String itemCount = driver.findElement(By.xpath("//*[@id=\"mainContent\"]/div[1]/div/div[2]/div[1]/div[1]/h1/span[1]")).getText().trim();
        String itemCount2 = itemCount.replace(",","");
        int itemCountint  = Integer.parseInt(itemCount2);
        if (!(itemCountint >=1000)){
            fail("Less than 1000 items");
        }
    }

}

The problem is that whenever I try to run the TestRunner, I receive the following error: Short version:

io.cucumber.core.exception.CucumberException: Failed to instantiate class steps.EbayHome_steps - this class doesn't have an empty or a page enabled constructor"

Long version:

 io.cucumber.core.exception.CucumberException: Failed to instantiate class steps.EbayHome_steps - this class doesn't have an empty or a page enabled constructor"
    at cucumber.runtime.SerenityObjectFactory.newInstance(SerenityObjectFactory.java:74)
    at cucumber.runtime.SerenityObjectFactory.cacheNewInstance(SerenityObjectFactory.java:59)
    at cucumber.runtime.SerenityObjectFactory.getInstance(SerenityObjectFactory.java:49)
    at io.cucumber.java.AbstractGlueDefinition.invokeMethod(AbstractGlueDefinition.java:47)
    at io.cucumber.java.JavaStepDefinition.execute(JavaStepDefinition.java:29)
    at io.cucumber.core.runner.CoreStepDefinition.execute(CoreStepDefinition.java:66)
    at io.cucumber.core.runner.PickleStepDefinitionMatch.runStep(PickleStepDefinitionMatch.java:63)
    at io.cucumber.core.runner.ExecutionMode$1.execute(ExecutionMode.java:10)
    at io.cucumber.core.runner.TestStep.executeStep(TestStep.java:86)
    at io.cucumber.core.runner.TestStep.run(TestStep.java:57)
    at io.cucumber.core.runner.PickleStepTestStep.run(PickleStepTestStep.java:51)
    at io.cucumber.core.runner.TestCase.run(TestCase.java:95)
    at io.cucumber.core.runner.Runner.runPickle(Runner.java:75)
    at io.cucumber.junit.PickleRunners$NoStepDescriptions.lambda$run$0(PickleRunners.java:151)
    at io.cucumber.core.runtime.CucumberExecutionContext.lambda$runTestCase$3(CucumberExecutionContext.java:110)
    at io.cucumber.core.runtime.RethrowingThrowableCollector.executeAndThrow(RethrowingThrowableCollector.java:23)
    at io.cucumber.core.runtime.CucumberExecutionContext.runTestCase(CucumberExecutionContext.java:110)
    at io.cucumber.junit.PickleRunners$NoStepDescriptions.run(PickleRunners.java:148)
    at io.cucumber.junit.FeatureRunner.runChild(FeatureRunner.java:144)
    at io.cucumber.junit.FeatureRunner.runChild(FeatureRunner.java:28)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at io.cucumber.junit.FeatureRunner.run(FeatureRunner.java:137)
    at io.cucumber.junit.Cucumber.runChild(Cucumber.java:196)
    at io.cucumber.junit.Cucumber.runChild(Cucumber.java:89)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at io.cucumber.junit.Cucumber$RunBeforeAllHooks.evaluate(Cucumber.java:266)
    at io.cucumber.junit.Cucumber$RunAfterAllHooks.evaluate(Cucumber.java:281)
    at io.cucumber.junit.Cucumber$StartTestRun.evaluate(Cucumber.java:233)
    at io.cucumber.junit.Cucumber$FinishTestRun.evaluate(Cucumber.java:248)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
    at com.intellij.rt.junit.IdeaTestRunner$Repeater$1.execute(IdeaTestRunner.java:38)
    at com.intellij.rt.execution.junit.TestsRepeater.repeat(TestsRepeater.java:11)
    at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:35)
    at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:235)
    at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54)
Caused by: java.lang.NoSuchMethodException: steps.EbayHome_steps.<init>()
    at java.base/java.lang.Class.getConstructor0(Class.java:3349)
    at java.base/java.lang.Class.getConstructor(Class.java:2151)
    at cucumber.runtime.SerenityObjectFactory.newInstance(SerenityObjectFactory.java:70)
    ... 45 more

  When I click on Advanced Link           # steps.EbayHome_steps.i_click_on_advanced_link()
  Then I navigate to Advanced Search page # steps.EbayHome_steps.i_navigate_to_advanced_search_page()

io.cucumber.core.exception.CucumberException: Failed to instantiate class steps.EbayHome_steps - this class doesn't have an empty or a page enabled constructor"

    at cucumber.runtime.SerenityObjectFactory.newInstance(SerenityObjectFactory.java:74)
    at cucumber.runtime.SerenityObjectFactory.cacheNewInstance(SerenityObjectFactory.java:59)
    at cucumber.runtime.SerenityObjectFactory.getInstance(SerenityObjectFactory.java:49)
    at io.cucumber.java.AbstractGlueDefinition.invokeMethod(AbstractGlueDefinition.java:47)
    at io.cucumber.java.JavaStepDefinition.execute(JavaStepDefinition.java:29)
    at io.cucumber.core.runner.CoreStepDefinition.execute(CoreStepDefinition.java:66)
    at io.cucumber.core.runner.PickleStepDefinitionMatch.runStep(PickleStepDefinitionMatch.java:63)
    at io.cucumber.core.runner.ExecutionMode$1.execute(ExecutionMode.java:10)
    at io.cucumber.core.runner.TestStep.executeStep(TestStep.java:86)
    at io.cucumber.core.runner.TestStep.run(TestStep.java:57)
    at io.cucumber.core.runner.PickleStepTestStep.run(PickleStepTestStep.java:51)
    at io.cucumber.core.runner.TestCase.run(TestCase.java:95)
    at io.cucumber.core.runner.Runner.runPickle(Runner.java:75)
    at io.cucumber.junit.PickleRunners$NoStepDescriptions.lambda$run$0(PickleRunners.java:151)
    at io.cucumber.core.runtime.CucumberExecutionContext.lambda$runTestCase$3(CucumberExecutionContext.java:110)
    at io.cucumber.core.runtime.RethrowingThrowableCollector.executeAndThrow(RethrowingThrowableCollector.java:23)
    at io.cucumber.core.runtime.CucumberExecutionContext.runTestCase(CucumberExecutionContext.java:110)
    at io.cucumber.junit.PickleRunners$NoStepDescriptions.run(PickleRunners.java:148)
    at io.cucumber.junit.FeatureRunner.runChild(FeatureRunner.java:144)
    at io.cucumber.junit.FeatureRunner.runChild(FeatureRunner.java:28)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at io.cucumber.junit.FeatureRunner.run(FeatureRunner.java:137)
    at io.cucumber.junit.Cucumber.runChild(Cucumber.java:196)
    at io.cucumber.junit.Cucumber.runChild(Cucumber.java:89)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at io.cucumber.junit.Cucumber$RunBeforeAllHooks.evaluate(Cucumber.java:266)
    at io.cucumber.junit.Cucumber$RunAfterAllHooks.evaluate(Cucumber.java:281)
    at io.cucumber.junit.Cucumber$StartTestRun.evaluate(Cucumber.java:233)
    at io.cucumber.junit.Cucumber$FinishTestRun.evaluate(Cucumber.java:248)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
    at com.intellij.rt.junit.IdeaTestRunner$Repeater$1.execute(IdeaTestRunner.java:38)
    at com.intellij.rt.execution.junit.TestsRepeater.repeat(TestsRepeater.java:11)
    at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:35)
    at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:235)
    at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54)
Caused by: java.lang.NoSuchMethodException: steps.EbayHome_steps.<init>()
    at java.base/java.lang.Class.getConstructor0(Class.java:3349)
    at java.base/java.lang.Class.getConstructor(Class.java:2151)
    at cucumber.runtime.SerenityObjectFactory.newInstance(SerenityObjectFactory.java:70)
    ... 45 more


Process finished with exit code 255

I have searched all the tutorials possible but none have given me a glimpse of how to fix it. From what I see it doesn't see the default class constructor and it cannot create the object.

Does someone know how to fix this issue?



Solution 1:[1]

? have solved the issue through making the constructor in the step definition file empty, defining the driver variable as static in the hook definition file and instantiating as a class object in the step definition file where ? called the getDriver method.

?t seems that serenity needs an empty constructor in the step definition file.

Solution 2:[2]

I looked back from begin cours than realize that : cucumber-picocontainer 7.3.2 and serenity-cucumber4 2.1.2 dependency can resolved issue

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Gabriela R
Solution 2 vecelo