我并行配置了TestNG测试,但是无法正常工作

时间:2019-05-28 01:02:24

标签: java eclipse selenium-webdriver testng

我该如何调试呢?

遵循SuiteTest.xml(TestNG是这种方式-修复代码段“ thread-count =“ 2” parallel =“ tests”):

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >

<suite name="SuiteTest" thread-count="2" parallel="tests">

<!-- classes tests methods  -->

    <test name="Firefox Test">

        <parameter name="browser" value="FIREFOX" />

        <classes>

            <class name="test.CN01_CT01_IncluirConteudoMP3" />

            <class name="test.CN01_CT02_IncluirConteudoMP4" />

        </classes>

    </test>

</suite>

DriveFactory类:

public class DriverFactory {

    static String path = System.getProperty("user.dir");

    private static ThreadLocal<WebDriver> threadDriver = new ThreadLocal<WebDriver>() {

        @Override

        protected synchronized WebDriver initialValue() {

            return initDriver();

        }

    };

    private DriverFactory() {

    }

    public static WebDriver getDriver() {

        return threadDriver.get();

    }

    public static WebDriver initDriver() {

        WebDriver driver = null;

        switch (Propriedades.browser) {

        case CHROME:

            //executeChrome(BasePage.url);

            break;

        case FIREFOX:

            String driverpathFirefox = path + File.separator + "drivers" + File.separator + "geckodriver.exe";

            System.setProperty("webdriver.gecko.driver", driverpathFirefox);

            driver = new FirefoxDriver();

            getDriver().get(BasePage.url);

            break;

        }

        driver.manage().window().maximize();

        return driver;

    }


    public static void killDriver() {

        WebDriver driver = getDriver();

        if (driver != null) {
            driver.quit();
            driver = null;
        }

        if (threadDriver != null) {
            threadDriver.remove();
            }

    }
}

测试类(非常简单地概括问题):

public class CN01_CT02_IncluirConteudoMP4 extends BaseTest {

    @Test

    public void CN01_CT02_IncluirConteudoMp4(){

        assertEquals("123", "123");

    }

}

public class CN01_CT01_IncluirConteudoMP3 extends BaseTest {

    @Test

    public void CN01_CT01_IncluirConteudoMp3() throws InterruptedException, AWTException{

        assertEquals("123", "123456");

    }

}

BaseTest类(我在这里登录,注销并关闭浏览器):

public class BaseTest {

    protected static ExtentReports extent;

    public static String testName;



    @Parameters({ "browser" })

    @BeforeMethod

    public void BeforeTest(Method method, Browsers browser) throws IOException, BiffException {

        Propriedades.browser = browser;

        String browserStr = browser.toString();




        //TestName

        testName = method.getName() + "-" + browserStr;



        // Login

        LoginPage.realizarLogin();

        //Start report

        extent = ExtentReport.StartReport(testName);    

    }

    @AfterMethod

    public void finaliza(ITestResult result) throws IOException {


        ExtentReport.ResultTest(result, testName);



        // Logout

        LoginPage.realizarLogout();

        // Closed Browser

        if (Propriedades.FECHAR_BROWSER) {

            killDriver();

        }

    }

    @AfterSuite

    public static void finalizaTudo(){

        DriverFactory.killDriver();

        extent.flush();

    }

}

Pom.xml:

<build>

    <testSourceDirectory>src</testSourceDirectory>

    <plugins>

        <plugin>

            <groupId>org.apache.maven.plugins</groupId>

            <artifactId>maven-surefire-plugin</artifactId>

            <version>2.18.1</version>

            <configuration>

                <includes>

                    <include>/suites/SuiteTest.xml</include>

                </includes>

                <parallel>tests</parallel>

                <threadCount>1</threadCount>

            </configuration>

        </plugin>

    </plugins>

</build>

        <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>6.14.3</version>
        </dependency>

        (..) 

我有一个BasePage类,该类仅实例化DSL,一次在屏幕元素上具有操作。

通过Eclipse运行时(Suite.xml-运行-> TestNG),系统会打开多个浏览器,并且未执行某些操作。

1 个答案:

答案 0 :(得分:1)

您的测试违反了Parallel Testing Best Practices,并且由于synchronized的WebDriver使用性质,您的测试完全不会并行。

解决方法是为每个测试使用单独的JVM实例运行测试,在 pom.xml 中添加以下几行:

<forkCount>2</forkCount>
<reuseForks>false</reuseForks>

更好的方法是重新编写测试,以摆脱static修饰符和同步方法。

更多信息:Fork Options and Parallel Test Execution