Search code examples
seleniumtestingtestngappium

Closing all open command prompt leads to error in forked process in testNG


I am using testNG for mobile automation and I want to close multiple command prompts(appium servers) which i launched. For this, I am using the below code

@AfterSuite
    public void closeCommandPrompts() throws IOException, InterruptedException{
    System.out.println("After suite");
    Thread.sleep(8000);
    Runtime.getRuntime().exec("taskkill /F /IM node.exe");
    System.out.println("closed node.exe");
    Runtime.getRuntime().exec("taskkill /F /IM cmd.exe");

The last line if I commnet it out works fine, however if they are not commented it gives an error in the forked process.

I guess testNG is internally using command prompt which i am trying to close when iam using taskkilll in @aftersuite.

Please help me in getting some work around.


Solution

  • You can use following code :

         CommandLine command = new CommandLine("cmd");
          command.addArgument("/c");
          command.addArgument("taskkill");
          command.addArgument("/F");
          command.addArgument("/IM");
          command.addArgument("node.exe");
    
          DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
          DefaultExecutor executor = new DefaultExecutor();
          executor.setExitValue(1);
          try {
                executor.execute(command, resultHandler);
                System.out.println("Stopped appium node ! ");
          } catch (IOException e) {
                System.out.println("FAIL => Unable to stop appium server "+e.getMessage());
                e.printStackTrace();
          }