org.testng.TestNGException:无法实例化黄瓜测试运行器

时间:2018-10-12 22:35:21

标签: java maven selenium testing cucumber

嗨,我正在尝试使用testng运行我的黄瓜框架,我非常确定我是一个版本问题,但是如果有人可以在此处指出问题,我会很感激

功能:

Feature: Application Login

Scenario: Home page default login
Given user is on landing page
When user logins into the application with username "jon" and password 
"1234"
Then Home page is displayed
And Cards displayed "true"

Scenario: Home page default login2
Given user is on landing page
When user logins into the application with username "john" and password 
"4321"
Then Home page is displayed
And Cards displayed "false"

定义:

package StepDefinations;

import cucumber.api.PendingException;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.When;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.And;



public class LoginDefination {

    @Given("^user is on landing page$")
    public void user_is_on_landing_page() throws Throwable {
        //code
        System.out.println("on landing page");
        }


    @When("^user logins into the application with username and password$")
    public void user_logins_into_the_application_with_username_and_password() throws Throwable {
        //code
        System.out.println("logging in");

    }

    @When("^user logins into the application with username \"([^\"]*)\" and password \"([^\"]*)\"$")
    public void user_logins_into_the_application_with_something_and_password_something(String strArg1, String strArg2) throws Throwable {
        System.out.println(strArg1 +" "+ strArg2);
    }

    @Then("^Home page is displayed$")
    public void home_page_is_displayed() throws Throwable {
        //code
        System.out.println("hello homepage");
    }

    @And("^Cards are displayed$")
    public void cards_are_displayed() throws Throwable {
        //code
        System.out.println("hello cards");
    }

    @And("^Cards are not displayed$")
    public void cards_are_not_displayed() throws Throwable {
        //code
        System.out.println("hello not cards");
    }

    @And("^Cards displayed \"([^\"]*)\"$")
    public void cards_displayed(String args) throws Throwable {
        //code
        System.out.println("this will or will not display the cards - " + args);
    }

}

测试运行器文件:

package CucumberOptions;

import cucumber.api.CucumberOptions;
import cucumber.api.testng.AbstractTestNGCucumberTests;

//Feature

@CucumberOptions(
        features = "src\\test\\java\\features",
        //if u want tp execute everything then just give the path till the package level
        glue= "StepDefinations"//Package name
        )
public class TestRunner extends AbstractTestNGCucumberTests {

}

testng xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
  <test name="cucumber testing 101">
  <classes>
  <class name="CucumberOptions.testme"/>
  <class name="CucumberOptions.TestRunner"/>

  </classes>
  </test> <!-- Test -->
</suite> <!-- Suite -->

maven pom.xml文件:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>Cucumber</groupId>
  <artifactId>Automation</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>Automation</name>
  <url>http://maven.apache.org</url>


  <build>
  <resources>

 <resource>
        <directory>src/main/java/resources</directory>
        <filtering>true</filtering>
 </resource>

  </resources>
  <plugins>

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.22.0</version>
        <configuration>
          <suiteXmlFiles>
            <suiteXmlFile>testng.xml</suiteXmlFile>
          </suiteXmlFiles>
        </configuration>
      </plugin>

</plugins>
  </build>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>

  <!-- https://mvnrepository.com/artifact/info.cukes/cucumber-java -->
<dependency>
    <groupId>info.cukes</groupId>
    <artifactId>cucumber-java</artifactId>
    <version>1.2.5</version>
</dependency>



  <!-- https://mvnrepository.com/artifact/info.cukes/cucumber-testng -->
<dependency>
    <groupId>info.cukes</groupId>
    <artifactId>cucumber-testng</artifactId>
    <version>1.1.5</version>
</dependency>


  <dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>3.14.0</version>
</dependency>


<dependency>
    <groupId>org.testng</groupId>
    <artifactId>testng</artifactId>
    <version>6.10</version>
    <scope>test</scope>
</dependency> 




  </dependencies>
</project>

错误:

[RemoteTestNG] detected TestNG version 6.10.0
org.testng.TestNGException: 
Cannot instantiate class CucumberOptions.TestRunner
    at org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:40)
    at org.testng.internal.ClassHelper.createInstance1(ClassHelper.java:363)
    at org.testng.internal.ClassHelper.createInstance(ClassHelper.java:275)
    at org.testng.internal.ClassImpl.getDefaultInstance(ClassImpl.java:126)
    at org.testng.internal.ClassImpl.getInstances(ClassImpl.java:191)
    at org.testng.TestClass.getInstances(TestClass.java:100)
    at org.testng.TestClass.initTestClassesAndInstances(TestClass.java:86)
    at org.testng.TestClass.init(TestClass.java:78)
    at org.testng.TestClass.<init>(TestClass.java:41)
    at org.testng.TestRunner.initMethods(TestRunner.java:425)
    at org.testng.TestRunner.init(TestRunner.java:252)
    at org.testng.TestRunner.init(TestRunner.java:222)
    at org.testng.TestRunner.<init>(TestRunner.java:171)
    at org.testng.remote.support.RemoteTestNG6_10$1.newTestRunner(RemoteTestNG6_10.java:28)
    at org.testng.remote.support.RemoteTestNG6_10$DelegatingTestRunnerFactory.newTestRunner(RemoteTestNG6_10.java:61)
    at org.testng.SuiteRunner$ProxyTestRunnerFactory.newTestRunner(SuiteRunner.java:623)
    at org.testng.SuiteRunner.init(SuiteRunner.java:189)
    at org.testng.SuiteRunner.<init>(SuiteRunner.java:136)
    at org.testng.TestNG.createSuiteRunner(TestNG.java:1375)
    at org.testng.TestNG.createSuiteRunners(TestNG.java:1355)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1209)
    at org.testng.TestNG.runSuites(TestNG.java:1133)
    at org.testng.TestNG.run(TestNG.java:1104)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
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 org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:29)
    ... 25 more
Caused by: java.lang.NoSuchMethodError: cucumber.runtime.RuntimeOptionsFactory.<init>(Ljava/lang/Class;[Ljava/lang/Class;)V
    at cucumber.api.testng.AbstractTestNGCucumberTests.<init>(AbstractTestNGCucumberTests.java:27)
    at CucumberOptions.TestRunner.<init>(TestRunner.java:14)
    ... 30 more

它是一段简单的代码,但是我只是想让它使用testng然后是maven,如果有人可以帮助我,我将非常感激:)

1 个答案:

答案 0 :(得分:0)

TestNG和Cucumber库的不兼容版本。在依赖关系树中验证它们的版本,并检查哪种TestNG版本适合您的Cucumber库版本,例如:

更新:我注意到您实际上已经发布了pom.xml

cucumber-testng的版本更新为1.2.5(使用与cucumber-java相同的版本)。使用testng版本6.9.10(如果您不介意的话)。这样,您将对齐所有库版本。

已确认:您在cucumber-testng版本与版本之间存在冲突。 1.1.5和cucumber-java版。 1.2.5。将两个库都设置为版本1.1.5,或将它们更新为使用版本1.2.5