如何在 docker 容器内针对外部应用程序运行 cypress 测试

时间:2021-02-07 18:57:28

标签: docker docker-compose cypress

我有一组 cypress e2e 测试。这些测试的代码存在于他们自己的 git repo 中。
通过 cypress cli 手动执行这些测试时,我可以根据环境 DEV/QA/STG 和 PROD 访问不同的 URL。

我可以在本地运行应用程序,但不想这样做,我想做的是在容器内执行测试并让它们访问容器外的应用程序。

为了实现这一点,我在端口 4200 上本地启动了应用程序。我的 cypress 配置在其配置文件中设置了基本 url 属性,我可以从 Cypress cli(不在 docker 中)成功运行测试。

在我的 cypress 测试仓库中,我有以下 Dockerfile

FROM cypress/base:12.18.4
RUN mkdir /app
WORKDIR /app
COPY . /app
RUN npm install
RUN $(npm bin)/cypress verify
RUN ["npm","run","cypress:run"]

执行 docker run 我看到以下错误。

#12 6.411 Cypress could not verify that this server is running:
#12 6.411
#12 6.411   > http://localhost:4200/
#12 6.411
#12 6.411 We are verifying this server because it has been configured as your `baseUrl`.
#12 6.411
#12 6.411 Cypress automatically waits until your server is accessible before running tests.
#12 6.411
#12 6.411 We will try connecting to it 3 more times...
#12 9.417 We will try connecting to it 2 more times...
#12 12.42 We will try connecting to it 1 more time...
#12 12.42
#12 16.43 Cypress failed to verify that your server is running.

不完全确定如何“允许”测试访问我的 localhost:4200

感觉我需要创建一个 docker-compose 文件,但不确定网络细节。

非常感谢任何帮助或想法

1 个答案:

答案 0 :(得分:1)

只需将 --network host 键添加到您的 docker run 命令:

docker run --name cypress --network host cypress/base:12.18.4

https://docs.docker.com/network/network-tutorial-host/