简体   繁体   中英

Is it possible to close a stream when performing Selenium-rc programmatically?

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?

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? .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM