Docker容器中具有Selenium的Codeception无法看到站点对其进行测试

时间:2018-07-03 16:14:43

标签: php docker phpunit codeception

我使用docker-compose在LEMP堆栈上运行我的网站,一切正常。

我遵循了Selenium docker的官方文档,我的composer文件是其相关文件的摘要:

version: "3"
services:
  selenium-hub:
    image: selenium/hub:3.12.0-cobalt
    container_name: selenium-hub
    ports:
      - "4444:4444"
  chrome:
    image: selenium/node-chrome:3.12.0-cobalt
    depends_on:
      - selenium-hub
    environment:
      - HUB_HOST=selenium-hub
      - HUB_PORT=4444
  firefox:
    image: selenium/node-firefox:3.12.0-cobalt
    depends_on:
      - selenium-hub
    environment:
      - HUB_HOST=selenium-hub
      - HUB_PORT=4444

然后我已经按照文档完全配置了代码接收:

acceptance.suite.yml

actor: AcceptanceTester
modules:
    enabled:
        - WebDriver:
            url: http://172.22.1.2
            browser: chrome
        - \Helper\Acceptence

TestCest.php

...
public function tryToTest(AcceptanceTester $I)
{
    $I->amOnPage('/');
    $I->see('Homepage');
}
...

运行测试时,它们会超时并且无法说出This site can't be reached。 docker容器肯定正在运行,并且可以通过Web浏览器通过IP 172.22.1.2容器进行访问

当我使用默认的PhpBrowser时,该测试全部运行并且正常工作,因此似乎代码接收无法访问selenium docker容器。有什么建议吗?

编辑......... 添加docker-compose以获得清晰感

version: "3.1"
services:

    mariadb:
      image: mariadb:10.1
      container_name: reports-mariadb
      working_dir: /application
      volumes:
        - .:/application
      environment:
        - MYSQL_ROOT_PASSWORD=reports
        - MYSQL_DATABASE=reports
        - MYSQL_USER=reports
        - MYSQL_PASSWORD=reports
      ports:
        - "8011:3306"
      networks:
        reports:
          ipv4_address: 172.22.1.3

    webserver:
      image: nginx:latest
      container_name: reports-nginx
      working_dir: /application
      volumes:
          - .:/application
          - ./dev/nginx/nginx.conf:/etc/nginx/conf.d/default.conf
      ports:
       - "8010:80"
      networks:
        reports:
          ipv4_address: 172.22.1.2

    php-fpm:
      build: dev/php-fpm
      container_name: reports-php-fpm
      working_dir: /application
      volumes:
        - .:/application
        - ./dev/php-fpm/php-ini-overrides.ini:/etc/php/7.2/fpm/conf.d/99-overrides.ini
      networks:
        reports:
          ipv4_address: 172.22.1.4

networks:
  reports:
    driver: bridge
    ipam:
      config:
        - subnet: 172.22.1.0/24

1 个答案:

答案 0 :(得分:0)

发布问题后,它就来了...。默认情况下,它在本地主机上运行,​​但是由于该应用程序在虚拟网络172.22.1.0/24内部运行,因此与应用程序容器相比,它显然是一个不同的本地主机。

所以我将容器移到我的主要docker-compose文件中,并将selenium-hub添加到虚拟网络中,如下所示:

version: "3.1"
services:

    mariadb:
      image: mariadb:10.1
      container_name: reports-mariadb
      working_dir: /application
      volumes:
        - .:/application
      environment:
        - MYSQL_ROOT_PASSWORD=reports
        - MYSQL_DATABASE=reports
        - MYSQL_USER=reports
        - MYSQL_PASSWORD=reports
      ports:
        - "8011:3306"
      networks:
        reports:
          ipv4_address: 172.22.1.3

    webserver:
      image: nginx:latest
      container_name: reports-nginx
      working_dir: /application
      volumes:
          - .:/application
          - ./dev/nginx/nginx.conf:/etc/nginx/conf.d/default.conf
      ports:
       - "8010:80"
      networks:
        reports:
          ipv4_address: 172.22.1.2

    php-fpm:
      build: dev/php-fpm
      container_name: reports-php-fpm
      working_dir: /application
      volumes:
        - .:/application
        - ./dev/php-fpm/php-ini-overrides.ini:/etc/php/7.2/fpm/conf.d/99-overrides.ini
      networks:
        reports:
          ipv4_address: 172.22.1.4

    selenium-hub:
        image: selenium/hub:3.12.0-cobalt
        container_name: selenium-hub
        ports:
          - "4444:4444"
    chrome:
        image: selenium/node-chrome:3.12.0-cobalt
        depends_on:
          - selenium-hub
        environment:
          - HUB_HOST=selenium-hub
          - HUB_PORT=4444
    firefox:
        image: selenium/node-firefox:3.12.0-cobalt
        depends_on:
          - selenium-hub
        environment:
          - HUB_HOST=selenium-hub
          - HUB_PORT=4444

networks:
  reports:
    driver: bridge
    ipam:
      config:
        - subnet: 172.22.1.0/24

然后,您只需要告诉代码接收它正在运行的主机:

actor: AcceptanceTester
modules:
    enabled:
        - WebDriver:
            url: http://172.22.1.2
            host: 172.22.1.5
            browser: chrome
        - \Helper\Acceptence