连接被拒绝:Docker 中的 PHPUnit selenium

Connection refused: PHPUnit selenium in Docker

我正在像这样链接两个容器 (docker-compose.yml):

test:
    container_name: test
    image: test
    ports:
      - "7761:80"
    links:
      - webdriver

webdriver:
    container_name: webdriver
    image: webdriver

'Test' 包含一个 运行 symfony 网站。 'Webdriver' 是自定义 ubuntu 图像,Selenium 服务器 运行 ChromeDriver 在端口 4444 上。

我是 运行 测试容器内此文件的 PHPunit:

class SeleniumTest extends PHPUnit_Extensions_Selenium2TestCase
{
  public function setUp()
  {
      $this->setHost('webdriver');
      $this->setPort(4444);
      $this->setBrowserUrl('http://localhost:7761');
      $this->setBrowser('chrome');
  }

  protected function login() {
    $this->url('/');

    $content = $this->byTag('body')->text();
    print $content;

    #...do tests...
  }
}

作曲家:

    "phpunit/phpunit": "~4",
    "phpunit/phpunit-selenium": "~1"

无论BrowserUrl的值是多少,我都无法连接。我尝试了 0.0.0.0、本地主机(端口 80 和 7761)、我的容器的 ID 等

这是日志:

root@/var/www# bin/phpunit tests
PHPUnit 4.5.1 by Sebastian Bergmann and contributors.

EThis webpage is not available
ERR_CONNECTION_REFUSED
RELOAD
DETAILS

Time: 580 ms, Memory: 3.50Mb

There was 1 error:

1) SeleniumTest::testLogin
PHPUnit_Extensions_Selenium2TestCase_WebDriverException: no such element: Unable to locate element: {"method":"name","selector":"subscriptionForm"}
  (Session info: chrome=48.0.2564.97)
  (Driver info: chromedriver=2.21.371461 (633e689b520b25f3e264a2ede6b74ccc23cb636a),platform=Linux 3.19.0-39-generic x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 12 milliseconds

有什么想法吗?


编辑:修复了使用 3 容器计划的问题,请参阅下面的解决方案:

PHP单位:

  public function setUp()
  {
      $this->setHost('webdriver');
      $this->setPort(4444);
      $this->setBrowserUrl('http://website');
      $this->setBrowser('chrome');
  }

docker-compose.yml:

test:
    container_name: test
    image: test

webdriver:
    container_name: webdriver
    image: webdriver
    links:
      - test:website

webdriver-tests:
    container_name: webdriver-tests
    image: custom-image-with-tests
    links:
       - webdriver

webdriver-tests 容器可以检查网站代码,包括测试,或单独的测试 repo。

您需要 link 从 webdriver 容器到 test 容器,但它会产生循环导入。您可能会在 link https://github.com/docker/compose/issues/666.

之后找到一些解决方法

我建议将主站点的测试分开放到另一个容器中,因为它们有不同的职责。此外,此解决方案可以避免循环导入。