我在通过Jenkins构建基于TestNG的Maven项目时遇到了问题。问题是,如果我运行的测试数量较少(最多30个),一切运行正常,但是一旦我增加了数量,构建就会一直失败。我看到的问题是,在所有浏览器实例都打开之后测试才会开始(例如,浏览器首先打开30个窗口然后开始执行30个测试套件)并且当测试完成时(不是整个测试套件),浏览器窗口没有关闭(即使在添加了driver.quit()方法之后)它仍然打开,直到TestSuite运行完成。
以下是我的代码片段。
主Java类:
public class AccountManagement {
public WebDriver driver = new FirefoxDriver();
public WebDriverWait wait = new WebDriverWait(driver, 20);
public void login(String uid, String pass) throws Throwable {
driver.manage().window().maximize();
driver.get(URL);
driver.findElement(By.id("Email")).sendKeys(uid);
driver.findElement(By.id("Password")).sendKeys(pass);
driver.findElement(By.id("btnLogin")).click();
<!-- REST OF THE CODE-->
}
}
示例测试类:
public class TC_AC408 extends AccountManagement {
@Test(retryAnalyzer=Retry.class)
public void accountActionLinks() {
login("auto.admin@test.com", "test@123");
<!-- REST OF THE CODE-->
}
@AfterTest
public void end() {
driver.quit();
}
}
这是我的testng.xml:
<suite name="Smoke Test" preserve-order="true" thread-count="1" verbose="1" parallel="false">
<test name="Accounts">
<classes>
<class name="SmokeTests.TC_AC408"></class>
<class name="SmokeTests.TC_AC409"></class>
<class name="SmokeTests.TC_AC410"></class>
<class name="SmokeTests.TC_AC411"></class>
<class name="SmokeTests.TC_AC412"></class>
<class name="SmokeTests.TC_AC413"></class>
<class name="SmokeTests.TC_AC414"></class>
<class name="SmokeTests.TC_AC416"></class>
</classes>
</test>
<test name="Organization">
<classes>
<class name="SmokeTests.TC_AC470"></class>
<class name="SmokeTests.TC_AC474"></class>
<class name="SmokeTests.TC_AC475"></class>
<class name="SmokeTests.TC_AC477"></class>
<class name="SmokeTests.TC_AC478"></class>
</classes>
</test>
<test name="Project Management">
<classes>
<class name="SmokeTests.TC_AC186"></class>
<class name="SmokeTests.TC_AC191"></class>
<class name="SmokeTests.TC_AC193"></class>
<class name="SmokeTests.TC_AC194"></class>
<class name="SmokeTests.TC_AC195"></class>
<class name="SmokeTests.TC_AC196"></class>
<class name="SmokeTests.TC_AC200"></class>
<class name="SmokeTests.TC_AC785"></class>
<class name="SmokeTests.TC_AC786"></class>
<class name="SmokeTests.TC_AC788"></class>
<class name="SmokeTests.TC_AC790"></class>
</classes>
</test>
<test name="Registration">
<classes>
<class name="SmokeTests.TC_AC381"></class>
<class name="SmokeTests.TC_AC385"></class>
<class name="SmokeTests.TC_AC387"></class>
<class name="SmokeTests.TC_AC398"></class>
<class name="SmokeTests.TC_AC386"></class>
<class name="SmokeTests.TC_AC388"></class>
</classes>
</test>
<test name="Product">
<classes>
<class name="SmokeTests.TC_AC482"></class>
<class name="SmokeTests.TC_AC483"></class>
<class name="SmokeTests.TC_AC486"></class>
<class name="SmokeTests.TC_AC487"></class>
<class name="SmokeTests.TC_AC489"></class>
<class name="SmokeTests.TC_AC491"></class>
<class name="SmokeTests.TC_AC492"></class>
<class name="SmokeTests.TC_AC1777"></class>
<class name="SmokeTests.TC_AC1823"></class>
<class name="SmokeTests.TC_AC1949"></class>
</classes>
</test>
<listeners>
<listener class-name="org.uncommons.reportng.HTMLReporter"/>
<listener class-name="org.uncommons.reportng.JUnitXMLReporter"/>
<listener class-name="SmokeTests.RetryListener"/>
</listeners>
</suite>
这是我的pom.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<!--suppress ALL -->
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 h
ttp://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>project_selenium_mvn</groupId>
<artifactId>project_selenium_mvn</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<suiteXmlFile>testng.xml</suiteXmlFile>
</properties>
<dependencies>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.1.1</version>
</dependency>
<!-- Adding Selenium dependency -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-server</artifactId>
<version>2.48.2</version>
</dependency>
<dependency>
<groupId>com.relevantcodes</groupId>
<artifactId>extentreports</artifactId>
<version>2.40</version>
</dependency>
<dependency>
<groupId>org.uncommons</groupId>
<artifactId>reportng</artifactId>
<version>1.1.4</version>
</dependency>
</dependencies>
<build>
<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.17</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>
</build>
</project>
以下是我得到的错误:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.19:test (default-test) on project acutiy_selenium_mvn: Execution default-test of goal org.apache.maven.plugins:maven-surefire-plugin:2.19:test failed: There was an error in the forked process
[ERROR] org.testng.TestNGException:
[ERROR]
[ERROR] Cannot instantiate class SmokeTests.TC_AC193
[ERROR] at org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:37)
[ERROR] at org.testng.internal.ClassHelper.createInstance1(ClassHelper.java:372)
[ERROR] at org.testng.internal.ClassHelper.createInstance(ClassHelper.java:284)
[ERROR] at org.testng.internal.ClassImpl.getDefaultInstance(ClassImpl.java:109)
[ERROR] at org.testng.internal.ClassImpl.getInstances(ClassImpl.java:181)
[ERROR] at org.testng.internal.TestNGClassFinder.<init>(TestNGClassFinder.java:120)
[JENKINS] Archiving C:\AutomatedTest\project_selenium_mvn\pom.xml to project_selenium_mvn/project_selenium_mvn/1.0-SNAPSHOT/project_selenium_mvn-1.0-SNAPSHOT.pom
[ERROR] at org.testng.TestRunner.initMethods(TestRunner.java:402)
[ERROR] at org.testng.TestRunner.init(TestRunner.java:228)
[ERROR] at org.testng.TestRunner.init(TestRunner.java:198)
[ERROR] at org.testng.TestRunner.<init>(TestRunner.java:147)
[ERROR] at org.testng.SuiteRunner$DefaultTestRunnerFactory.newTestRunner(SuiteRunner.java:525)
[ERROR] at org.testng.SuiteRunner.init(SuiteRunner.java:156)
[ERROR] at org.testng.SuiteRunner.<init>(SuiteRunner.java:110)
[ERROR] at org.testng.TestNG.createSuiteRunner(TestNG.java:1235)
[ERROR] at org.testng.TestNG.createSuiteRunners(TestNG.java:1222)
[ERROR] at org.testng.TestNG.runSuitesLocally(TestNG.java:1074)
[ERROR] at org.testng.TestNG.run(TestNG.java:999)
[ERROR] at org.apache.maven.surefire.testng.TestNGExecutor.run(TestNGExecutor.java:282)
[ERROR] at org.apache.maven.surefire.testng.TestNGXmlTestSuite.execute(TestNGXmlTestSuite.java:83)
[ERROR] at org.apache.maven.surefire.testng.TestNGProvider.invoke(TestNGProvider.java:114)
[ERROR] at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:286)
[ERROR] at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:240)
[ERROR] at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:121)
[ERROR] Caused by: java.lang.reflect.InvocationTargetException
[ERROR] at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
[ERROR] at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
[ERROR] at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
[ERROR] at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
[ERROR] at org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:28)
[ERROR] ... 22 more
[ERROR] Caused by: org.openqa.selenium.WebDriverException: Failed to connect to binary FirefoxBinary(C:\Program Files (x86)\Mozilla Firefox\firefox.exe) on port 7079; process output follows:
[ERROR]
[ERROR] Build info: version: '2.48.2', revision: '41bccdd10cf2c0560f637404c2d96164b67d9d67', time: '2015-10-09 13:08:06'
[ERROR] System info: host: 'WIN-O8VQ2P3P2GD', ip: '172.31.62.242', os.name: 'Windows Server 2008 R2', os.arch: 'amd64', os.version: '6.1', java.version: '1.7.0_79'
[ERROR] Driver info: driver.version: FirefoxDriver
[ERROR] at org.openqa.selenium.firefox.internal.NewProfileExtensionConnection.start(NewProfileExtensionConnection.java:135)
[ERROR] at org.openqa.selenium.firefox.FirefoxDriver.startClient(FirefoxDriver.java:271)
[ERROR] at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:117)
[ERROR] at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:216)
[ERROR] at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:211)
[ERROR] at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:207)
[ERROR] at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:120)
[ERROR] at Automation.ProjectManagementPage.<init>(ProjectManagementPage.java:17)
[ERROR] at SmokeTests.TC_AC193.<init>(TC_AC193.java:15)
[ERROR] ... 27 more
[ERROR] Caused by: org.openqa.selenium.firefox.NotConnectedException: Unable to connect to host 127.0.0.1 on port 7079 after 45000 ms. Firefox console output:
[ERROR]
[ERROR] at org.openqa.selenium.firefox.internal.NewProfileExtensionConnection.start(NewProfileExtensionConnection.java:123)
[ERROR] ... 35 more
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/PluginExecutionException
答案 0 :(得分:1)
每次测试时,每个类都要创建驱动程序实例,每次测试只运行一次。而不是直接在类中实例化WebDriver,而是在@BeforeTest中进行。它将确保每次测试只创建一个驱动程序。
你的课程在升级后看起来如下。
public class AccountManagement {
public WebDriver driver;
public WebDriverWait wait;
@BeforeTest
public void beforeTest(){
driver = new FirefoxDriver();
wait = new WebDriverWait(driver, 20);
}
public void login(String uid, String pass) throws Throwable {
driver.manage().window().maximize();
driver.get(URL);
driver.findElement(By.id("Email")).sendKeys(uid);
driver.findElement(By.id("Password")).sendKeys(pass);
driver.findElement(By.id("btnLogin")).click();
<!-- REST OF THE CODE-->
}
}