Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
388 views
in Technique[技术] by (71.8m points)

java - Cannot instantiate test steps class

I have following jar files added to my project :

  1. gherkin 2.12
  2. cucumber-junit 1.2.5
  3. cucumber-java 1.2.5
  4. cucumber core 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

I am getting an error when I try to run test runner class:

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

Test Runner Class:

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 Class

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("[email protected]");

        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: Folder Structure

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The issue is on the second line of the code below since the "driver" object is null when the "wait" is initialized and that causes a NullPointerException.

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

You should initialize the "driver" on a Before hook or initialize the "wait" after the "driver" has been initialized.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...