I started execution of a testng.xml through apache commons executor libarary by following code:
DefaultExecuteResultHandler resultHandler;
ExecuteWatchdog watchdog;
final Executor executor;
resultHandler = new DefaultExecuteResultHandler();
watchdog = new ExecuteWatchdog(-1L);
executor = new DefaultExecutor();
executor.setStreamHandler(new PumpStreamHandler(new LogOutputStream() {
@Override
protected void processLine(final String line, @SuppressWarnings("unused") int level) {
Display.getDefault().syncExec(new Runnable() {
public void run() {
if (line.toLowerCase().indexOf("error") > -1) {
textArea.append(line+"\n");
} else if (line.toLowerCase().indexOf("warn") > -1) {
textArea.append(line+"\n");
} else {
textArea.append(line+"\n");
}
}
});
}
}));
executor.setExitValue(1);
executor.setWatchdog(watchdog);
executor.execute(cl, resultHandler);
But I want to give a stop button to stop this process. I tried :
executor.getWatchdog().destroyProcess();
But this destroys only the watchdog . However the testng.xml I started keeps on running in the background. I want to destroy the process that I gave in the command . Is this possible?
The reason behind why your solution is not killing the JVM that was spawned is perhaps because you are invoking
cmd.exe
and from within that is where you are probably spawning the JVM. So when you invokedestroyProcess()
I believe itscmd.exe
that is getting killed but notjava.exe
.You should try changing your command line to something like below:
Here's a solution that does not make use of Apache commons executor for doing this, but manages everything within a single JVM and also provides for a way to retrieve the output and error output from TestNG.
This solution makes use of the TestNG APIs.
The main test runner, that uses TestNG to run tests, in a different thread looks like below
The utility class that re-directs all output and error contents to a thread, so that they can be redirected to anywhere, looks like below :
This class is mostly borrowed code from the solution Redirect console output to string in java with some improvisations.
The test class used looks like below
The output looks like below