13

On PhantomJS 1.9.2, ubuntu 12 LTS and Ghostdirver 1.04 together with selenium 2.35 I get dangling phantomjs processes after my tests. Anyone knows a good way how to fix this?

Here is a test program that demonstrates the odd behavior:

package testing;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriverService;
import org.openqa.selenium.remote.DesiredCapabilities;

public class PhantomIsNotKilledDemo {

    private static WebDriver getDriver(){
        String browserPathStr = System.getProperty("selenium.pathToBrowser");
        if (browserPathStr == null) browserPathStr = "/home/user1/apps/phantomjs/bin/phantomjs";

        DesiredCapabilities caps = DesiredCapabilities.phantomjs();

        caps.setCapability("takesScreenshot", true);
        caps.setCapability(
                PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,
                browserPathStr );

        WebDriver driver = new PhantomJSDriver(caps);

        return driver;
    }

    public static void main(String[] args) {
        int max = 10;
        for (int i = 0; i < max; i++){
            WebDriver d1 = getDriver();
            d1.get("http://www.imdb.com/title/tt1951264");

            System.out.println("done with cycle " + (i+1) +" of "+max);
            d1.close();
            //d1.quit();
        }

        System.out.println("done");
        System.exit(0);
    }
}

To run this, you should supply the path of your phantomjs binary as system property or set the variable accordingly.

After letting this run I do this shell command

ps -ef | grep phantomjs

and find 10 dangling phantomjs processes.

If I use d1.quit() instead, I end up with no dangling process. This is clearly better, but still I would have expected to get the same result with .close.

Note, this is a crosspost of https://github.com/detro/ghostdriver/issues/162#issuecomment-25536311

Update This post is changed according to Richard's suggestion (see below).

4

2 回答 2

9

您应该使用quit()来终止进程而不是close().

正如您所发现的,close 将关闭当前窗口(和浏览器),但不会关闭进程。如果您要向进程发送其他命令或想要检查进程,这很有用。

退出适用于您想要关闭每个窗口并停止进程时,这听起来就像您正在寻找的那样。

这两种方法的文档如下:

关()

关闭当前窗口,如果它是当前打开的最后一个窗口,则退出浏览器。

退出()

退出此驱动程序,关闭所有关联的窗口。

于 2015-10-31T01:06:23.017 回答
3

我会将您的代码重写为以下内容:

public static void main(String[] args) {
    int max = 10;
    for (int i = 0; i < max; i++){
        WebDriver d1 = getDriver();

        d1.get("http://www.imdb.com/title/tt1951264");

        System.out.println("done with cycle " + (i+1) +" of "+max);
        d1.quit();
    }

    System.out.println("done");
    System.exit(0);
}

我不完全确定为什么.close()没有结束 WebDriver。理论上,.close()如果在最后一个打开的窗口中调用 WebDriver,应该退出它。当调用该 url 时,可能有什么东西正在打开第二个窗口?或者可能.close()对 phantomjs 有不同的工作方式。

至于为什么.quit()不关闭所有 phantomjs 会话,getDriver()您调用的最后一个在循环之外没有相应.quit()的外部。我重组了你的 for 循环来创建 WebDriver 的实例,执行你的测试,然后.quit()在每个循环结束时 WebDriver/phantomjs 的会话。

于 2013-10-02T20:32:46.953 回答