Docker + Selenium Chrome 驱动程序错误:服务器提前终止,状态为 127

问题描述 投票:0回答:1

我有一个运行一些 Selenium 测试脚本的 Docker 脚本。

以下是 Dockerfile 内容:

FROM selenium/hub:nightly

WORKDIR /usr/src/app

RUN sudo apt-get update

COPY . /usr/src/app/

RUN sudo curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -

RUN sudo apt-get install nodejs -y

CMD ./start.sh

“start.sh”文件看起来有点像这样:

#!/usr/bin/env bash
echo "init..."

npm run test:dev

“test”是一个 NodeJs 命令(来自 package.json),它运行:

"test": "cucumber-js features/"

这将加载 Selenium Chrome 驱动程序并运行一系列 Selenium/Cucumber 测试。

在桌面(MacOS)上运行时,一切都运行良好!当我使用 Docker 运行此程序时,我在每次测试开始时都会收到以下错误。我认为测试甚至无法开始,因为它无法加载 Chrome 驱动程序:

Error: Server terminated early with status 127
    at /usr/src/app/node_modules/selenium-webdriver/remote/index.js:259:24

我用来构建和运行 docker 的命令是:

docker build - < Dockerfile && docker run selenium_automation_tests < Dockerfile

我们的run.sh文件的内容是:

#!/usr/bin/env bash
docker build --platform linux/x86_64 -t selenium_automation_tests .
docker run --platform linux/x86_64 -d -p 4442-4444:4442-4444  -it selenium_automation_tests #/bin/bash

我已经在 Google 和 stackoverflow 上搜索了此错误(“服务器提前终止,状态为 127”),并尝试了很多建议,但到目前为止没有任何效果。我该怎么做才能摆脱这个错误并在 Docker 中运行 Selenium/Cucumber 测试?

node.js docker selenium-webdriver dockerfile
1个回答
0
投票

假设您的项目如下所示:

├── Dockerfile
├── features
│   ├── step_definitions
│   │   └── test_steps.js
│   └── test.feature
├── package.json
├── run.sh
└── start.sh

🗎

Dockerfile

FROM selenium/standalone-chrome:latest

USER root

RUN curl -fsSL https://deb.nodesource.com/setup_18.x | bash - && \
    apt-get install -y nodejs

WORKDIR /usr/src/app

COPY . .

RUN npm install

RUN chmod +x ./start.sh

USER seluser

CMD ["./start.sh"]

🗎

package.json

{
  "name": "selenium-test",
  "type": "module",
  "version": "1.0.0",
  "main": "index.js",
  "scripts": {
    "test:dev": "cucumber-js features/"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.18.2",
    "selenium-webdriver": "^4.18.1",
    "supabase": "^1.145.4"
  },
  "keywords": [],
  "description": "",
  "devDependencies": {
    "@cucumber/cucumber": "^10.3.1",
    "chai": "^5.1.0",
    "chromedriver": "^122.0.6"
  }
}

🗎

test.feature

Feature: Chrome Loads Page

  Scenario: Load example page
    Given I am on the example.com page
    Then the page has loaded

🗎

test_steps.js
(各种选项可确保 Chrome 在容器内愉快地运行。它们可能并不是全部必要。)

import { Given, Then, After } from '@cucumber/cucumber';
import { Builder, By, until } from 'selenium-webdriver';
import assert from 'assert';
import chrome from 'selenium-webdriver/chrome.js';

let driver;

let options = new chrome.Options();
options.addArguments('--headless');
options.addArguments('--no-sandbox');
options.addArguments('--disable-dev-shm-usage');
options.addArguments('--disable-gpu');
options.addArguments('--disable-extensions');
options.addArguments('--remote-debugging-port=9222');

Given('I am on the example.com page', async function () {
  driver = await new Builder().forBrowser('chrome').setChromeOptions(options).build();
  await driver.get('http://www.example.com');
});

Then('the page has loaded', async function () {
  let element = await driver.wait(until.elementLocated(By.css('h1')), 10000);
  let text = await element.getText();
  
  assert.strictEqual(text, "Example Domain", 'The page did not load correctly.');
});

After(async function () {
  if (driver) {
    await driver.quit();
  }
});

run.sh
start.sh
文件与问题中提供的相同。

© www.soinside.com 2019 - 2024. All rights reserved.