How To Run My Test Case in Parallel Multiple Threads With TestNG

1,778 views
Skip to first unread message

Gulshan Saini

unread,
Sep 15, 2011, 7:34:34 AM9/15/11
to testng...@googlegroups.com
Newbie here, so go gentle.  :)

I have a class which contains all of my test cases. Now I want to run the same class multiple times(Using TestNG Threads).
I am using xml to pass the parameters and start the execution.

I tried to use all following things
parallel=methods,
parallel=tests
parallel=classes

My first line in xml is:
<suite name="My suite" parallel="classes" thread-count="5">

I am not able to run multiple threads and execution happens only once.

Can anybody help me how to achieve this or provide me some sample.

Thanks
Gulshan

Cédric Beust ♔

unread,
Sep 15, 2011, 1:03:14 PM9/15/11
to testng...@googlegroups.com
Hi Gulshan,

I'm a bit confused: are you trying to run your tests in parallel or run the same class multiple times?

If the latter, I suggest using a @Factory.

-- 
Cédric




--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/testng-users/-/2p5ONzrwQ3kJ.
To post to this group, send email to testng...@googlegroups.com.
To unsubscribe from this group, send email to testng-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/testng-users?hl=en.

Gulshan Saini

unread,
Sep 15, 2011, 1:06:26 PM9/15/11
to testng-users
I am trying to run same class multiple times.

On Sep 15, 10:03 pm, Cédric Beust ♔ <ced...@beust.com> wrote:
> Hi Gulshan,
>
> I'm a bit confused: are you trying to run your tests in parallel or run the
> same class multiple times?
>
> If the latter, I suggest using a @Factory.
>
> --
> Cédric
>

Gulshan Saini

unread,
Sep 15, 2011, 1:08:59 PM9/15/11
to testng-users
ooh i missed your suggestion. how can i use @Factory to achieve this
any example will be really appreciated.

santosh h s

unread,
Sep 15, 2011, 1:29:22 PM9/15/11
to testng...@googlegroups.com
Can you Please provide me a code snippet as how can i use @Factory  
--

/Santosh

Cédric Beust ♔

unread,
Sep 15, 2011, 1:39:49 PM9/15/11
to testng...@googlegroups.com
Did you look it up in the documentation? Anything that's not clear?

-- 
Cédric

Gulshan Saini

unread,
Sep 15, 2011, 2:27:11 PM9/15/11
to testng-users
Ok - I tried the @Factory(Documentation #Section 5.7 - Factories)
annotation and here are the results.

1. Two browser sessions are created by WebDriver.
2. First one open the desired page and nothing happens.
3. Second one go through all the test cases.

Here is sample of my class
public class Wordpress {
static WebDriver driver;
static WebElement element;
Page page = new Page(driver);

private int m_numberOfTimes;

public Wordpress(int numberOfTimes) {
m_numberOfTimes = numberOfTimes;
}

@BeforeClass(alwaysRun = true)
public void setupBeforeSuite(ITestContext context) {

for (int i = 0; i < m_numberOfTimes; i++) {

String getUrl =
context.getCurrentXmlTest().getParameter("webdriver.url");
driver = new FirefoxDriver();
driver.get(getUrl);

}
}

@AfterClass(alwaysRun = true)
public void setupAfterSuite() {
driver.quit();
}

@Test(description="Maximize Browser Window")
public void MaximizeBrowser(){
//some code
}

@Test(description="Verfiy If We Are On Home Page", dependsOnMethods =
{ "MaximizeBrowser" })
public void homePage(){
//some code
}

@Test(description="Goto Login Page",dependsOnMethods =
{ "homePage" })
public void loginPage(){
//some code
}

@Test(description="Enter Valid Login Details", dependsOnMethods =
{ "loginPage" })
public void enterlogindetails(ITestContext context) {
//some code
}

======================== And This Is Another Class
================================================
public class WebTestFactory {
@Factory
public Object[] createInstances() {
Object[] result = new Object[2];
for (int i = 0; i < 2; i++) {
result[i] = new Wordpress(i * 2);
}
return result;
}
}
===============================================================================================




On Sep 15, 10:39 pm, Cédric Beust ♔ <ced...@beust.com> wrote:
> Did you look it up in the documentation? Anything that's not clear?
>
> --
> Cédric
>

Krishnan

unread,
Sep 16, 2011, 2:24:37 AM9/16/11
to testng...@googlegroups.com
You are working with Factories which is basically going to give you one or more test instances to work with.
However I am seeing that you have your WebDriver and WebElement data members as static, which means that they are common for all the instances.
So there are chances of the instances confusing the WebDriver and WebElement static data members and overwriting it.

You should consider making them instance variables so as to ensure that your tests are fully self sufficient 

Gulshan Saini

unread,
Sep 16, 2011, 3:08:15 AM9/16/11
to testng...@googlegroups.com
Thanks Krishnan - I am understanding what you are suggesting but I am confused in how to implement this i.e. where to make this change.

Reply all
Reply to author
Forward
0 new messages