【问题标题】:How can I run cypress tests inside a docker container against an external application如何在 docker 容器内针对外部应用程序运行 cypress 测试
【发布时间】:2021-02-07 18:57:28
【问题描述】:

我有一组 cypress e2e 测试。这些测试的代码位于他们自己的 git 存储库中。
通过 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 文件,但不确定网络细节。

非常感谢任何帮助或想法

【问题讨论】:

    标签: docker docker-compose cypress


    【解决方案1】:

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

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

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

    【讨论】:

      【解决方案2】:

      也许这个也有帮助。

      在你的 cypress.json 中你可以设置:

      "baseUrl": "http://host.docker.internal:4200"

      在这种情况下,您的 cypress 测试将针对运行 docker 容器的机器的 localhost 运行,端口为 4200。

      意思是告诉docker使用宿主机的localhost。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-08-16
        • 1970-01-01
        • 2022-08-23
        • 2018-03-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多