Search code examples
javaseleniumload-testingtestng-eclipse

How to Perform load Testing with Selenium TestNG at a time by opening multiple windows


I have executed the below code to perform load test

 @Test(invocationCount = 5, threadPoolSize = 1)
 public void GmailLogin() throws InterruptedException {

 System.setProperty("webdriver.chrome.driver", "D:\\CIPL0564\\D 
Drive\\Software\\chromedriver_win32\\chromedriver.exe");
 driver = new ChromeDriver();
 driver.manage().window().maximize();
driver.get("https://tst-oec-ebooking.azurewebsites.net/");
driver.manage().timeouts().implicitlyWait(6000, TimeUnit.SECONDS);
driver.findElement(By.xpath("//*[@id=\"Email\"]")).sendKeys("[email protected]");
driver.manage().timeouts().implicitlyWait(6000, TimeUnit.SECONDS);
driver.findElement(By.xpath("//*[@id=\"Password\"]")).sendKeys("Pass@123");
driver.findElement(By.xpath("//*[@id=\"login_submit\"]")).click();
Thread.sleep(1500);
driver.manage().timeouts().implicitlyWait(5000, TimeUnit.SECONDS);
driver.quit();

}

It executed 5 times with no errors but one after other.I need to execute this at a time by opening multiple windows. I have tried by giving threadPoolSize = 5,but i got error as session not created.


Solution

  • TestNG runner would create just one test class instance for all test methods run. This logic also occurs when running in parallel. It seems like your WebDriver object is defined globally, so in each invocation when you call "driver = new ChromeDriver()" it overrides the other.

    My advice for you is to use ThreadLocal object to define your WebDriver sessions. In that way, each WebDriver object in given thread acts as independent data set. See this link http://seleniumautomationhelper.blogspot.com/2014/02/initializing-webdriver-object-as-thread.html for more detailed explanation about this subject.