0

I am trying to run multiple testsuites for Selenium-rc programmatically with Selenese. I analyzed the java code for the selenium-server-standalone-2.37.0.jar and determined that I had to use the main method of the GridLauncher class. And because I want to run multiple test suites, i put in a loop.

for (String[] arguments : argumentsList) {
    GridLauncher.main(arguments);
}

The first problem I encountered was that GridLauncher uses System.exit() to close itself. But that causes my process to stop also. The way that I solved this was to make the System.exit() throw an exception. This idea I got from this question in Stackoverflow.

private static class ExitTrappedException extends SecurityException {
    static void forbidSystemExitCall() {
        final SecurityManager securityManager = new SecurityManager() {
            @Override
            public void checkPermission( Permission permission ) {
                if( "exitVM".equals( permission.getName() ) ) {
                    throw new ExitTrappedException() ;
                }
            }
            @Override
            public void checkExit(int status) {
                throw new SecurityException();
            }
        } ;
        System.setSecurityManager( securityManager ) ;
    }
    static void enableSystemExitCall() {
        System.setSecurityManager( null ) ;
    }
}

for (String[] arguments : argumentsList) {
    ExitTrappedException.forbidSystemExitCall();
    try {
        GridLauncher.main(arguments);
    } catch (SecurityException se) {
        //catching System.exit()
    } finally {
        ExitTrappedException.enableSystemExitCall() ;
    }
}

The problem that I encounter now is that the stream doesn't close and i get an exception when the loop tries to perform the Selenium test: WARN - Failed to start: SocketListener1@0.0.0.0:4444 I tried to look for ways to close the stream but can't find anything. Is it possible to close it? If so, how?

Community
  • 1
  • 1

1 Answers1

0

You might want to try garbage-collecting the streams and have them finalized (as such classes tend to have a finalize() method which releases resources). You would basically System.gc() followed by System.runFinalization(). For details see this question: How to ensure finalize() is always called (Thinking in Java exercise)

You may also need to get rid of some threads before you do System.gc(). See question Get a List of all Threads currently running in Java for how to list threads. Another question discusses how to stop a thread: How do you kill a thread in Java?.

Community
  • 1
  • 1
halfbit
  • 3,414
  • 1
  • 20
  • 26
  • I looked in to your possible solution but I was not able to close the threads. They were created by the `selenium-server-standalone-2.37.0.jar` and they do not have proper methods to end their threads. Might I have overlooked something? – T. Huyzen Dec 02 '13 at 15:05
  • Have you tried interrupting the `GridLauncher` threads. I would expect them to be idle (i.e. in some wait state), so using `Thread.interrupt()` on them might work, see 2nd answer to http://stackoverflow.com/questions/671049/how-do-you-kill-a-thread-in-java. – halfbit Dec 03 '13 at 21:52