1

How do you manage resource clean up in JUnit4 (in a JUnitRunner / in a TestCase) when you interrupt the test in debug mode ?

I use Selenium WebDriver by implementing JUnit tests. When I run the test in debug, if I interrupt the test by clicking on the stop button, then my navigator is still open. I would like to close this navigator by calling a method. But I don't know where calling this method. In fact, I've overrided BlockJUnit4ClassRunner to have my own Runner and I also have implemented a WebDriverTestCase. So I can do a lot of things.

I tried to implement my own SecurityManager and override the method checkExit(int status) but, when I interrupt my test in debug mode, the SecurityManager is not called (with a System.exit(), it works correctly).

  • do you know the jvm is exiting? If it is, you can add a shutdown hook to run custom code when the jvm exits – sbridges Apr 23 '11 at 01:49
  • 1
    It's possible that when you click on the stop button, the JVM running the tests is killed. Depending on the signal(s) send to the process, it might be possible to install a signal handler to close the navigator – NamshubWriter Apr 28 '11 at 02:47

1 Answers1

2

As far as I know, there is no way to execute anything, when you stop a debug session.

The only solution to this kind of problem is: Do the tear down in the set up!

This means, you should check in your @Before method, bevor you create a new WebDriver, if an old browser process is still running, and if you find one, kill it. (See How to get a list of current open windows/process with Java? and Killing a process using Java)

Furthermore you probably also want to delete the webdriver directories in your temp directory.

Community
  • 1
  • 1
Michael Tamm
  • 883
  • 8
  • 13