简体   繁体   English

在詹金斯(Jenkins)建立TestG maven项目时遇到问题

[英]Having issue building TestG maven project in Jenkins

I have an issue with building TestNG based Maven project via Jenkins. 我通过Jenkins构建基于TestNG的Maven项目时遇到问题。 The issue is that if I have less number of test to run (up to 30) everything runs fine, but as soon as I increase the number, the build fails consistently. 问题是,如果我要运行的测试数量较少(最多30个),则一切运行正常,但是一旦我增加测试数量,构建就会始终失败。 The problem I see is that, tests doesn't start until all the browser instance are open (eg, browser opens 30 windows first then start execution for suite of 30 tests) and when the test gets complete(not entire test suite), the browser window doesn't close (even after adding driver.quit() method) and it remains open till the TestSuite run is complete. 我看到的问题是,直到打开所有浏览器实例(例如,浏览器首先打开30个窗口,然后针对30个测试套件开始执行)之后,测试才会开始,并且当测试完成(而不是整个测试套件)时,浏览器窗口不会关闭(即使在添加driver.quit()方法之后),并且在TestSuite运行完成之前它将保持打开状态。

Below is snippet of my code. 以下是我的代码段。

Main Java class: 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-->
    }
}

Sample test class: 示例测试类:

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();
    }
}

Here is my testng.xml: 这是我的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>

Here is my pom.xml file: 这是我的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>

And below is the error I am getting: 下面是我得到的错误:

[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

You are creating driver instance whenever for every class inside a test and quitting driver only once per test.Instead of instantiating WebDriver directly in class do it in @BeforeTest. 您可以随时为测试中的每个类创建驱动程序实例,并且每个测试仅退出一次驱动程序,而不是直接在类中实例化WebDriver,而是在@BeforeTest中进行创建。 It will ensure only one driver is created per test. 这将确保每个测试仅创建一个驱动程序。

Your class whould look like below after upation. 升级后,您的班级如下所示。

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-->
 }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM