cucumber.runtime.CucumberException: 实例化失败 class

cucumber.runtime.CucumberException: Failed to instantiate class

在我的项目中,我正在尝试使用 (Cucumber + TestNG + Maven[=40= 构建框架]). 在我尝试使用 testng.xml 文件在不同浏览器上并行进行 运行 测试之前,一切都运行良好。 当我尝试 运行 和 testng.xml 时,我收到以下错误:

"cucumber.runtime.CucumberException: cucumber.runtime.CucumberException: Failed to instantiate class testSteps.TestLoginFun"

我觉得我的逻辑有问题,或者类之间的联系,但我找不到。

这是我的testng.xml的内容:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<!--  <suite name="Suite" parallel="false"> -->
<suite name="SuiteTestNG" parallel="tests">



<test name="Test on Firefox">
   <parameter name="browser" value="Firefox" />
   <classes>
          <class name="runner.LoginTestRunner" />
    </classes>
</test>

<test name="Test on Chrome">
   <parameter name="browser" value="Chrome" />
   <classes>
        <class name="runner.LoginTestRunner" />
   </classes>
</test>
</suite>

这是我的 MavenCucumberProject.pom:

    <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>MavenCucumberProject</groupId>
  <artifactId>MavenCucumberProject</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <!-- <packaging>jar</packaging> -->

 <!-- Change from here -->

<dependencies>

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

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

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.11</version>
    </dependency>

    <dependency>
       <groupId>info.cukes</groupId>
       <artifactId>cucumber-testng</artifactId>
       <version>1.2.2</version>
    </dependency>

    <dependency>
        <groupId>info.cukes</groupId>
        <artifactId>cucumber-junit</artifactId>
        <version>1.2.2</version>
    </dependency>

    <dependency>
        <groupId>info.cukes</groupId>
        <artifactId>cucumber-core</artifactId>
        <version>1.2.2</version>
    </dependency>

    <dependency>
        <groupId>info.cukes</groupId>
        <artifactId>cucumber-java</artifactId>
        <version>1.2.2</version>
    </dependency>

    <dependency>
         <groupId>com.github.mkolisnyk</groupId>
         <artifactId>cucumber-reports</artifactId>
         <version>1.0.6</version>
    </dependency>

    <dependency>
        <groupId>info.cukes</groupId>
        <artifactId>gherkin</artifactId>
        <version>2.12.2</version>
    </dependency>

    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-all</artifactId>
        <version>1.9.5</version>
    </dependency>

    <dependency>
      <groupId>org.apache.poi</groupId>
      <artifactId>poi</artifactId>
      <version>3.10.1</version>
    </dependency>

    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-chrome-driver</artifactId>
        <version>2.53.1</version>
    </dependency>

    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-firefox-driver</artifactId>
        <version>2.53.1</version>
    </dependency>

    <dependency>
          <groupId>com.google.guava</groupId>
          <artifactId>guava</artifactId>
          <version>18.0</version>
     </dependency>

      <dependency>
          <groupId>org.hamcrest</groupId>
          <artifactId>hamcrest-all</artifactId>
          <version>1.3</version>
      </dependency>


      <dependency>
        <groupId>org.apache.maven.surefire</groupId>
        <artifactId>surefire-testng</artifactId>
        <version>2.18.1</version>
      </dependency>
</dependencies>

<build>
 <pluginManagement>
        <plugins>

            <plugin>

                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.0</version>
                <configuration>
                    <compilerVersion>1.8</compilerVersion>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>


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

            <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-source-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>attach-sources</id>
                                <goals>
                                    <goal>jar</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>



        </plugins>

    </pluginManagement>

</build>

  <repositories>
    <repository>
      <id>forplay-legacy</id>
      <url>http://forplay.googlecode.com/svn/mavenrepo</url>
    </repository>
  </repositories>

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


</project>

这是我的 LoginTestRunner.java:

@RunWith(ExtendedCucumber.class)

@ExtendedCucumberOptions(jsonReport = "target/cucumber.json",

overviewReport = true,
outputFolder = "target")
@CucumberOptions(
        features="Features",
        tags = "@login", 
        glue="testSteps",
        plugin={"html:target/cucumber-html-report",
        "json:target/cucumber.json", "pretty:target/cucumber-pretty.txt",
        "usage:target/cucumber-usage.json", "junit:target/cucumber-results.xml"})

public class LoginTestRunner extends AbstractTestNGCucumberTests{

    public WebDriver driver;



    @Parameters({"browser"})
    @BeforeTest
    public void chooseBrowser(String browser){
        try {
            if (browser.equalsIgnoreCase("Firefox")) {
               driver = new FirefoxDriver();
               System.out.println("Running Firefox");
            } else if (browser.equalsIgnoreCase("Chrome")) {
               System.setProperty("webdriver.chrome.driver", "C:\chromedriver.exe");
               driver = new ChromeDriver();
               System.out.println("Running Chrome");
            } 
        }
            catch (WebDriverException e) {
                System.out.println(e.getMessage());
            }
    }

    public WebDriver getDriver(){

        driver.navigate().to("https://www.google.com");
        driver.manage().window().maximize();
        return driver;
    }


    public void closeDriver() {

        driver.quit();   

}

}

And my **TestLoginFun.java**:

    public class TestLoginFun extends LoginTestRunner{

        WebDriver driver;
        CucumberResultsOverview results = new CucumberResultsOverview();
        CucumberUsageReporting report = new CucumberUsageReporting();

        @BeforeSuite
        public void reports() throws Exception{

            results.setOutputDirectory("target");
            results.setOutputName("cucumber-results");
            results.setSourceFile("./src/test/resources/cucumber.json");
            report.setOutputDirectory("target");
            report.setJsonUsageFile("target/cucumber.json");
            report.executeReport();
        }


         public TestLoginFun(){

             this.driver= getDriver();
         }



        @Given("^A user accessed the url https://www.google.com$")
        public void a_user_accessed_the_url_https_www_google_com() throws Throwable {

            driver.get("https://www.google.com");

        }

最后是我的 Features/Login.feature:

@login
Feature: Test Login

  Scenario: Verify that all login fileds and objects are available
    Given A user accessed the url https://www.google.com

所以,最近我根据这里 post 这个很棒的代码重构了我的代码: https://rationaleemotions.wordpress.com/2013/07/31/parallel-webdriver-executions-using-testng/

此 link 指导您使用 TestNG 构建并行 WebDriver 执行。