使用硒和Firefox运行gitlab CI,获取WebDriverRorr:流程意外关闭,状态1

发布于 2025-02-08 19:05:41 字数 2676 浏览 2 评论 0原文

我需要设置一个简单的CI配置来运行Web应用程序项目的测试。 ,我的测试文件看起来像这样:

import { promises as fsp } from "fs";
import { equal } from "assert";
import {
  Builder,
  Capabilities,
  // eslint-disable-next-line node/no-unpublished-import
} from "selenium-webdriver";

/* global describe, it, after */

async function takeScreenshot(driver, file) {
  const image = await driver.takeScreenshot();
  await fsp.writeFile(file, image, "base64");
}

describe("Selenium test", () => {
  const driver = new Builder().withCapabilities(Capabilities.firefox()).build();

  it("should go to google.com and check title", async () => {
    await driver.get("https://www.google.com");
    await takeScreenshot(driver, "test.png");
    const title = await driver.getTitle();
    equal(title, "Google");
  });

  after(() => driver.quit());
});

我正在使用以下CI配置

image: ubuntu

test:
  script:
    - apt update
    - apt install -y firefox npm xvfb
    - npm install
    - npm run lint
    - xvfb-run npm run test

当前 问题。

所有这些都会产生以下错误:

$ xvfb-run npm run test
> [email protected] test
> node ./node_modules/mocha/bin/mocha --exit --timeout 10000
  Selenium test
    1) should go to google.com and check title
    2) "after all" hook for "should go to google.com and check title"
  0 passing (303ms)
  2 failing
  1) Selenium test
       should go to google.com and check title:
     WebDriverError: Process unexpectedly closed with status 1
      at Object.throwDecodedError (node_modules/selenium-webdriver/lib/error.js:522:15)
      at parseHttpResponse (node_modules/selenium-webdriver/lib/http.js:549:13)
      at Executor.execute (node_modules/selenium-webdriver/lib/http.js:475:28)
      at processTicksAndRejections (internal/process/task_queues.js:97:5)
  2) Selenium test
       "after all" hook for "should go to google.com and check title":
     WebDriverError: Process unexpectedly closed with status 1
      at Object.throwDecodedError (node_modules/selenium-webdriver/lib/error.js:522:15)
      at parseHttpResponse (node_modules/selenium-webdriver/lib/http.js:549:13)
      at Executor.execute (node_modules/selenium-webdriver/lib/http.js:475:28)
      at processTicksAndRejections (internal/process/task_queues.js:97:5)
Cleaning up project directory and file based variables
ERROR: Job failed: exit code 1

我不知道我该怎么办来解决这个问题。我没有附上当前的配置,实际上我认为使用Ubuntu映像可能是浪费,但其他事情也对我有用。如果某人具有完全不同的配置,该配置将与随附的测试文件一起使用,那么我很乐意采用它。

I need to setup a simple CI configuration to run tests of a web app project. Currently, my test file looks like this:

import { promises as fsp } from "fs";
import { equal } from "assert";
import {
  Builder,
  Capabilities,
  // eslint-disable-next-line node/no-unpublished-import
} from "selenium-webdriver";

/* global describe, it, after */

async function takeScreenshot(driver, file) {
  const image = await driver.takeScreenshot();
  await fsp.writeFile(file, image, "base64");
}

describe("Selenium test", () => {
  const driver = new Builder().withCapabilities(Capabilities.firefox()).build();

  it("should go to google.com and check title", async () => {
    await driver.get("https://www.google.com");
    await takeScreenshot(driver, "test.png");
    const title = await driver.getTitle();
    equal(title, "Google");
  });

  after(() => driver.quit());
});

and I am using the following CI configuration

image: ubuntu

test:
  script:
    - apt update
    - apt install -y firefox npm xvfb
    - npm install
    - npm run lint
    - xvfb-run npm run test

where npm run lint simply executes npx eslint **/*.mjs and does not cause a problem.

All this produces the following error:

$ xvfb-run npm run test
> [email protected] test
> node ./node_modules/mocha/bin/mocha --exit --timeout 10000
  Selenium test
    1) should go to google.com and check title
    2) "after all" hook for "should go to google.com and check title"
  0 passing (303ms)
  2 failing
  1) Selenium test
       should go to google.com and check title:
     WebDriverError: Process unexpectedly closed with status 1
      at Object.throwDecodedError (node_modules/selenium-webdriver/lib/error.js:522:15)
      at parseHttpResponse (node_modules/selenium-webdriver/lib/http.js:549:13)
      at Executor.execute (node_modules/selenium-webdriver/lib/http.js:475:28)
      at processTicksAndRejections (internal/process/task_queues.js:97:5)
  2) Selenium test
       "after all" hook for "should go to google.com and check title":
     WebDriverError: Process unexpectedly closed with status 1
      at Object.throwDecodedError (node_modules/selenium-webdriver/lib/error.js:522:15)
      at parseHttpResponse (node_modules/selenium-webdriver/lib/http.js:549:13)
      at Executor.execute (node_modules/selenium-webdriver/lib/http.js:475:28)
      at processTicksAndRejections (internal/process/task_queues.js:97:5)
Cleaning up project directory and file based variables
ERROR: Job failed: exit code 1

and I have no idea what I could do to fix this. I am not attached to the current configuration and in fact I think using an Ubuntu image might be a waste, but other things haven't worked for me either. If someone has a completely different configuration which will work with the attached test file then I will gladly adopt it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柳絮泡泡 2025-02-15 19:05:41

我认为您没有安装geckodriver。我在运行ChromiumFirefox的同时遇到了相同的问题,以及seleniumChromiumChromedriver依赖性,因此我没有问题。同时,firefox没有geckodriver依赖关系。

因此,请只需安装geckodriver即可。

PS I可以提供准备硒选项的完整解决方案:

const builder = new Builder().forBrowser(Browser.FIREFOX);

const firefoxOptions = new Firefox.Options();
const testBrowserPath = await which(TEST_BROWSER_NAME);
firefoxOptions.setBinary(testBrowserPath);
firefoxOptions.addArguments(...TEST_BROWSER_SELENIUM_ARGUMENTS);
builder.setFirefoxOptions(firefoxOptions);

const testBrowserDriverPath = await which(TEST_BROWSER_DRIVER_NAME);
const firefoxServiceBuilder = new Firefox.ServiceBuilder(testBrowserDriverPath);
builder.setFirefoxService(firefoxServiceBuilder);

const webDriver = await builder.build();

哪里:

export const TEST_BROWSER_NAME = 'firefox';
export const TEST_BROWSER_SELENIUM_ARGUMENTS = ['--headless'];
export const TEST_BROWSER_DRIVER_NAME = 'geckodriver';

I think you don't have a geckodriver installed. I had the same problem while running chromium and firefox together with selenium. chromium had chromedriver dependency, so I had no problems with it. Meanwhile, firefox had no geckodriver dependency.

So please just install geckodriver.

PS I can provide a full solution for preparing selenium options:

const builder = new Builder().forBrowser(Browser.FIREFOX);

const firefoxOptions = new Firefox.Options();
const testBrowserPath = await which(TEST_BROWSER_NAME);
firefoxOptions.setBinary(testBrowserPath);
firefoxOptions.addArguments(...TEST_BROWSER_SELENIUM_ARGUMENTS);
builder.setFirefoxOptions(firefoxOptions);

const testBrowserDriverPath = await which(TEST_BROWSER_DRIVER_NAME);
const firefoxServiceBuilder = new Firefox.ServiceBuilder(testBrowserDriverPath);
builder.setFirefoxService(firefoxServiceBuilder);

const webDriver = await builder.build();

Where:

export const TEST_BROWSER_NAME = 'firefox';
export const TEST_BROWSER_SELENIUM_ARGUMENTS = ['--headless'];
export const TEST_BROWSER_DRIVER_NAME = 'geckodriver';
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文