无法实例化测试步骤类

时间:2017-12-21 13:18:48

标签: java selenium cucumber

我在项目中添加了以下jar文件:

  1. gherkin 2.12
  2. cucumber-junit 1.2.5
  3. cucumber-java 1.2.5
  4. 黄瓜核心1.2.5
  5. cucumber jvm-deps 1.0.5
  6. cobetura 2.1.1
  7. mockito all 1.9.1
  8. cucumber-reporting 3.13
  9. 当我尝试运行测试运行器类时出现错误:

     cucumber.runtime.CucumberException: Failed to instantiate class stepDefinition.Test_steps
    

    测试跑步者类

    package CucumberTest;
    
    import org.junit.runner.RunWith;
    import cucumber.api.junit.Cucumber;
    import cucumber.api.CucumberOptions;
    
    @RunWith(Cucumber.class)
    @CucumberOptions(
            features = "Feature"
            ,glue={"stepDefinition"}
            )
    
    public class TestRunner {
    
    
    }
    

    Test_steps类

    package stepDefinition;
    
    import java.util.concurrent.TimeUnit;
    
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.support.ui.ExpectedConditions;
    import org.openqa.selenium.support.ui.WebDriverWait;
    import PageObject.LoginVariables;
    import cucumber.api.PendingException;
    import cucumber.api.java.en.Given;
    import cucumber.api.java.en.Then;
    import cucumber.api.java.en.When;
    
    
    public class Test_steps {
    
        private static WebDriver driver = null;
        WebDriverWait wait = new WebDriverWait(driver, 60);
    
        @Given("^User is on Homepage$")
        public void user_is_on_Homepage() throws Throwable {
            // Write code here that turns the phrase above into concrete actions
            driver = new ChromeDriver();
            System.setProperty("webdriver.chrome.driver", "C:\\Selenium\\chromedriver.exe");
            driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
            driver.get("http://test.salesforce.com");
    
            throw new PendingException();
        }
    
        @When("^User Enters userId and Password$")
        public void user_Enters_userId_and_Password() throws Throwable {
            // Write code here that turns the phrase above into concrete actions
    
            wait.until(ExpectedConditions
                    .visibilityOfElementLocated(By.xpath(LoginVariables.login_xpath)))
                    .sendKeys("username@mail.com");
    
            wait.until(ExpectedConditions
                    .visibilityOfElementLocated(By.xpath(LoginVariables.password_xpath)))
                    .sendKeys("password");
    
            wait.until(ExpectedConditions
                    .visibilityOfElementLocated(By.xpath(LoginVariables.login_xpath))).click();
    
            throw new PendingException();
        }
    
        @Then("^User is able to login sucessfully in salesorce$")
        public void user_is_able_to_login_sucessfully_in_salesorce() throws Throwable {
            // Write code here that turns the phrase above into concrete actions
    
            System.out.println("User Login Sucessful");
            throw new PendingException();
        }
    
    }
    

    文件夹结构: Folder Structure

3 个答案:

答案 0 :(得分:2)

问题出现在下面代码的第二行,因为"驱动程序"当"等待"被初始化并导致NullPointerException。

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

你应该初始化"驱动程序"在挂钩或初始化"等待"在"驱动程序"之后已初始化。

答案 1 :(得分:0)

在我看来你的文件夹结构是错误的,我将TestRunner放在测试包的顶部,而不是src包。 pageobjects和stepdef也属于测试包而不是src,并且功能文件属于test resources文件夹。

答案 2 :(得分:0)

第一次看@AndréBarbosa的答案看起来很有希望。但是,您需要注意下面提到的所有这些方面:

  • 当您尝试将WebDriver实例强制转换为WebDriverWait当驱动程序 null 时,如下所示,这应该给您一个 NullPointerException 运行时。但似乎我们甚至没有到达这里。

    private static WebDriver driver = null;
    WebDriverWait wait = new WebDriverWait(driver, 60);
    
  • 正如您使用WebDriverWait一样,您应完全删除ImplicitlyWait documentation明确说明:

    WARNING: Do not mix implicit and explicit waits. Doing so can cause unpredictable wait times. For example setting an implicit wait of 10 seconds and an explicit wait of 15 seconds, could cause a timeout to occur after 20 seconds.
    
  • 理想情况下,每个元素(JavaScript / AJAX Call除外)都不会需要60秒才能显示/可点击/可互动。因此,您可以动态如下:

    new WebDriverWait(driver, 5).until(ExpectedConditions.visibilityOfElementLocated(By.id("element_xpath"))).sendKeys("value");
    
  • Feature Folder 对我来说是主要关注点。 Feature 文件夹和 LogIn.feature 文件看起来未使用。如果您从其他项目中移动或复制了 Feature 文件夹,则可能会发生这种情况。因此,解决方案是完全删除 Feature 文件夹,并创建一个新的 Feature 文件夹和 LogIn.feature 仅通过您的 IDE (不是通过其他软件NotepadTextpadSubLime3)提交下面的图片(New - > File):

New_Feature_File_Folder

  • Project Workspace 中清除 IDE 并执行Test

  • CCleaner之前和之后运行 Test Execution 工具,以清除所有操作系统杂务。

  

注意:请勿move / copy功能file(s) / directory(ies)删除 unwanted并通过 IDE 创建