关闭所有打开的命令提示符会导致 testNG 中的分叉进程出错

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

我正在使用 testNG 进行移动自动化,我想关闭我启动的多个命令提示符(appium 服务器)。为此,我使用以下代码

@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");

最后一行如果我commnet它工作正常,但是如果他们没有评论它会在分叉过程中给出错误。

我猜 testNG 在内部使用命令提示符,当我在 @aftersuite 中使用 taskkilll 时,我试图关闭它。

请帮助我解决问题。

您可以使用以下代码:

     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();
      }