gpt4 book ai didi

java - TestNG 与 selenium 驱动工厂并行执行

转载 作者:行者123 更新时间:2023-12-02 11:41:25 24 4
gpt4 key购买 nike

尝试为我正在构建的框架设置驱动程序工厂作为学习工具。然而,我正在努力在 testng 中并行执行测试类。

我正在使用surefire插件

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20.1</version>
<configuration>
<properties>
<suiteXmlFiles>
<suiteXmlFile>src/main/resources/master.xml</suiteXmlFile>
</suiteXmlFiles>
</properties>
</configuration>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectj.version}</version>
</dependency>
</dependencies>
</plugin>

我使用的testng版本是:

<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.9.10</version>
<scope>test</scope>
</dependency>

master.xml如下:

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Parallel test suite" parallel="classes" thread-count="2">
<test name="Test 1">
<classes>
<class name="testsuite.TestCase1"/>
<class name="testsuite.TestCase2"/>
</classes>
</test>
</suite>

为了在此处发布,我简化了驱动程序工厂:

public class DriverFactory
{
//protected WebDriver driver;

private DriverFactory()
{
//Do-nothing..Do not allow to initializethis class from outside
}
private static DriverFactory instance = new DriverFactory();

public static DriverFactory getInstance()
{
return instance;
}

ThreadLocal<WebDriver> driver = new ThreadLocal<WebDriver>() // thread local driver object for webdriver
{
@Override
protected WebDriver initialValue()
{
System.setProperty("webdriver.chrome.driver",
"src/main/resources/chromedriver.exe");
return new ChromeDriver(); // can be replaced with other browser drivers
}
};

public WebDriver getDriver() // call this method to get the driver object and launch the browser
{
return driver.get();
}

public void removeDriver() // Quits the driver and closes the browser
{
driver.get().quit();
driver.remove();
}
}

使用以下mvn命令运行:

mvn 干净测试

我知道这对我来说是一件愚蠢的事情,因为我阅读了几乎所有与此相关的教程、博客文章和文档。

非常感谢任何帮助,即使只是为我指出正确的方向。

最佳答案

TestNG 允许您并行运行测试,包括 JUnit 测试。为此,您必须设置并行参数,并且如果默认值 5 不够,则可以更改 threadCount 参数。您必须设置配置参数:

 <configuration>
<parallel>methods</parallel>
<threadCount>10</threadCount>
</configuration>

给你:

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20.1</version>
<configuration>
<parallel>methods</parallel>
<threadCount>10</threadCount>
<properties>
<suiteXmlFiles>
<suiteXmlFile>src/main/resources/master.xml</suiteXmlFile>
</suiteXmlFiles>
</properties>
</configuration>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectj.version}</version>
</dependency>
</dependencies>
</plugin>

更新

尝试以下操作

<suite name="Parallel test suite" parallel="classes" thread-count="2">
<test name="Test 1">
<classes>
<class name="testsuite.TestCase1"/>
</classes>
</test>
<test name="Test 2">
<classes>
<class name="testsuite.TestCase2"/>
</classes>
</test>

关于java - TestNG 与 selenium 驱动工厂并行执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48521585/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com