问题
I have following jar files added to my project :
- gherkin 2.12
- cucumber-junit 1.2.5
- cucumber-java 1.2.5
- cucumber core 1.2.5
- cucumber jvm-deps 1.0.5
- cobetura 2.1.1
- mockito all 1.9.1
- 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("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:
回答1:
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.
回答2:
Looks to me like your folder structure is wrong, I'd place the TestRunner at the top of my test packages rather than the src package. The pageobjects and stepdefs also belong in the test packages rather than src and the feature files belong in the test resources folder.
回答3:
At the first look @AndréBarbosa 's Answer looked pretty promising to me. However you need to take care of all these aspects mentioned below :
As you are trying to cast the
WebDriverinstance toWebDriverWaitwhen the driver is null as below, this should have given you a NullPointerException at Runtime. But seems we are not reaching here even.private static WebDriver driver = null; WebDriverWait wait = new WebDriverWait(driver, 60);As you have used
WebDriverWaityou should remove theImplicitlyWaittotally as documentation clearly says: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.Ideally, each and every element (except the
JavaScript/AJAX Call) won't take 60 seconds to be visible/clickable/interactable. Hence you can be dynamic as follows :new WebDriverWait(driver, 5).until(ExpectedConditions.visibilityOfElementLocated(By.id("element_xpath"))).sendKeys("value");The
Feature Folderlooks to me as the main concern. TheFeaturefolder andLogIn.featurefile looks unused. This may happen if you have moved or copied theFeaturefolder from another project. Hence the solution will be to delete theFeaturefolder totally and create a newFeaturefolder andLogIn.featurefile through yourIDEonly (not through other softwaresNotepadorTextpadorSubLime3) as per the image below (New->File) :
Clean up you
Project Workspacewithin yourIDEand execute theTestRun
CCleanertool before and afterTest Executionto wipe away all the OS chores.
Note : Do not
move/copyfeaturefile(s)/directory(ies). Delete theunwantedand create a new one through yourIDEonly.
来源:https://stackoverflow.com/questions/47925875/cannot-instantiate-test-steps-class