Cannot instantiate test steps class

时光总嘲笑我的痴心妄想 提交于 2019-12-12 07:04:33

问题


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("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 WebDriver instance to WebDriverWait when 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 WebDriverWait you should remove the ImplicitlyWait totally 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 Folder looks to me as the main concern. The Feature folder and LogIn.feature file looks unused. This may happen if you have moved or copied the Feature folder from another project. Hence the solution will be to delete the Feature folder totally and create a new Feature folder and LogIn.feature file through your IDE only (not through other softwares Notepad or Textpad or SubLime3) as per the image below (New -> File) :

  • Clean up you Project Workspace within your IDE and execute the Test

  • Run CCleaner tool before and after Test Execution to wipe away all the OS chores.

Note : Do not move/copy feature file(s)/directory(ies). Delete the unwanted and create a new one through your IDE only.



来源:https://stackoverflow.com/questions/47925875/cannot-instantiate-test-steps-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!