如何在多个并行线程中运行单个 testng 测试用例 [英] How to run a single testng test case in multiple parallel threads

查看:46
本文介绍了如何在多个并行线程中运行单个 testng 测试用例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在任何浏览器中多次并行运行单个测试 NG 测试用例

I want to run single test NG test case in any browser multiple times and parallely

推荐答案

A.1.要多次运行测试用例,请使用 @Test 注释中的 invocationCount 属性.这个 invocationCount 决定了 TestNG 应该运行这个测试方法的次数.

A.1. To run a test case multiple times use invocationCount attribute in @Test annotation. This invocationCount determines how many times TestNG should run this test method.

@Test(invocationCount = ?)

A.2.要在不同线程中多次运行测试用例,请使用 @Test 注释中的 threadPoolSize 属性.该属性告诉TestNG创建一个线程池,通过多线程运行测试方法

A.2. To run a test case mutiple times in different threads use threadPoolSize attribute in @Test annotation. This attribute tells TestNG to create a thread pool to run the test method via multiple threads

@Test(invocationCount = ?, threadPoolSize = ?)

B.3.要在不同线程的多个浏览器中运行测试用例,请将您的 webDriver 初始化为 ThreadLocal

B.3. To run a test case in multiple browsers in different threads initialize your webDriver as ThreadLocal

private static ThreadLocal<WebDriver> webDriver = new ThreadLocal<WebDriver>();

在 testng.xml 中创建以浏览器名称作为参数的测试

In testng.xml create tests with browser name as a parameter

<test name="Test">
    <parameter name="browserName" value="firefox"></parameter>
    <classes>
        <class name="MyTestCases" />
    </classes>
</test>

<test name="Test">
    <parameter name="browserName" value="Chrome"></parameter>
    <classes>
        <class name="MyTestCases" />
    </classes>
</test>

使用@Parameters() 获取浏览器名称的值

use @Parameters() to fetch the value of browser name

@BeforeTest
@Parameters("browserName")
public void webDriverHandler(String browserName){
  // String browserName contains parameter value
}

创建一个驱动程序处理程序和一个测试库类.使用 Driver 处理程序设置和获取 webDriver 并使用您的基类获取浏览器名称

Create a Driver handler and a testbase classes. Use Driver handler to set and get webDriver and use your base class to fetch the browser name

驱动程序处理程序:

public class DriverHandler {

private static ThreadLocal<WebDriver> webDriver = new ThreadLocal<WebDriver>();

public static WebDriver getDriver() {
    return webDriver.get();
}

public static void setWebDriver(String browser) {

     WebDriver driver = null;

     if (browser.contains("firefox")) {

       driver = new FirefoxDriver();

    } else if (browser.contains("chrome")) {

       ChromeOptions options = new ChromeOptions();
       driver = new ChromeDriver(options);  

    }
    webDriver.set(driver);
}


}

测试库:

public class TestBase { 

 @BeforeTest
 @Parameters("browserName")
 public void threadHandler(String browserName) {

    Thread.currentThread().setName(browser.toLowerCase());
    if (DriverManager.getDriver() == null)
        DriverManager.setWebDriver(browser);
 }


}

这篇关于如何在多个并行线程中运行单个 testng 测试用例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆