简体   繁体   English

多线程TestNG DataProvider执行

[英]Multithreaded TestNG DataProvider Execution

I am trying for multithreaded test via TestNG. 我正在尝试通过TestNG进行多线程测试。 @BeforeMethod instantiates the WebDrivers for the test @AfterMethod closes the WebDrivers after the test. @BeforeMethod实例化测试的WebDrivers @AfterMethod在测试后关闭WebDrivers。 @Dataprovider provides the data for the test to run multiple times in loop @Dataprovider为测试提供数据以使其在循环中多次运行

 import java.lang.reflect.Method;

 import org.openqa.selenium.By;
 import org.openqa.selenium.WebDriver;
 import org.openqa.selenium.chrome.ChromeDriver;
 import org.testng.annotations.AfterMethod;
 import org.testng.annotations.AfterSuite;
 import org.testng.annotations.BeforeMethod;
 import org.testng.annotations.DataProvider;
 import org.testng.annotations.Test;

 public class MultipleSession {
     private WebDriver driver;

     @BeforeMethod
     public void beforeMethod() {
         System.err.println("Before ID" + Thread.currentThread().getId());
         System.setProperty("webdriver.chrome.driver", "chromedriver.exe");
         if (driver == null) {
             driver = new ChromeDriver();

             driver.get("https://www.google.co.in/");
         }

     }

     @DataProvider(name = "sessionDataProvider", parallel = true)
     public static Object[][] sessionDataProvider(Method method) {
         int len = 12;

         Object[][] parameters = new Object[len][1];
         for (int i = 0; i < len; i++) {
             parameters[i][0] = i;

         }
         return parameters;
     }

     @Test(dataProvider = "sessionDataProvider")
     public void executSessionOne(int input) {
         System.err.println("Test ID" + Thread.currentThread().getId());



         driver.findElement(By.name("q")).sendKeys(input + "");
         try {
             Thread.sleep(5000);
         } catch (InterruptedException e) {

             e.printStackTrace();
         }

     }

     @AfterMethod
     public void afterMethod() {
         System.err.println("After ID" + Thread.currentThread().getId());
     }

     @AfterSuite
     public void afterSuti() {
         driver.close();
         driver.quit();
     }
  }

My problem is , Even though TestNG opens 10 browsers at a time, the tasks are carried out only by one browser while other 9 browsers do nothing. 我的问题是,即使TestNG一次打开10个浏览器,任务也只能由一个浏览器执行,而其他9个浏览器则什么也不做。 How do i distribute the instances of Webdriver declared in beforeMethod to all the threads? 如何将beforeMethod中声明的Webdriver实例分发到所有线程?

Thanks in advance. 提前致谢。

private WebDriver driver;

This line means that there is only a single instance of webdriver, driver = new ChromeDriver(); 这行表示只有一个webdriver实例, driver = new ChromeDriver(); is instantiating the same driver object again and again. 一次又一次实例化同一驱动程序对象。

To Solve this problem, create a factory and ask for driver object from the factory in every @Test method.Sample code of the factory will be something like 要解决此问题,请创建工厂并在每个@Test方法中向工厂询问驱动程序对象。工厂的示例代码将类似于

static synchronized RemoteWebDriver getDriver()
{
System.setProperty("webdriver.chrome.driver", "chromedriver.exe");
         if (driver == null) {
             driver = new ChromeDriver()
}

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

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