我在 Cucumber Java 中得到 io.cucumber.java.InvalidMethodSignatureException

I am getting io.cucumber.java.InvalidMethodSignatureException in Cucumber Java

My Feature file -
Feature: Open google page
 
  @Sanity
  Scenario: Open google search
    Given User open google search
    Then enter text "hi" in search bar
    And close the browser 

###########################################################################

Page Object class -
package pageObjects;

import org.openqa.selenium.Keys;    
import org.openqa.selenium.WebDriver;    
import org.openqa.selenium.WebElement;      
import org.openqa.selenium.support.CacheLookup;   
import org.openqa.selenium.support.FindBy;   
import org.openqa.selenium.support.PageFactory;

public class GoogleObjects {
    
    public WebDriver ldriver;
    
    public GoogleObjects(WebDriver rdriver) {
        
        ldriver = rdriver;
        PageFactory.initElements(rdriver, this);
    }

    @FindBy(name = "q")
    @CacheLookup
    WebElement search;
    
    
    public void Search(String searchTxt) {
        
        search.sendKeys(searchTxt);
        search.sendKeys(Keys.DOWN, Keys.ENTER);
        
    }
    
    public void btnClick() {
        
        ldriver.close();
        
    }
}

############################################################################

StepDifinition class - 
package stepDefinitions;

import org.openqa.selenium.WebDriver;    
import io.cucumber.java.en.*;    
import pageObjects.GoogleObjects;    

public class GoogleSearch {

    public WebDriver driver;

    
    @Given("^User open google search$")
    public void user_open_google_search() throws Throwable {
        
        driver.get("https://www.google.com");
       
    }


    @Then("^enter text \"([^\"]*)\" in search bar$")
    public void enter_text_something_in_search_bar(String TxT) throws Throwable {
        
        GoogleObjects gb = new GoogleObjects(driver);
        gb.Search(TxT);
    }
    
    @And("^close the browser$")
    public void close_the_browser() throws Throwable {
        
        GoogleObjects gb = new GoogleObjects(driver);
        gb.close();
    }
}
##########################################################################

Test Runner class -
package runner;

import org.junit.runner.RunWith;
import io.cucumber.junit.Cucumber;    
import io.cucumber.junit.CucumberOptions;    

@RunWith(Cucumber.class)  
@CucumberOptions(  
                    features = {".//Features/google.feature"},   
                    glue = {"stepDefinitions"},    
                    monochrome=true,    
                    tags = "@Sanity",    
                    plugin = {"pretty","html:target/reports.html" ,  
  "json:target/reports.json"}   
    )

public class testRunner {

}
######################################################################

hooks class - 
package stepDefinitions;

import java.io.IOException;      
import org.openqa.selenium.WebDriver;     
import org.openqa.selenium.firefox.FirefoxDriver;     
import io.cucumber.java.After;    
import io.cucumber.java.Before;   

public class hooks {

        WebDriver driver;
    
        @Before
        public WebDriver setUp() throws IOException {
            
            System.setProperty("webdriver.chrome.driver","*:\****\***\Drivers\chromedriver.exe");
            
            driver = new ChromeDriver();
            
            return driver;
        }
        
        @After
        public void tearDown() {
            
            driver.quit();
        
        }
    }

############################################# ############## 我用黄瓜框架创建了一个Maven项目并编写了一个功能文件,步骤 定义、测试 运行ner 和挂钩文件。 当我 运行 我的测试 运行ner 我得到“io.cucumber.java.InvalidMethodSignatureException”,当我导入 io.cucumber.java.before/after 但我的测试是 运行ning当我从 junit 导入时,如果我们从 junit 导入,@before 和 @after 在黄瓜中不起作用。我不明白为什么会收到 InvalidMethodSignatureException。 我尝试为 @Before 注释提供 order=0 。它不起作用 以上是代码,请帮帮我。

你遇到这个问题是因为用 io.cucumber.java.Before 注释的方法必须是 void.

在您的示例中 returns WebDriver。这就是为什么你得到 InvalidMethodSignatureException.