cuming.runtime.CucumberException:无法实例化stepDefinitions类

时间:2018-09-11 04:16:17

标签: cucumber

我正在尝试运行用Java编写的黄瓜测试。

除了实现功能文件后,一切似乎都运行良好,我开始遇到以下错误:cumulan.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 {

}

项目结构如下:

enter image description here

StepDefinitons类如下。首先运行功能文件,然后获取需要在stepDefinitons类中实现的方法。

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");
            }
        }

1 个答案:

答案 0 :(得分:2)

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

public class Steps {

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

    ...

因为driver尚未在new WebDriverWait(driver, 20)初始化。

要初始化driver,可以使用@Before(在方案之前执行)或@BeforeStep(在步骤之前执行)注释的方法。

请参阅相关文档
https://docs.cucumber.io/cucumber/api/#scenario-hooks
https://docs.cucumber.io/cucumber/api/#step-hooks