cucumber.runtime.CucumberException:未能实例化stepDefinitions类

cucumber.runtime.CucumberException:未能实例化stepDefinitions类,cucumber,Cucumber,我正在尝试运行用java编写的cucumber测试 一切似乎都正常运行,除了在实现功能文件之后,我开始出现以下错误:cucumber.runtime.CucumberException:未能实例化stepDefinitions类 package runners; import cucumber.api.CucumberOptions; import cucumber.api.junit.Cucumber; import org.junit.runner.RunWith; @RunWith(

我正在尝试运行用java编写的cucumber测试

一切似乎都正常运行,除了在实现功能文件之后,我开始出现以下错误:cucumber.runtime.CucumberException:未能实例化stepDefinitions类

 package runners;

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

@RunWith(Cucumber.class)
@CucumberOptions(
        features = "src/test/resources/FeatureFile",
        glue = {"stepDefinitions"}
)

public class TestRunner {

}
package stepDefinitions;
import cucumber.api.java.en.Given;
    import cucumber.api.java.en.Then;
    import cucumber.api.java.en.When;
    import org.junit.Assert;
    import org.openqa.selenium.By;
    import org.openqa.selenium.Keys;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.support.ui.ExpectedConditions;
    import org.openqa.selenium.support.ui.WebDriverWait;
    import java.util.concurrent.TimeUnit;
    public class Steps {

            private static WebDriver driver;
            WebDriverWait wait=new WebDriverWait(driver, 20);

            @Given("^I naviagte to login page$")
            public void i_naviagte_to_login_page() {
                System.setProperty("webdriver.chrome.driver", "E:\\TestingJAR\\chromedriver_win32\\chromedriver.exe");
                driver = new ChromeDriver();
                driver.manage().window().maximize();
                driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
                driver.get("http://www.facebook.com");
            }

            @When("^I enter username as operqa(\\d+)$")
            public void i_enter_username_as_operqa(String usr) {
                WebElement username = driver.findElement(By.name("username"));
                username.sendKeys(usr);
                username.sendKeys(Keys.TAB);
            }

            @When("^I enter password as (\\d+)$")
            public void i_enter_password_as(String pass) {
                WebElement tipoValidacion;
                tipoValidacion =  wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id=\"tipoValidacionDiv\"]/div/div/div/span[3]")));
                tipoValidacion.click();
                driver.findElement(By.xpath("//*[@id=\"tipoValidacionDiv\"]/div/div/div/span[1]")).click();

                WebElement password;
                password = wait.until(ExpectedConditions.visibilityOfElementLocated(By.name("password")));
                password.sendKeys(pass);
            }

            @When("^I press Ingresar button$")
            public void i_press_Ingresar_button() {
                driver.findElement(By.xpath("//BUTTON[@type='submit']")).click();
            }

            @Then("^I should be able to login successfully$")
            public void i_should_be_able_to_login_successfully() {
                 WebElement loginSuccess =  driver.findElement(By.xpath("//H1[@class=''][text()='Home']"));
                 Assert.assertEquals(loginSuccess.getText(),"Home");
            }
        }
项目结构如下:

StepDefinitons类如下所示。首先我运行feature文件,然后我得到了需要在stepDefinitons类中实现的方法

 package runners;

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

@RunWith(Cucumber.class)
@CucumberOptions(
        features = "src/test/resources/FeatureFile",
        glue = {"stepDefinitions"}
)

public class TestRunner {

}
package stepDefinitions;
import cucumber.api.java.en.Given;
    import cucumber.api.java.en.Then;
    import cucumber.api.java.en.When;
    import org.junit.Assert;
    import org.openqa.selenium.By;
    import org.openqa.selenium.Keys;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.support.ui.ExpectedConditions;
    import org.openqa.selenium.support.ui.WebDriverWait;
    import java.util.concurrent.TimeUnit;
    public class Steps {

            private static WebDriver driver;
            WebDriverWait wait=new WebDriverWait(driver, 20);

            @Given("^I naviagte to login page$")
            public void i_naviagte_to_login_page() {
                System.setProperty("webdriver.chrome.driver", "E:\\TestingJAR\\chromedriver_win32\\chromedriver.exe");
                driver = new ChromeDriver();
                driver.manage().window().maximize();
                driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
                driver.get("http://www.facebook.com");
            }

            @When("^I enter username as operqa(\\d+)$")
            public void i_enter_username_as_operqa(String usr) {
                WebElement username = driver.findElement(By.name("username"));
                username.sendKeys(usr);
                username.sendKeys(Keys.TAB);
            }

            @When("^I enter password as (\\d+)$")
            public void i_enter_password_as(String pass) {
                WebElement tipoValidacion;
                tipoValidacion =  wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id=\"tipoValidacionDiv\"]/div/div/div/span[3]")));
                tipoValidacion.click();
                driver.findElement(By.xpath("//*[@id=\"tipoValidacionDiv\"]/div/div/div/span[1]")).click();

                WebElement password;
                password = wait.until(ExpectedConditions.visibilityOfElementLocated(By.name("password")));
                password.sendKeys(pass);
            }

            @When("^I press Ingresar button$")
            public void i_press_Ingresar_button() {
                driver.findElement(By.xpath("//BUTTON[@type='submit']")).click();
            }

            @Then("^I should be able to login successfully$")
            public void i_should_be_able_to_login_successfully() {
                 WebElement loginSuccess =  driver.findElement(By.xpath("//H1[@class=''][text()='Home']"));
                 Assert.assertEquals(loginSuccess.getText(),"Home");
            }
        }

实例化失败,因为无法使用默认构造函数创建步骤为的新对象

public class Steps {

    private static WebDriver driver;
    WebDriverWait wait=new WebDriverWait(driver, 20);

    ...
因为
driver
尚未在
newwebdriverwait(driver,20)
初始化

要初始化
驱动程序
,可以使用
@Before
(在场景之前执行)或
@BeforeStep
(在步骤之前执行)注释方法

查看相关文档


实例化失败,因为无法使用默认构造函数创建步骤
的新对象

public class Steps {

    private static WebDriver driver;
    WebDriverWait wait=new WebDriverWait(driver, 20);

    ...
因为
driver
尚未在
newwebdriverwait(driver,20)
初始化

要初始化
驱动程序
,可以使用
@Before
(在场景之前执行)或
@BeforeStep
(在步骤之前执行)注释方法

查看相关文档


您能提供
Steps.java
类的内容吗?您是否尝试过在
stepDefinitions
包中没有任何类的情况下运行它?你应该得到要实现的方法的列表。是的,我得到了,它要求我实现方法,然后它停止工作,然后你应该提供
步骤。java
。我已经添加了我的Steps.java类你能提供
Steps.java
类的内容吗?您是否尝试过在
stepDefinitions
包中没有任何类的情况下运行它?你应该得到要实现的方法的列表。是的,我得到了,它要求我实现方法,然后它停止工作,然后你应该提供
步骤。java
。否则没人能猜出它失败的原因