Scenario:–
How we can achieve running of parallel set of tests in TestNG.
How we can achieve running of parallel set of tests in TestNG.
![]() |
Parallel Run |
Solution:-
We are already done with cross browser testing using TestNG, we would utilize the same example here to design our solution.
Test Scenario:-
We want parallel instances of Test Case one in Firefox and another in Chrome, but with different set of data.
Example:-
Firefox with ‘username1’ and ‘password1’
Chrome with ‘username2’ and ‘password2
Approach:-
We would use the @parameter annotation to create different set of data.
Also we would add,’Parallel’ attribute in the Suite tag of XML file, this is being used to run tests parallel in different browsers.
And ‘thread-count’ attribute is to used to pass the number of maximum threads to be created.
XML File:-
<?xml version="1.0" encoding="UTF-8"?>
<suite name="Suite" parallel="tests" thread-count="2">
<test name="Run in Firefox">
<parameter name="browser" value="firefox"></parameter>
<parameter name="userName" value="Test1"></parameter>
<parameter name="Password" value="Pwd1"></parameter>
<classes>
<class name="srcTest.ParallelTesting"/>
</classes>
</test>
<test name="Run in chrome">
<parameter name="browser" value="chrome"></parameter>
<parameter name="userName" value="Test2"></parameter>
<parameter name="Password" value="Pwd2"></parameter>
<classes>
<class name="srcTest.ParallelTesting"/>
</classes>
</test>
</suite>
Java Code:-
package srcTest;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class ParallelTesting {
private WebDriver driver;
@Test
@Parameters({"userName","Password"})
public void login(String userName,String Password)
{
//Adding Implicit wait
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
//Maximize browser
driver.manage().window().maximize();
//Open the Login Application
driver.get("http://www.ufthelp.com/p/testpage.html");
//Fill the UserName and Password fields
driver.findElement(By.id("userName")).sendKeys(userName);
driver.findElement(By.id("password")).sendKeys(Password);
//Click the Sign in Button
driver.findElement(By.id("SignIn")).click();
//Clicking on the alert message
driver.switchTo().alert().accept();
}
@BeforeClass
@Parameters({"browser"})
public void BeforeClass(String browser) throws Exception {
//Incase it is firefox browser
if(browser.equals("firefox"))
{
driver = new FirefoxDriver();
}
//Incase browser is chrome
else if(browser.equals("chrome"))
{
System.setProperty("webdriver.chrome.driver", "D:\Automation\Selenium\MyCode\chromedriver_win32\chromedriver.exe");
driver = new ChromeDriver();
}
//Exit the Test without execution
else
{
//Throw an exception for no matching browsers
throw new Exception("Invalid Browser"+browser);
}
}
@AfterClass
public void afterClass() {
//Destroying the object
driver.quit();
}
}
One reply on “Parallel execution of Tests in TestNG”
This is not working Parallel, Its Failing on Chrome Browser. Username & Password values are entered in FF only, Chrome browser, not able to enter Username & Password, so following error displayed.
org.openqa.selenium.NoAlertPresentException: no alert open