seleniumselenium-webdriverwebdriverselenium-chromedriver

how to kill drivers after running the automation scripts


I am trying to run the automation scripts using java-Selenium TestNG. For opening browser I have invoked chrome driver. even after stopping the scripts the chrome driver is running in Background process in task manager. For ex If I execute my script for 20 time then the chrome driver is running for 20 time in the task manager. I didnt face any issue because of this problem. but still wanted to know should I add anything in my script to stop those drivers? . Will it cause any issue in the future ?


Solution

  • You can kill unclosed process writing in @AfterSuite (in Java)

    If You launch test on Windows:

    //kill chromedriver
    Runtime.getRuntime().exec("taskkill /im chromedriver.exe /f");
    

    If You launch test on Linux:

    String[] cmd = new String[]{"/bin/sh", "killchrome.sh"};
    Process pr = Runtime.getRuntime().exec(cmd);
    

    and in killchrome.sh (in this example file is in main project directory) you should write

    kill $(ps aux | grep 'chromedrive[r]' | awk '{print $2}')