Two Browsers opened when running testng suite

2,476 views
Skip to first unread message

Mark Southward

unread,
May 31, 2012, 4:35:27 PM5/31/12
to testng-users
Hi,
I've tried to set up a simple scenario where my tests are in 2 classes
which extend a baseClass which invokes Firefox webdriver 2. This is in
anticipation of having lots more tests in other classes (albeit with a
little more code). This is run from within Eclipse.

A Firefox browser opens and before navigating to the "home page"
another browser opens. The tests seem to execute ok. I'm sure it will
be my code, I just can't see which bit(s), so if anyone can point it
out to me I would be very grateful!

Thanks for reading,

Mark

The testNG.xml file:-
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="none" >
<test name="Test" preserve-order="true">
<classes>
<class name="simple.FirstClass">
<methods>
<include name="step1"/>
<include name="step2"/>
</methods>
</class>
<class name="simple.SecondClass">
<methods>
<include name="step3"/>
<include name="step4"/>
</methods>
</class>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->


The base class:-
package simple;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;

public class BaseClass {
WebDriver driver = new FirefoxDriver();

@BeforeSuite
public void setup() throws Exception {
driver.get("http://www.google.com");
}

@AfterSuite
public void tearDown() throws Exception {
driver.quit();
}
}

The 1st test class:-
package simple;

import org.testng.annotations.Test;

public class FirstClass extends BaseClass {

@Test
public void step1() {
System.out.println("***In step1");
}

@Test
public void step2() {
System.out.println("***In step2");
}
}

The 2nd class:-
package simple;

import org.testng.annotations.Test;

public class SecondClass extends BaseClass {

@Test
public void step3() {
System.out.println("***In step3");
}

@Test
public void step4() {
System.out.println("***In step4");
}
}

The output:-
***In step1
***In step2
***In step3
***In step4

===============================================
Suite
Total tests run: 4, Failures: 0, Skips: 0
===============================================


Raj

unread,
May 31, 2012, 5:12:56 PM5/31/12
to testng...@googlegroups.com, testng-users
I guess you shud be implementing a singleton pattern to restrict single browser instance....

Thanks,
Raj
> --
> You received this message because you are subscribed to the Google Groups "testng-users" group.
> 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.
>

Tomek Kaczanowski

unread,
May 31, 2012, 5:54:36 PM5/31/12
to testng...@googlegroups.com
(a shot in the dark) make driver static perhaps?

--
Regards / Pozdrawiam
Tomek Kaczanowski
http://practicalunittesting.com


2012/5/31 Raj <vasika...@gmail.com>:

Krishnan Mahadevan

unread,
May 31, 2012, 9:16:52 PM5/31/12
to testng...@googlegroups.com
I dont think either the singleton nor the static approach is going to work. 
Am saying this because both of these approaches although would cause only 1 instance of webdriver it can have catastrophic results on the test itself because now both the classes would be working with the same webdriver which means they both try to perform different operations on the same browser. I think removal of @BeforeSuite and using @BeforeClass would be the right thing. 

The other way would be to move the instantiation part to a separate class, have the instantiation done in a beforeinvocation, save the newly created object in a ThreadLocal variable and then query this variable in the test method. 


--
Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/

Jeff

unread,
May 31, 2012, 9:25:05 PM5/31/12
to testng...@googlegroups.com

Seems to be working as it should....what is the behavior you want?  TestNG will instantiate your test classes (creating two browser instances...one for each test class) before it starts running tests. 

If you don't want the browsers started by simply instantiating the classes, move the driver initialization code into the @BeforeClass method.

Jeff

unread,
May 31, 2012, 9:42:22 PM5/31/12
to testng...@googlegroups.com

My phone is having sync issues...did my last response go through?

--

Mark Southward

unread,
Jun 1, 2012, 4:19:03 AM6/1/12
to testng-users
Thanks for the replies, found them thought provoking.

I've tried the static method and that did indeed only create one
instance of the browser.

I don't have a clear strategy (yet) for how I want this to work
because I (clearly) haven't got a grasp yet of "how it all fits
together". I'm new to Java, testNG, and selenium.

I hadn't realised that a browser will be created for each class (I was
following the blog example from the testNG website)- that could
potentially be a lot of browser instances!

I certainly don't want browsers, running functional tests, working
independantly, all pointing at my webservice at the same time.

At the moment, it seems like having the startup in @BeforeClass is the
way to go.

Jeff - yes, your response came through, thanks.

Thanks,

Mark

On Jun 1, 2:42 am, Jeff <predato...@gmail.com> wrote:
> My phone is having sync issues...did my last response go through?
> On May 31, 2012 7:17 PM, "Krishnan Mahadevan" <
>
>
>
>
>
>
>
> krishnan.mahadevan1...@gmail.com> wrote:
> > I dont think either the singleton nor the static approach is going to
> > work.
> > Am saying this because both of these approaches although would cause only
> > 1 instance of webdriver it can have catastrophic results on the test itself
> > because now both the classes would be working with the same webdriver which
> > means they both try to perform different operations on the same browser. I
> > think removal of @BeforeSuite and using @BeforeClass would be the right
> > thing.
>
> > The other way would be to move the instantiation part to a separate class,
> > have the instantiation done in a beforeinvocation, save the newly created
> > object in a ThreadLocal variable and then query this variable in the test
> > method.
>
> > On Friday, June 1, 2012, Tomek Kaczanowski wrote:
>
> >> (a shot in the dark) make driver static perhaps?
>
> >> --
> >> Regards / Pozdrawiam
> >> Tomek Kaczanowski
> >>http://practicalunittesting.com
>
> >> 2012/5/31 Raj <vasikarla....@gmail.com>:
> >> > I guess you shud be implementing a singleton pattern to restrict single
> >> browser instance....
>
> >> > Thanks,
> >> > Raj
>
> >> > On May 31, 2012, at 1:35 PM, Mark Southward <southym...@gmail.com>
> > My Scribblings @http://wakened-cognition.blogspot.com/

Sumit Terkhedkar

unread,
Sep 26, 2017, 11:55:30 AM9/26/17
to testng-users

Hi,

I am a bit late in this game, but it seems I am facing the same issue.
Is there anyone here found out the working solution?

I tried all of the mentioned solutions but none of them are working for me.. It is still opening a browser window per test class.

Any help appreciated.

Thanks for reading :)

Prawin Mattool Vijayan

unread,
Mar 16, 2019, 3:59:59 PM3/16/19
to testng-users
Try using priority for each test like the below given order for the two classes and it should fix the two browser issue.

The 1st test class:-
@Test(priority=1)
@Test(priority=2)

The 2nd class:-
@Test(priority=3)
@Test(priority=4)

suresh kumar Shetty

unread,
Jun 23, 2020, 10:39:02 AM6/23/20
to testng-users
Though i have given priority to @Test, still opens number of browsers equal to the number of classes called in testng.xml

⇜Krishnan Mahadevan⇝

unread,
Jun 23, 2020, 10:40:45 AM6/23/20
to testng...@googlegroups.com
Hard to say what is wrong without you sharing more additional information.

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
My Technical Scribblings @ https://rationaleemotions.com/


--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to testng-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/testng-users/f66ecb33-78aa-471a-a62c-0948480dd385o%40googlegroups.com.

Banh Duy

unread,
Jun 24, 2020, 3:25:50 AM6/24/20
to testng-users
The TestNG will scan all of your test classes then automatic convert all fields and methods to static, that's why two WebDriver driver be initialized.
You should only declare the webdriver and put the initialize webdriver statement into @BeforeClass and destroy it in @AfterClass.

Vào 03:35:27 UTC+7 Thứ Sáu, ngày 01 tháng 6 năm 2012, Mark Southward đã viết:

⇜Krishnan Mahadevan⇝

unread,
Jun 24, 2020, 3:31:55 AM6/24/20
to testng...@googlegroups.com
The TestNG will scan all of your test classes then automatic convert all fields and methods to static, that's why two WebDriver driver be initialized.

This is incorrect. TestNG does not fiddle around with the modifiers in a class and certainly TestNG doesnt do any of this sort.
The reason why two webdriver instances are being spun off here is because, the field that defines them at the class level is also get initialised.

When TestNG finds a test class, it uses reflection to instantiate it. Java by default will initialise the fields if they have been defined. in this case, the field is being defined and set to a new FirefoxDriver() object. So everytime a test class object gets created (2 in this case) the FirefoxDriver object also gets instantiated.



Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
My Technical Scribblings @ https://rationaleemotions.com/

--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to testng-users...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages