How to run multiple browser instance with Webdriver, Java, and Junit/TestNG?



  • I have a test ready to be executed but it takes a long time to finish. In this test I'm feeding in csv data, so basically the whole test will run 56 times. I was wondering if there's anyway I could use multiple browser instance and divide the workload to four instance. It will save me some time. I tried to use TestNG's ThreadPoolSize but it's not doing what I want it to. It's using the same data for four instances of firefox. I want each browser to have it's own unique data. Please check my code and let me know what I'm missing. I really appriciate every one's help. public class StudentPageTest { WebDriver driver; DesiredCapabilities capability; WebElement element; WebDriverWait wait; private String baseURL; @BeforeTest public void setUp() throws MalformedURLException{ //capability = DesiredCapabilities.firefox(); //driver = new FirefoxDriver(); //wait = new WebDriverWait(driver, 120); //driver.manage().deleteAllCookies(); baseURL = "http://somewebsite.com"; } @SuppressWarnings("resource") @Test(threadPoolSize = 4) public void StudentPortalTest() throws InterruptedException, IOException{ driver = new FirefoxDriver(); wait = new WebDriverWait(driver, 120); driver.manage().deleteAllCookies(); String studentId = "studentID.csv"; BufferedReader br = null; String line = ""; String cvsSplitBy = ","; br = new BufferedReader(new FileReader(studentId)); while ((line = br.readLine()) != null) { String[] student_id = line.split(cvsSplitBy); //Logging in Student Portal---------------------------------------------------------------------------------------------------------------| for (int i = 0; i < student_id.length; i++) { driver.get(baseURL+student_id[i]); driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); driver.findElement(By.cssSelector(".logo>img")).isDisplayed(); driver.findElement(By.cssSelector("#UserName")).sendKeys("SecretUserName"); n driver.findElement(By.cssSelector("#Password")).sendKeys("EvenMoreSecretPassword"); driver.findElement(By.cssSelector(".submitBtn")).click(); driver.manage().timeouts().implicitlyWait(120, TimeUnit.SECONDS); Thread.sleep(4000); ...............and the test goes on below................... } @AfterTest public void tearDown(){ driver.quit(); } }



  • Your have four threads, each running test in a separate browser instance, but reading whole file again and again. This is because each thread knows nothing about what test data other threads used. A solution to the problem is to move loading and distributing test data among threads outside the test so that something else passes test data D1 to thread T1, D2 to T2, etc. In TestNG this boils down to just a few steps: Implementing @DataProvider annotated method that will load test data from file (see sample implementation here) Telling @DataProvider method to run in parallel as well @DataProvider(name = "loadData", parallel = true) Telling the test to use the defined data provider but omitting threadPoolSize parameter. @Test(dataProvider = "loadData") Configuring how many data provider instances in parallel must be run: Parallel data providers running from an XML file share the same pool of threads, which has a size of 10 by default. You can modify this value in the tag of your XML file: <suite name="Suite1" data-provider-thread-count="20" >



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2