黄瓜无法实例化类,步骤未定义

时间:2018-09-18 11:25:57

标签: selenium selenium-webdriver cucumber cucumber-java cucumber-junit

每次尝试使用含硒的黄瓜进行测试时,我都会不断遇到这些错误:

cucumber.runtime.CucumberException: Failed to instantiate class br.gov.pb.receita.atfbdd.steps.arrecadacao.ConsultarLancamentoARR001 - this class doesn't have an empty or a page enabled constructor"
    at cucumber.runtime.SerenityObjectFactory.newInstance(SerenityObjectFactory.java:66)
    at cucumber.runtime.SerenityObjectFactory.cacheNewInstance(SerenityObjectFactory.java:51)
    at cucumber.runtime.SerenityObjectFactory.getInstance(SerenityObjectFactory.java:41)
    at cucumber.runtime.java.JavaStepDefinition.execute(JavaStepDefinition.java:48)
    at cucumber.runtime.PickleStepDefinitionMatch.runStep(PickleStepDefinitionMatch.java:50)
    at cucumber.runner.TestStep.executeStep(TestStep.java:55)
    at cucumber.runner.TestStep.run(TestStep.java:42)
    at cucumber.runner.PickleStepTestStep.run(PickleStepTestStep.java:53)
    at cucumber.runner.TestCase.run(TestCase.java:47)
    at cucumber.runner.Runner.runPickle(Runner.java:44)
    at cucumber.runtime.junit.PickleRunners$NoStepDescriptions.run(PickleRunners.java:140)
    at cucumber.runtime.junit.FeatureRunner.runChild(FeatureRunner.java:68)
    at cucumber.runtime.junit.FeatureRunner.runChild(FeatureRunner.java:23)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at cucumber.runtime.junit.FeatureRunner.run(FeatureRunner.java:73)
    at net.serenitybdd.cucumber.CucumberWithSerenity.runChild(CucumberWithSerenity.java:144)
    at net.serenitybdd.cucumber.CucumberWithSerenity.runChild(CucumberWithSerenity.java:45)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at net.serenitybdd.cucumber.CucumberWithSerenity$1.evaluate(CucumberWithSerenity.java:153)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at cucumber.runtime.SerenityObjectFactory.newInstance(SerenityObjectFactory.java:63)
    ... 36 more
Caused by: java.lang.NullPointerException
    at br.gov.pb.receita.atfbdd.steps.arrecadacao.ConsultarLancamentoARR001.<init>(ConsultarLancamentoARR001.java:34)
    ... 41 more

cucumber.runtime.junit.UndefinedThrowable: The step "Eu solicito a funcionalidade Consultar Lan�amento" is undefined

cucumber.runtime.junit.UndefinedThrowable: The step "Eu informar os dados para consulta" is undefined

cucumber.runtime.junit.UndefinedThrowable: The step "O sistema valida as informa��es e executa a consulta" is undefined

cucumber.runtime.junit.UndefinedThrowable: The step "O sistema exibe uma lista contendo os lan�amentos obtidos" is undefined

我尝试运行步骤类 whith和whitout ScenarioSteps,并且您可以看到,它确实有一个空的构造函数。

但是,即使在我的步骤类中扩展了ScenarioSteps,我仍然不断收到这些错误“ cucumber.runtime.junit.UndefinedThrowable:步骤未定义”,如上所示。

这是我的阶梯课:

@CucumberOptions(features = "classpath:features/arrecadacao/")
public class ConsultarLancamentoARR001 extends ScenarioSteps {

    Selenium selenium;

    public ConsultarLancamentoARR001() {

    }

    ConsultarLancamentoPage consultarLancamento = new ConsultarLancamentoPage(selenium.getDriver());
    LoginPage loginPage = new LoginPage(selenium.getDriver());

    @Step
    @Given("^Eu desejo acessar o sistema ATF$")
    public void euDesejoAcessarAtf() throws Exception {
        selenium.getDriver().navigate().to(ConfiguracaoATF.URL_ADDRESS_DES);
    }

    @Step
    @When("^Eu informo as credenciais de login$")
    public void euInformoCredenciaisLogin() throws Exception {
        loginPage.logarSistemaViaCredenciaisUsuario(DadosConfigARR.USUARIO_ADMIN_DES, DadosConfigARR.SENHA_ADMIN_DES);
    }

    @Step
    @Then("^Eu devo logar no sistema com sucesso$")
    public void euLogoComSucesso() throws Exception {
        assertEquals("Lotação:", selenium.getDriver().findElement(By.xpath("/html/body/table/tbody/tr[2]/td/table/tbody/tr[3]/td[1]")));
    }

    @Step
    @Given("^Eu solicito a funcionalidade Consultar Lançamento$")
    public void euSolicitoConsultarLancamento() throws Exception {
        consultarLancamento.acessarFuncionalidadeARR_001();
    }

    @Step
    @When("^Eu informar os dados para consulta$")
    public void euInformoDadosConsulta() throws Throwable {
        consultarLancamento.informarNumeroLancamento(DadosConfigARR.NOSSO_NUMERO_REG1);
        consultarLancamento.descerBarraDeRolagemCompletamente();
        consultarLancamento.clicarBotaoConsultarLancamento();
        Thread.sleep(2000);

    }

    @Step
    @Then("^O sistema valida as informações e executa a consulta$")
    public void oSistemaValidaInformacoes() throws Throwable {
        consultarLancamento.naoSelecionarNenhumItem();
        Thread.sleep(2000);

        assertEquals("Selecione um item", consultarLancamento.capturarPopupAlerta());

        consultarLancamento.detalharLancamento();
        Thread.sleep(5000);
    }

    @Step
    @And("^O sistema exibe uma lista contendo os lançamentos obtidos$")
    public void oSistemaExibeLancamentos() throws Throwable {
        consultarLancamento.detalharLancamento();
        Thread.sleep(5000);
    }

}

这是我的跑步者:

@RunWith(CucumberWithSerenity.class)
@CucumberOptions(
        features = "src/test/resources/features/arrecadacao/",
        glue = "classpath:br.gov.pb.receita.atfbdd.steps.arrecadacao")

public class CucumberRunnerARR {

    protected static WebDriver driver;

    @BeforeClass
    public static void beforeClass() throws Exception {
        driver = Selenium.getInstance().getDriver();
        driver.manage().window().maximize();
//      LoginPage loginPage = new LoginPage(driver);
//      loginPage.logarSistemaViaCredenciaisUsuario(DadosConfigARR.USUARIO_ADMIN_DES, DadosConfigARR.SENHA_ADMIN_DES);
    }

    @AfterClass
    public static void afterClass() throws Exception {
        driver.quit();
    }
}

这是我的.feature:

Feature: Consultar lançamento

  O usuário consulta um lançamento no sistema ATF

  Background: O usuário está logado
    Given Eu desejo acessar o sistema ATF
    When Eu informo as credenciais de login
    Then Eu devo logar no sistema com sucesso

  @CT015
  Scenario: Fluxo básico de eventos
    Given Eu solicito a funcionalidade Consultar Lançamento
    When Eu informar os dados para consulta
    Then O sistema valida as informações e executa a consulta
    And O sistema exibe uma lista contendo os lançamentos obtidos

并且,如果有帮助的话,这是项目的组织方式:

project explorer

我在做什么错了?

1 个答案:

答案 0 :(得分:1)

很确定我遇到了这个问题,下面是我的package.json文件,请确保您使用的是所有版本的相同版本,我相信某个版本太高或某个版本太低,我不记得该怎么回事是的。

{
  "name": "hellocucumber",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "cls && cucumber-js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "chai": "^4.1.2",
    "chromedriver": "^2.41.0",
    "cucumber": "^4.2.1",
    "node-key-sender": "^1.0.10",
    "selenium-cucumber-js": "^1.5.13"
  },
  "dependencies": {
    "gherkin": "^5.1.0",
    "selenium-webdriver": "^4.0.0-alpha.1"
  }
}

我知道您没有使用package.json文件,但是如果使用我的版本号,您应该能够确定这些版本是否与您导入到项目中的版本相同。