在我的項目,我想建立使用框架(黃瓜 + TestNG的 + Maven的)。 一切都很好,直到我試圖在不同的瀏覽器上使用testng.xml文件並行運行測試。 當我嘗試運行的testng.xml,我收到以下錯誤:cucumber.runtime.CucumberException:無法實例
「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 = 「目標/ 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");
}
最後我特點/ 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