3

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();
 }
}
asked Jun 6, 2014 at 14:47
1

1 Answer 1

2

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:

  1. Implementing @DataProvider annotated method that will load test data from file (see sample implementation here)
  2. Telling @DataProvider method to run in parallel as well

    @DataProvider(name = "loadData", parallel = true)
    
  3. Telling the test to use the defined data provider but omitting threadPoolSize parameter.

    @Test(dataProvider = "loadData")
    
  4. 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" >
    
answered Jun 6, 2014 at 21:17
3
  • 1
    There may be some value in running some of those tests in a different process in case the test crashes. Commented Jun 7, 2014 at 16:59
  • @user246, that would require parallelization on a lavel of CI server (e.g., Jenkins), right? Commented Jun 7, 2014 at 18:26
  • 1
    That would be one way. Another would be to build a fork/exec capability into the test runner, with results delivered over a socket or something. I'll bet someone's already done that with JUnit/TestNG. Commented Jun 7, 2014 at 18:50

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.