【问题标题】:java selenium leaving zombie processesjava selenium 离开僵尸进程
【发布时间】:2022-01-16 14:07:43
【问题描述】:

我正在设置一个测试套件来访问一些内部(最终是外部 [生产] 以替换 pingdom),但是现在当我在 eclipse 中运行它时,所有其他“作为 Java 应用程序运行”的调用都会挂起,留下一些僵尸进程,并且什么也不输出(我猜从挂起部分很明显)。但是如果我杀死它(仍然在 Eclipse 中),然后重新启动,它会立即输出页面并且不会留下僵尸进程。所以为了形象化,它是这样工作的:

  1. 以 java 应用程序运行,并打印出我期望的结果
  2. 在 java 应用程序挂起时运行并且几乎什么都不做(除了产生僵尸进程)
  3. 以 java 应用程序运行,并打印出我期望的结果
  4. 在 java 应用程序挂起时运行并且几乎什么都不做(除了产生僵尸进程)
  5. ...
Jerry$ ps -ef | grep -i headless | wc
      16     511   15011
Jerry$ ps -ef | grep -i headless | wc
      21     678   19990
Jerry$ ps -ef | grep -i headless | wc
      21     678   19990
Jerry$ ps -ef | grep -i headless | wc
      26     845   24977

我已经尝试了几次,所以您可以看到它从 16 开始,经过几次运行后,现在是 26 个仍在后台运行的僵尸(孤立?)进程。

现在,代码:

public class SeleniumTest {

    public static void main(String[] args) {
        // Globally set the driver location
        System.setProperty("webdriver.chrome.driver", "/usr/local/bin/chromedriver");

        // Locally pass in the options
        ChromeOptions chromeOptions = new ChromeOptions();
        chromeOptions.addArguments("--headless");
        chromeOptions.addArguments("--no-sandbox");

        // Create the driver, download, and test
        WebDriver driver = new ChromeDriver(chromeOptions);

        driver.get("http://192.168.2.15:8080/testApp/m/getBeans2");
        
        System.out.println(driver.getPageSource());
        System.out.println(driver.getCurrentUrl());

        // This block was introduced around the 21 mark as I was testing if I had to 
        // quit first between subsequent calls, so calling it twice didn't lead to the
        // initial 16 zombie processes, just the call to the QA server above
        System.out.println("--------------------   LOCALHOST   --------------------");
        driver.get("http://localhost:8080/testApp/m/getBeans2");

        System.out.println(driver.getPageSource());
        System.out.println(driver.getCurrentUrl());

        driver.quit();
    }
}

现在我正在使用 Open JDK 11、Chrome 驱动程序 95.0.4638.69 和这个版本的 selenium:

    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>4.1.0</version>
    </dependency>

那么...为什么我会得到 Zombies/孤立进程 - 这是我必须在运行之间清理的东西吗?

【问题讨论】:

  • 本题目前多题合一。它应该只关注一个问题。
  • @Prophet 重点将是主要问题,即标题;如果您认为这会让其他人回答,我可以删除后续问题,但没有人尝试过

标签: java selenium selenium-webdriver


【解决方案1】:

确切的原因仍然未知(在上面的问题中,我说所有其他调用都失败了,那部分仍然未知),但是测试有点太简单了(它注定会变得更加复杂和有用,如果我继续构建测试类的最终版本,然后进行测试,而不是测试/检查每一步,我什至没有注意到),这意味着,如果 Selenium 抛出异常,driver.quit() 没有被调用,所以进程继续存在。

所以将这些行添加到上面的代码中,“修复”它,命名,设置一些超时并捕获异常

    driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(5));

    try {
        driver.get(url);
    } catch(Throwable t) {
        // do something useful here
        System.err.println("Error with the driver");
        t.printStackTrace();
    } finally {
        driver.quit();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-10-19
    • 1970-01-01
    • 2019-09-01
    • 1970-01-01
    • 2014-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多