RE: How to run the tests in a class into 2 groups in parallel sessions in testng

Suppose a class has 200 methods and we want to run 100 in session next 100 in another sessions. How can we proceed this using testng.

anejamohit_18 Default Asked on January 22, 2017 in Selenium WebDriver.
Add Comment
1 Answers

First install Java JRE and JDK.
In Advanced system settings/ Environment Variables setup java path:
Create a new system variable (if it is not created):

Name: JAVA_HOME
Value(the location of the jdk): C:\Program Files\Java\jdk1.7.0_25

In to the Path variable add:
The location of jre: c:\Program Files (x86)\Java\jre7\bin\;

Install selenium and testing to Eclipse.
Download (from code.google.com) the selenium-server-standalone-2.33.0.jar file (or the newest).
Copy this file to a simple place, for example C:\

Now open command prompt (with administrator privileges).
Go to c:\ and enter the following line to start Selenium Grid:
java -jar selenium-server-standalone-2.33.0.jar -role hub -port 4444

Open a new command prompt window, go to c:\ and enter the following line to register the browser to the Selenium Grid:
java -jar selenium-server-standalone-2.33.0.jar -role node -hub http://localhost:4444/grid/register -browser browserName=firefox,maxInstances=3

The maxInstance will determine the max number of the browsers. If it is set to 3 than maximum 3 browsers(tests) are able to run parallel.
(If you want to know more about Selenium Grid visit the Selenium site: http://docs.seleniumhq.org/docs/07_selenium_grid.jsp )

Now in Eclipse create a new project, a new package and a new class. Name it TestBase.
To this file create:
• a new RemoteWebDriver,
• to the @BeforeMethod the setUp() function
• to the @AfterMethod the tearDown() function
• getDriver() function

Here is the full code:

package testNG.Parallel;
import java.net.MalformedURLException;
import java.net.URL;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;

public class TestBase {

protected ThreadLocal<RemoteWebDriver> threadDriver = null;

@BeforeMethod
public void setUp() throws MalformedURLException {

threadDriver = new ThreadLocal<RemoteWebDriver>();
DesiredCapabilities dc = new DesiredCapabilities();
FirefoxProfile fp = new FirefoxProfile();
dc.setCapability(FirefoxDriver.PROFILE, fp);
dc.setBrowserName(DesiredCapabilities.firefox().getBrowserName());
threadDriver.set(new RemoteWebDriver(new URL(“http://localhost:4444/wd/hub”), dc));
}

public WebDriver getDriver() {
return threadDriver.get();
}

@AfterMethod
public void closeBrowser() {
getDriver().quit();

}
}
The URL is set to localhost:4444, to the same location and port as the Selenium Grid. Now create for example 5 java class file. These files would contain the tests.
For example here are my sample test files:

package testNG.Parallel;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.testng.annotations.Test;

public class Test01 extends TestBase{

@Test
public void testLink()throws Exception{
getDriver().get(“http://facebook.com”);
WebElement textBox = getDriver().findElement(By.xpath(“//input[@value=’Your Email’]”));
textBox.click();
textBox.sendKeys(“Just a test!”);
Thread.sleep(2000);
}
}

package testNG.Parallel;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.testng.annotations.Test;

public class Test02 extends TestBase{

@Test
public void testLink()throws Exception{
getDriver().get(“https://twitter.com”);
WebElement textBox = getDriver().findElement(By.xpath(“//label[text()=’Full name’]”));
textBox.click();
textBox.sendKeys(“Just another test!”);
Thread.sleep(2000);
}

}

package testNG.Parallel;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.testng.annotations.Test;

public class Test03 extends TestBase{

@Test
public void testLink()throws Exception{
getDriver().get(“http://facebook.com”);
WebElement textBox = getDriver().findElement(By.xpath(“//input[@value=’Re-enter Email’]”));
textBox.click();
textBox.sendKeys(“Test three!”);
Thread.sleep(2000);
}
}

package testNG.Parallel;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.testng.annotations.Test;

public class Test04 extends TestBase{

@Test
public void testLink()throws Exception{
getDriver().get(“https://twitter.com”);
WebElement textBox = getDriver().findElement(By.xpath(“//label[text()=’Full name’]”));
textBox.click();
textBox.sendKeys(“Test 4!”);
Thread.sleep(2000);
}
}

package testNG.Parallel;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.testng.annotations.Test;

public class Test05 extends TestBase{

@Test
public void testLink()throws Exception{
getDriver().get(“http://facebook.com”);
WebElement textBox = getDriver().findElement(By.xpath(“//input[@value=’Re-enter Email’]”));
textBox.click();
textBox.sendKeys(“Test 5!”);
Thread.sleep(2000);
}
}
Important things:
Every test file need to be extended with TestBase.
In the test files doesn’t need the @BeforeMethod or @AfterMethod. These methods will be executed from TestBase.
From the Tests the webdriver will be reached with getDriver() function which is created in TestBase.

Now we need to create two (for the better visibility) xml file in Eclipse to the project root.
One of the files must be named to testng.xml.
Here is the content of the testng.xml file:

<suite name=”My Test Suite”>
<suite-files>
<suite-file path=”./testFiles.xml” />
</suite-files>
</suite>
Write the following lines to the other xml file (the paths of the test files):

<!DOCTYPE suite SYSTEM “http://testng.org/testng-1.0.dtd”>
<suite name=”Parallel test runs” parallel=”tests” thread-count=”2″>

<test name=”T_01″>
<classes>
<class name=”testNG.Parallel.Test01″ ></class>
</classes>
</test>

<test name=”T_02″>
<classes>
<class name=”testNG.Parallel.Test02″ ></class>
</classes>
</test>

<test name=”T_03″>
<classes>
<class name=”testNG.Parallel.Test03″ ></class>
</classes>
</test>

<test name=”T_04″>
<classes>
<class name=”testNG.Parallel.Test04″ ></class>
</classes>
</test>

<test name=”T_05″>
<classes>
<class name=”testNG.Parallel.Test05″ ></class>
</classes>
</test>

</suite>
The test file locations are added by the following logic:
Package name (testNG.Parallel) dot fileName (Test01).

In this file the second line is very interesting. There is the “parallel” parameter. Set to “tests” because we want to run tests parallel. The other parameter is the “thread-count”. If it is set to 2, than two browsers will be opened and the first two tests will run from the list. If the thread-count is 5 than five browsers will be opened and all the five tests will executed parallel!

To run the tests just right click to testng.xml , select Run As-> TestNG Suite.
That’s it!
Have fun!

I’m a selenium Tester and I have a web page which describes about the selenium Testing. If your audience is also interested in Selenium Testing, they can take a look here: Selenium Testing

 
Default Answered on January 23, 2017.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.