如何在Alpine Linux上运行Angular6 E2E测试



尝试使用 Alpine Linux 运行 E2E 测试时,我们遇到了以下神秘错误:

E/launcher - spawn (...)/selenium/chromedriver_2.40 ENOENT

如何复制

docker run -it --name my-alpine --rm alpine:latest /bin/ash
apk add --update nodejs nodejs-npm chromium 
&& export CHROME_BIN=/usr/bin/chromium-browser
npm install -g @angular/cli
ng new testAlpine
cd testAlpine
ng e2e

结果

[14:10:54] I/file_manager - creating folder /testAlpine/node_modules/protractor/node_modules/webdriver-manager/selenium
ℹ 「wdm」: Compiled successfully.
[14:10:54] I/update - chromedriver: unzipping chromedriver_2.40.zip
[14:10:55] I/update - chromedriver: setting permissions to 0755 for /testAlpine/node_modules/protractor/node_modules/webdriver-manager/selenium/chromedriver_2.40
(node:121) [DEP0022] DeprecationWarning: os.tmpDir() is deprecated. Use os.tmpdir() instead.
[14:10:55] I/launcher - Running 1 instances of WebDriver
[14:10:55] I/direct - Using ChromeDriver directly...
[14:10:55] E/launcher - spawn /testAlpine/node_modules/protractor/node_modules/webdriver-manager/selenium/chromedriver_2.40 ENOENT
[14:10:55] E/launcher - Error: spawn /testAlpine/node_modules/protractor/node_modules/webdriver-manager/selenium/chromedriver_2.40 ENOENT
at _errnoException (util.js:1024:11)
at Process.ChildProcess._handle.onexit (internal/child_process.js:190:19)
at onErrorNT (internal/child_process.js:372:16)
at _combinedTickCallback (internal/process/next_tick.js:138:11)
at process._tickCallback (internal/process/next_tick.js:180:9)
[14:10:55] E/launcher - Process exited with error code 199
An unexpected error occured: undefined

一些调查细节

假设量角器使用 Nodejs spawn 函数,我们尝试从 ash 运行命令,结果是"未找到"消息! 挖掘它发现这可能与缺少动态链接库有关。

为了验证假设,我们安装了 binutils 来检查动态链接依赖关系。

apk add --update binutils
readelf -l /testAlpine/node_modules/protractor/node_modules/webdriver-manager/selenium/chromedriver_2.40

结果显示了Alpine Linux中不存在的依赖关系,

/lib64/ld-linux-x86-64.so.2

Elf file type is EXEC (Executable file)
Entry point 0x490000
There are 11 program headers, starting at offset 64
Program Headers:
Type           Offset             VirtAddr           PhysAddr
FileSiz            MemSiz              Flags  Align
PHDR           0x0000000000000040 0x0000000000200040 0x0000000000200040
0x0000000000000268 0x0000000000000268  R      0x8
INTERP         0x00000000000002a8 0x00000000002002a8 0x00000000002002a8
0x000000000000001c 0x000000000000001c  R      0x1
[Requesting program interpreter: /lib64/ld-linux-x86-64.so.2]
LOAD           0x0000000000000000 0x0000000000200000 0x0000000000200000
0x000000000028f27c 0x000000000028f27c  R      0x1000
LOAD           0x0000000000290000 0x0000000000490000 0x0000000000490000

尝试使用网络驱动程序管理器获取更新的chrome驱动程序版本也不起作用。 下载的版本存在相同的问题。

npm install -g webdriver-manager
webdriver-manager update
webdriver-manager: using global installed version 12.0.6
[14:30:20] I/file_manager - creating folder /usr/lib/node_modules/webdriver-manager/selenium
[14:30:21] I/update - chromedriver: unzipping chromedriver_2.40.zip
[14:30:21] I/update - chromedriver: setting permissions to 0755 for /usr/lib/node_modules/webdriver-manager/selenium/chromedriver_2.40
/testAlpine # /usr/lib/node_modules/webdriver-manager/selenium/chromedriver_2.40
/bin/ash: /usr/lib/node_modules/webdriver-manager/selenium/chromedriver_2.40: not found

随机的坚韧

  • 随着Alpine Linux迁移到musl libc,是否有必要重建 chromeDriver使用它?

有必要使用 musl libc 重建 chromedriver 或使用 alpine 存储库中的 chromedriver 包,您可以使用apk add --no-cache chromium-chromedriver安装它。

您还需要告诉量角器使用正确的二进制文件chromeDriver: '/usr/bin/chromedriver'量角器配置中。

对于将来遇到此问题的人,我完全无法获得alpine+angular+protractor+webdriver-manager的组合以任何开箱即用的支持方式进行合作。

经过多次修补,我设法让它工作

  1. 使用Web驱动程序管理器生成配置
  2. 使用阿尔卑斯山注册表中的驱动程序覆盖 Web 驱动程序管理器驱动程序

Dockerfile:

FROM node:12-alpine
RUN 
echo "http://dl-cdn.alpinelinux.org/alpine/edge/community" >> /etc/apk/repositories 
&& echo "http://dl-cdn.alpinelinux.org/alpine/edge/main" >> /etc/apk/repositories 
&& echo "http://dl-cdn.alpinelinux.org/alpine/edge/testing" >> /etc/apk/repositories 
&& apk --no-cache update 
&& apk --no-cache upgrade 
&& apk add --no-cache --virtual .build-deps chromium chromium-chromedriver 
&& rm -rf /var/cache/apk/* /tmp/*
ENV CHROME_BIN /usr/bin/chromium-browser

量角器.js

capabilities: {
browserName: 'chrome',
chromeOptions: {
args: ['--headless', '--disable-gpu', '--no-sandbox'],
},
},
directConnect: true,

脚本看起来有点像这样:

before_script:
- yarn install --frozen-lockfile --cache-folder "$CI_PROJECT_DIR/.yarn"
script:
- npx webdriver-manager update --versions.chrome="$(chromium-browser --version | cut -d ' ' -f 2)"
- mv "$CI_PROJECT_DIR/node_modules/webdriver-manager/selenium/chromedriver_$(chromium-browser --version | cut -d ' ' -f 2)" "$CI_PROJECT_DIR/node_modules/webdriver-manager/selenium/chromedriver.bak"
- ln -s "/usr/bin/chromedriver" "$CI_PROJECT_DIR/node_modules/webdriver-manager/selenium/chromedriver_$(chromium-browser --version | cut -d ' ' -f 2)"
- yarn run e2e --webdriverUpdate=false

请注意,上面提到的代码是 Gitlab CI 配置的代码:根据您的需求,您可能必须:

  • 将其转换为可用于您的环境的脚本
  • 删除/替换一些环境变量

相关内容

  • 没有找到相关文章

最新更新