cucumber.runtime.CucumberException:无法实例化类

时间:2016-09-27 13:21:24

标签: maven selenium-webdriver cucumber automated-tests testng-eclipse

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

" cucumber.runtime.CucumberException:cucumber.runtime.CucumberException:无法实例化类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 =&#34; target / cucumber.json&#34;,

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");

        }

最后我的功能/登录功能

@login
Feature: Test Login

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

1 个答案:

答案 0 :(得分:0)

所以,最近我根据这篇伟大的帖子重新构建了我的代码: https://rationaleemotions.wordpress.com/2013/07/31/parallel-webdriver-executions-using-testng/

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