Way to put driver.quit() in @AfterTest

403 views
Skip to first unread message

Pandaman

unread,
Aug 31, 2011, 6:19:41 PM8/31/11
to webdriver
Hi all,

I am trying to put the driver.quit() in @AfterTest so that I don't
have to write it at the end of each test. However, I do not want to
declare driver as a global variable with in the class since doing that
will make it not thread-safe when running tests in parallel.

in selenium grid 1.0, I can do session().close(); or
closeSeleniumSession(); because session() is a dedicated static
variable within the thread. However, for webdriver, there isn't
something quite like that.

Are there some recommended ways to do it?
Thanks.

Krishnan Mahadevan

unread,
Aug 31, 2011, 10:18:31 PM8/31/11
to webd...@googlegroups.com
@AfterTest is going to be invoked only after all of your tests have run to completion. So it gets invoked only once.

If all you want is to clean up the driver objects before your tests exit, you might very well put driver.quit() in your @AfterTest.


Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"




--
You received this message because you are subscribed to the Google Groups "webdriver" group.
To post to this group, send email to webd...@googlegroups.com.
To unsubscribe from this group, send email to webdriver+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/webdriver?hl=en.


Pandaman

unread,
Sep 1, 2011, 2:22:34 PM9/1/11
to webdriver
But my @AfterTest is in my parent class. My actual test is in the
child class. Is there a way to add the quit() in the parent class?

On Aug 31, 7:18 pm, Krishnan Mahadevan
<krishnan.mahadevan1...@gmail.com> wrote:
> @AfterTest is going to be invoked only after all of your tests have run to
> completion. So it gets invoked only once.
>
> If all you want is to clean up the driver objects before your tests exit,
> you might very well put driver.quit() in your @AfterTest.
>
> Thanks & Regards
> Krishnan Mahadevan
>
> "All the desirable things in life are either illegal, expensive, fattening
> or in love with someone else!"
>

Danny Guerrier

unread,
Sep 1, 2011, 7:35:18 PM9/1/11
to webd...@googlegroups.com
Make the driver a protected variable in the parent class and you child
class will have access to it.

Krishnan Mahadevan

unread,
Sep 1, 2011, 10:34:13 PM9/1/11
to webd...@googlegroups.com
If you have access to your parent class then you can very well introduce the @AfterTest and include this there.
If you don't have access to it, you can have a @AfterTest method in your class and AFAIK the @AfterTest in your class should get invoked too.


Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"



Pandaman

unread,
Sep 2, 2011, 12:37:46 PM9/2/11
to webdriver
Thanks for the reply. However, I still think that the problem is not
descriptive enough for everyone to understand, so I am giving an
example:

imagine my parent class is parentTest:

class parentTest() extends RemoteWebDriver{
@BeforeTest
public void beforeTest(){
......
URL url;
Capabilities capabilities;

RemoteWebDriver driver = new RemoteWebDriver(url, capabilities);
......
}

@AfterTest
public void afterTest(){

[I want to call driver.quit(); here but how to pass the one I
instantiated from @BeforeTest to @AfterTest?]
}
}

and my child class is childTest:

class childTest() extends parentTest {

@Test
public void realTest(){
[I want to use the driver defined in the @BeforeTest here, but how do
I pass it in? driver is not a static variable and it should be not
because I am running testng parallel executino to the test]
}
}

hope this describes the problem well enough.
Thanks everyone for offering help :)

On Sep 1, 7:34 pm, Krishnan Mahadevan
<krishnan.mahadevan1...@gmail.com> wrote:
> If you have access to your parent class then you can very well introduce the
> @AfterTest and include this there.
> If you don't have access to it, you can have a @AfterTest method in your
> class and AFAIK the @AfterTest in your class should get invoked too.
>
> Thanks & Regards
> Krishnan Mahadevan
>
> "All the desirable things in life are either illegal, expensive, fattening
> or in love with someone else!"
>
>
>
>
>
>
>
> On Fri, Sep 2, 2011 at 5:05 AM, Danny Guerrier <dguerr...@gmail.com> wrote:
> > Make the driver a protected variable in the parent class and you child
> > class will have access to it.
>

shi junjuan

unread,
Sep 4, 2011, 10:53:41 PM9/4/11
to webd...@googlegroups.com
The way I resolve this problem is to define a class(like WebDriverManager) which has a property to contain all the WebDriver instances:
public static ThreadLocal<WebDriver> driverSession = new ThreadLocal<WebDriver>();

when you create the WebDriver instance, call driverSession.set(driver). And use WebDriverManager.driverSession.get() to get the WebDriver instance.
Reply all
Reply to author
Forward
0 new messages