3

I am trying for a multithreaded test via TestNG. @BeforeMethod instantiates the WebDrivers for the test. @AfterMethod closes the WebDrivers after the test. @Dataprovider provides the data for the test to run multiple times in a loop.

 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 afterSuite() {
         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.

How do I distribute the instances of Webdriver declared in beforeMethod to all the threads?

Laurel
  • 5,965
  • 14
  • 31
  • 57
manish
  • 49
  • 2
  • 5
  • TestNG creates the class once and shares fields across test methods. Instead pass the `WebDriver` through the `DataProvider` to scope the variable. – Ben Manes Feb 04 '16 at 17:41
  • @ben If i do it that way, the problem is only one instance of browser is opened and all tasks are carried out in that instance. My requirement is for multiple browsers and all the tasks should be allocated to all opened threads, without closing the browser. – manish Feb 05 '16 at 09:48
  • You probably need to set the test runner to `parallel = 'methods'`, as otherwise the tests are run sequentially. However, if the rest of your test suite is not parallel ready you need to annotate it with `@Test(singleThreaded = true)`. I haven't used WebDriver from Java for a long time, so hopefully multiple can be run per JVM. If not, then you'll have to use the fork approach to split the test across multiple JVMs. – Ben Manes Feb 05 '16 at 19:44

1 Answers1

0
private WebDriver driver;

This line means that there is only a single instance of 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

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