title: Parallel execution of test throws exception when parallel is set to true on DataProvider - TestNG
body: <p>Parallel execution of test throws the following exception when parallel=true on DataProvider. Two browser instances will open running on separate threads but only one browser instance will execute successfully and the other one gets stuck on trying to find element and throwing an exception. 
  Please, how do I get the test to execute successfully when parallel=true in dataprovider. I'm I missing something maybe the WebDriver instance in the BasePage. Please help. Thanks.</p>
  
  <pre><code>[PoolService-1] 15:44:06,447 INFO  [Default test] Close driver
  PASSED: loginTest("unresticteduser@gmail.com", "123456")
  FAILED: loginTest("resticteduser@gmail.com", "123456")
  
  WARNING: WebDriverException thrown by findElement(By.xpath: //button[contains(text(), 'NO THANKS')])
  org.openqa.selenium.WebDriverException: org.apache.http.conn.HttpHostConnectException: Connect to 
  localhost:8478 [localhost/127.0.0.1, localhost/0:0:0:0:0:0:0:1] failed: Connection refused: connect
  </code></pre>
  
  <p>When parallel=false on dataprovider, the test will open one browser instance at a time as the data is fed into the test and executes successfully.  </p>
  
  <pre><code>PASSED: loginTest("unresticteduser@gmail.com", "123456")
  PASSED: loginTest("resticteduser@gmail.com", "123456")
  </code></pre>
  
  <p>Here's the code I wrote:</p>
  
  <p>1) BrowserFactory</p>
  
  <pre class="lang-java prettyprint-override"><code>public class BrowserDriverFactory {
  
  private ThreadLocal&lt;WebDriver&gt; driver = new ThreadLocal&lt;WebDriver&gt;();
  private String browser;
  private Logger log;
  
  public BrowserDriverFactory(String browser, Logger log) {
      this.browser = browser.toLowerCase();
      this.log = log;
  }
  
  public WebDriver createDriver() {
      // Create driver
      log.info("Create driver: " + browser);
  
      if(browser.equals("chrome")) {
          System.setProperty("webdriver.chrome.driver", "src/main/resources/chromedriver.exe");
          driver.set(new ChromeDriver());
      }
      else if(browser.equals("firefox")) {
          System.setProperty("webdriver.gecko.driver", "src/main/resources/geckodriver.exe");
          driver.set(new FirefoxDriver());
      }
      else {
          System.out.println("Do not know how to start: " + browser + ", starting chrome.");
          System.setProperty("webdriver.chrome.driver", "src/main/resources/chromedriver.exe");
          driver.set(new ChromeDriver());
      }
  
      return driver.get();
   }
  
  }
  </code></pre>
  
  <p>2)BaseTest</p>
  
  <pre class="lang-java prettyprint-override"><code>public class BaseTest {
  
  protected WebDriver driver;
  public WebDriverWait wait;
  protected Logger log;
  protected FirefoxProfile profile;
  protected String url = "http://www.qaclickacademy.com/";
  
  protected String testSuiteName;
  protected String testName;
  protected String testMethodName;
  
  @BeforeMethod(alwaysRun = false)
  public void setUp(Method method, ITestContext ctx) {
      String testName = ctx.getCurrentXmlTest().getName();
      log = LogManager.getLogger(testName);
  
      BrowserDriverFactory factory = new BrowserDriverFactory(config.getBrowser(), log);
      driver = factory.createDriver();
      driver.get(url);
  
      profile=new FirefoxProfile();
      // Set preferences for file type 
      profile.setPreference("browser.helperApps.neverAsk.openFile", "application/octet-stream");
  
      driver.manage().window().maximize();
      wait = new WebDriverWait(driver, 5);
  
      this.testSuiteName = ctx.getSuite().getName();
      this.testName = testName;
      this.testMethodName = method.getName();
  }
  
      @AfterMethod(alwaysRun = true)
      public void tearDown(ITestResult result) {
  
          if(result.getStatus()==ITestResult.FAILURE) {
          //capture screenshot
          }
  
          log.info("Close driver");
          // Close browser
          driver.quit();
      }
  }
  </code></pre>
  
  <p>3)DataProvider</p>
  
  <pre class="lang-java prettyprint-override"><code> @DataProvider(name="SearchProvider", parallel=true)
      public Object[][] getDataFromDataprovider(){
      return new Object[][] 
          {
              { "unresticteduser@gmail.com", "123456" },
              { "resticteduser@gmail.com", "123456" }
          };
  
      }
  </code></pre>
  
  <p>4)BasePage</p>
  
  <pre class="lang-java prettyprint-override"><code> public class BasePage { 
      protected WebDriver driver;
      public WebDriverWait wait;
      protected Logger log;
  
      public BasePage(WebDriver driver, Logger log) {
          this.driver = driver;
          this.log = log;
          wait = new WebDriverWait(driver, 5);
      }
  
      protected void sleep(long millis) {
              try {
                  Thread.sleep(millis);
              } catch (InterruptedException e) {
                  e.printStackTrace();
              }
      }
  
       protected void clickStallElement(By locator) {
  
       try {
          waitForVisibilityOf(locator, 30);
          findElement(locator).click();
       }
       catch(org.openqa.selenium.StaleElementReferenceException ex)
       {
          waitForVisibilityOf(locator, 30);
          findElement(locator).click();
        }
      }
  
  
      /** Open page with given URL */
      protected void openUrl(String url) {
          driver.get(url);
      }
  
      /** Find element using given locator */
      protected WebElement findElement(By locator) {
          return driver.findElement(locator);
      }
  
      /** Find all elements using given locator */
      protected List&lt;WebElement&gt; findAllElements(By locator) {
          return driver.findElements(locator);
      }
  
      /** Click on element with given locator when its visible */
      protected void click(By locator) {
          waitForVisibilityOf(locator, 30);
          findElement(locator).click();
          log.info("Clicked WebElement");
      }
  
      ...
  
  }
  </code></pre>
  
  <p>5) BaseTest</p>
  
  <pre><code>public class SignInPageTest extends BaseTest {
  
  
      @Test(dataProvider = "SearchProvider", dataProviderClass = DBDataProvider.class)
      public void loginTest(String username, String password) {
  
          System.err.println("Running Test=&gt; " + this + " -&gt; on thread [" + Thread.currentThread().getId() + "]");
  
          log.info("Starting LogInTest #  for " + username);
  
          ClickAcadamyLandingPage home = new ClickAcadamyLandingPage(driver, log);
          home.openPage();
  
          home.closeNewsLetterPopup();
  
          // click login
          ClickAcadamyLoginPage loginPage = new ClickAcadamyLoginPage(driver, log);
          loginPage = home.clickLoginBtn();
  
          loginPage.enterEmailAddress(username);
  
          loginPage.enterPassword(password);
  
          ClickAcadamyDashboard dashboard = loginPage.clickLogin();
          dashboard.clickLogout();
  
      }
  
  
   }
  </code></pre>
  
  <p>6)testng.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"&gt;
  &lt;suite name="Suite" data-provider-thread-count = "2" thread-count="2" verbose="1" parallel="tests"&gt;
  
     &lt;listeners&gt;
       &lt;listener class-name="com.paralleltest.basetest.TestListener"/&gt;
     &lt;/listeners&gt;
  
     &lt;test name="loginTest"&gt;
      &lt;classes&gt;
        &lt;class name="com.paralleltest.SignInPageTest"/&gt;  
      &lt;/classes&gt;
     &lt;/test&gt; 
  
   &lt;/suite&gt; 
  </code></pre>