首页 > 解决方案 > TestCafe 铬测试作为 docker build 的一部分运行

问题描述

出于某种原因,在我们的 CI 中,我们需要在 docker 容器内运行节点测试(包括获取依赖项等)。所以,我试图让 UI 测试作为 docker build 的一部分运行。

这就是我的 Dockerfile 的样子:

FROM testcafe/testcafe:1.3.3

USER root

#some packages needed for some dependencies
RUN apk add --no-cache yarn python make build-base vim curl 

RUN ln -s /opt/testcafe/docker/testcafe-docker.sh /usr/local/bin/testcafe-docker

WORKDIR /usr/src/app

RUN yarn config set registry https://private-npm-registry --global

COPY package*.json ./

RUN yarn

COPY . .

RUN yarn test:ui:ci

# "test:ui:clean": "rm -rf uitests/reports"
# "test:ui:ci-debug": "yarn test:ui:clean; testcafe-docker 'chromium --no-sandbox' uitests/tests -S -s uitests/reports/screenshots --video uitests/reports/videos -r spec,json:uitests/reports/report.json,html:uitests/reports/report.html",
# "test:ui:ci": "start-server-and-test serve http://127.0.0.1:8080 test:ui:ci-debug"

我明白了ERROR Unable to establish one or more of the specified browser connections. This can be caused by network issues or remote device failure.

另外,我尝试使用user用户,但在运行测试之前在文件reports夹内创建文件夹时出现权限错误。uitests

我尝试了有无--no-sandbox选项,遇到了同样的问题。也试过chromium:headless --no-sandbox了,同样的错误。

请问有什么建议吗?谢谢。

更新:还尝试了用户:(user通过使用文件夹进行报告来避免权限问题/tmp)并遇到同样的问题:

20-Jul-2019 23:53:33    > yarn test:ui:clean; whoami; ls -sail; testcafe-docker 'chromium --no-sandbox' uitests/tests -S -s /tmp/uitests/reports/screenshots --video /tmp/uitests/reports/videos -r spec,json:/tmp/uitests/reports/report.json,html:/tmp/uitests/reports/report.html
20-Jul-2019 23:53:33    
20-Jul-2019 23:53:34    $ rm -rf /tmp/uitests/reports
20-Jul-2019 23:53:34    user
20-Jul-2019 23:53:34    total 528
20-Jul-2019 23:53:34         13      4 drwxr-xr-x   10 user     user          4096 Jul 20 13:52 .
20-Jul-2019 23:53:34         12      4 drwxr-xr-x    8 root     root          4096 Jul 20 13:52 ..
20-Jul-2019 23:53:34         79      4 -rw-r--r--    1 root     root            20 Jul 19 07:06 .dockerignore
20-Jul-2019 23:53:34         75      4 -rw-r--r--    1 root     root            45 Jul 19 07:06 .eslintignore
20-Jul-2019 23:53:34         83      4 -rw-r--r--    1 root     root           790 Jul 19 07:06 .eslintrc
20-Jul-2019 23:53:34         78      4 drwxr-xr-x    8 root     root          4096 Jul 20 13:48 .git
20-Jul-2019 23:53:34         82      4 -rw-r--r--    1 root     root           326 Jul 20 13:48 .gitignore
20-Jul-2019 23:53:34         87      4 -rw-r--r--    1 root     root           189 Jul 19 07:06 Dockerfile
20-Jul-2019 23:53:34         81      4 -rw-r--r--    1 root     root           592 Jul 20 13:48 DockerfileUITest
20-Jul-2019 23:53:34         89      4 -rw-r--r--    1 root     root           451 Jul 19 07:06 README.md
20-Jul-2019 23:53:34         90      4 drwxr-xr-x    3 root     root          4096 Jul 19 07:06 backend
20-Jul-2019 23:53:34       4096      4 drwxr-xr-x    3 user     user          4096 Jul 20 13:53 build
20-Jul-2019 23:53:34         85      4 -rwxr-xr-x    1 root     root           959 Jul 19 07:06 build.sh
20-Jul-2019 23:53:34         84      4 -rw-r--r--    1 root     root          1124 Jul 19 07:06 deploy.yaml
20-Jul-2019 23:53:34         91      4 drwxr-xr-x 1348 user     user          4096 Jul 20 13:52 node_modules
20-Jul-2019 23:53:34         74      4 -rw-r--r--    1 root     root          2959 Jul 20 13:48 package.json
20-Jul-2019 23:53:34         76      4 drwxr-xr-x    2 root     root          4096 Jul 19 07:06 public
20-Jul-2019 23:53:34         88      4 -rwxr-xr-x    1 root     root           742 Jul 19 07:06 runUITestsInCI.sh
20-Jul-2019 23:53:34         77      4 drwxr-xr-x    8 root     root          4096 Jul 19 07:06 src
20-Jul-2019 23:53:34         80      4 drwxr-xr-x    7 root     root          4096 Jul 19 07:06 uitests
20-Jul-2019 23:53:34         86    448 -rw-r--r--    1 root     root        454847 Jul 20 13:48 yarn.lock
20-Jul-2019 23:53:34    Using locally installed version of TestCafe.
20-Jul-2019 23:55:36    ERROR Unable to establish one or more of the specified browser connections. This can be caused by network issues or remote device failure.
20-Jul-2019 23:55:36    
20-Jul-2019 23:55:36    Type "testcafe -h" for help.

更新 2: 也尝试使用 firefox 和同样的问题:ERROR Unable to establish one or more of the specified browser connections. This can be caused by network issues or remote device failure.

更新 3: 抱歉耽搁了。在别的事情上分心了。在 CI 和本地机器上都试过了,这是相同的行为。还尝试了评论中的建议,echo -e '#!/bin/sh\n/usr/bin/chromium-browser --no-sandbox --remote-debugging-port=9222 --headless' > /usr/local/bin/testcafe-docker并在 CI 和本地环境中获得了以下输出。

02-Dec-2019 18:45:17    DevTools listening on ws://127.0.0.1:9222/devtools/browser/711c6409-be9a-4e08-959e-0c994c8c5742
02-Dec-2019 18:45:17    [1202/074517.060637:ERROR:gl_implementation.cc(282)] Failed to load /usr/lib/chromium/swiftshader/libGLESv2.so: Error loading shared library /usr/lib/chromium/swiftshader/libGLESv2.so: No such file or directory
02-Dec-2019 18:45:17    [1202/074517.064824:ERROR:viz_main_impl.cc(176)] Exiting GPU process due to errors during initialization
02-Dec-2019 18:45:17    [1202/074517.072317:WARNING:dns_config_service_posix.cc(341)] Failed to read DnsConfig.
02-Dec-2019 18:45:17    [1202/074517.072782:WARNING:gpu_process_host.cc(1220)] The GPU process has crashed 1 time(s)
02-Dec-2019 18:45:17    [1202/074517.135149:ERROR:gl_implementation.cc(282)] Failed to load /usr/lib/chromium/swiftshader/libGLESv2.so: Error loading shared library /usr/lib/chromium/swiftshader/libGLESv2.so: No such file or directory
02-Dec-2019 18:45:17    [1202/074517.139203:ERROR:viz_main_impl.cc(176)] Exiting GPU process due to errors during initialization
02-Dec-2019 18:45:17    [1202/074517.143251:WARNING:gpu_process_host.cc(1220)] The GPU process has crashed 2 time(s)
02-Dec-2019 18:45:17    [1202/074517.208950:WARNING:gpu_process_host.cc(990)] Reinitialized the GPU process after a crash. The reported initialization time was 0 ms
02-Dec-2019 18:45:17    [1202/074517.209227:ERROR:gpu_channel_manager.cc(397)] ContextResult::kFatalFailure: Failed to create shared context for virtualization.

标签: automated-testschromiume2e-testingtestcafedocker-build

解决方案


最近的“Chromium”版本不允许您在root用户下运行它们。user在运行 TestCafe 测试之前将用户从“root”更改为。此外,您需要设置创建新文件夹的权限。请参阅写入共享卷 docker 中的详细说明

...
USER user
RUN yarn test:ui:ci

推荐阅读