Tomasz Grobelny
Tomasz Grobelny

Reputation: 2746

How to kill chromedriver

After running my protractor tests I may be left with chromedriver.exe running. The simple question is: how do I kill it? There are several things to note here:

  1. I cannot just kill based on process name since several other chromedrivers may be running and may be needed by other tests.

  2. I already stop the selenium server using "curl http://localhost:4444/selenium-server/driver/?cmd=shutDownSeleniumServer"

  3. I noticed that the chromedriver is listening on port 33107 (is it possible to specify this port somehow?), but I do not know how should I call it to quit.

  4. Probably I should be using driver.quit() in my tests, but on some occasions it might not get called (eg. when the build is cancelled).

Any ideas how to kill the proper chromedriver process from command line (eg. using curl)?

Upvotes: 1

Views: 4862

Answers (1)

Cosmin
Cosmin

Reputation: 2394

The proper way to do it's as you mentioned by using driver.quit() in your tests. Actually, to be exact in your test cleanup method, since you want a fresh instance of the browser every time. Now, the problem with some Unit Test Frameworks (like MSTest for example) is that if your test initialize method fails, the test cleanup one will not be called. As a workaround for this you can surround in a try-catch statement you test initialize with catch calling and executing your test cleanup.

public void TestInitialize()
{
    try
    {
        //your test initialize statements
    }
    catch
    {
        TestCleanup();
        //throw exception or log the error message or whatever else you need
    }
}
public void TestCleanup()
{
    driver.Quit();
}

EDIT: For the case when the build is canceled, you can create a method that kills all open instances of Chrome browser and ChromeDriver that gets executed before you start a new suite of tests. E.g. if your Unit Testing Framework used has something similar to Class Initialize or Assembly Initialize you can do it there.

However, on a different post I found this approach:

PORT_NUMBER=1234 lsof -i tcp:${PORT_NUMBER} | awk 'NR!=1 {print $2}' | xargs kill Breakdown of command

(lsof -i tcp:${PORT_NUMBER}) -- list all processes that is listening on that tcp port (awk 'NR!=1 {print $2}') -- ignore first line, print second column of each line (xargs kill) -- pass on the results as an argument to kill. There may be several.

Here, to be more exact: How to find processes based on port and kill them all?

Upvotes: 2

Related Questions