Parallel execution of Tests in TestNG – User friendly Tech help

Scenario:
nHow we can achieve running of parallel set of tests in TestNG.n

n

n

n

n

n

n

n

Parallel Run

n

Solution:-
nWe are already done with cross browser testing using TestNG, we would utilize the same example here to design our solution.
n
nTest Scenario:-
nWe want parallel instances of Test Case one in Firefox and another in Chrome, but with different set of data.
nExample:-
nFirefox with ‘username1’ and ‘password1’
nChrome with ‘username2’ and ‘password2
n
nApproach:-
nWe would use the @parameter annotation to create different set  of data.
nAlso we would add,’Parallel’ attribute in the Suite tag of XML file, this  is being used to run tests parallel in different browsers.
nAnd ‘thread-count’ attribute is to used to pass the number of maximum threads to be created.
n
nXML File:-

n




















n

Java Code:-

n

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();
}
}

n

More Testing Scenarios

n

@PhotoCourtsey

Was this article helpful?
YesNo

Similar Posts