Docker与Laravel Dusk合作

时间:2018-05-27 09:25:10

标签: laravel docker docker-compose laravel-dusk

我在docker中运行了一个Laravel应用程序并尝试运行浏览器测试。由于app和chrome在单独的容器中运行,我从测试文件中引用了命名容器 - http://selenium-hub:4444。触发测试套件后,它会显示以下响应:

There was 1 error:

1) Tests\Browser\ExampleTest::testBasicExample
Facebook\WebDriver\Exception\WebDriverException: JSON decoding of remote response failed.
Error code: 4
The response: '<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <link rel="stylesheet" type="text/css" href="/assets/displayhelpservlet.css" media="all"/>
  <link href="/assets/favicon.ico" rel="icon" type="image/x-icon" />
  <script src="/assets/jquery-3.1.1.min.js" type="text/javascript"></script>
  <script src="/assets/displayhelpservlet.js" type="text/javascript"></script>
  <script type="text/javascript">
    var json = Object.freeze('{"consoleLink":"/grid/console","type":"Grid Hub","version":"3.12.0","class":"org.openqa.grid.web.servlet.DisplayHelpServlet$DisplayHelpServletConfig"}')
;
  </script>
</head>
<body>

<div id="content">
  <div id="help-heading">
    <h1><span id="logo"></span></h1>
    <h2>Selenium <span class="se-type"></span>&nbsp;v.<span class="se-version"></span></h2>
  </div>

  <div id="content-body">
    <p>
      Whoops! The URL specified routes to this help page.
    </p>
    <p>
      For more information about Selenium <span class="se-type"></span> please see the
      <a class="se-docs">docs</a> and/or visit the <a class="se-wiki">wiki</a>.
      <span id="console-item">
        Or perhaps you are looking for the Selenium <span class="se-type"></span> <a class="se-console">console</a>.
      </span>
    </p>
    <p>
      Happy Testing!
    </p>
  </div>

  <div>
    <footer id="help-footer">
      Selenium is made possible through the efforts of our open source community, contributions from
      these <a href="https://github.com/SeleniumHQ/selenium/blob/master/AUTHORS">people</a>, and our
      <a href="http://www.seleniumhq.org/sponsors/">sponsors</a>.
   </footer>
  </div>
 </div>

</body>
</html>'


/var/www/html/code/hackerpair/vendor/facebook/webdriver/lib/Remote/HttpCommandExecutor.php:298
/var/www/html/code/hackerpair/vendor/facebook/webdriver/lib/Remote/RemoteWebDriver.php:126
/var/www/html/code/hackerpair/tests/DuskTestCase.php:39
/var/www/html/code/hackerpair/vendor/laravel/dusk/src/Concerns/ProvidesBrowser.php:189
/var/www/html/code/hackerpair/vendor/laravel/framework/src/Illuminate/Support/helpers.php:768
/var/www/html/code/hackerpair/vendor/laravel/dusk/src/Concerns/ProvidesBrowser.php:190
/var/www/html/code/hackerpair/vendor/laravel/dusk/src/Concerns/ProvidesBrowser.php:92
/var/www/html/code/hackerpair/vendor/laravel/dusk/src/Concerns/ProvidesBrowser.php:64
/var/www/html/code/hackerpair/tests/Browser/ExampleTest.php:22

ERRORS!
Tests: 1, Assertions: 0, Errors: 1.

显然,它无法到达app容器。我尝试为测试设置命名URL,但它没有工作:

ExampleTest.php

class ExampleTest extends DuskTestCase
{
    /**
     * A basic browser test example.
     *
     * @return void
     */

    public function testBasicExample()
    {
        $this->browse(function (Browser $browser) {
            $browser->visit('http://app:80/')
                    ->assertSee('Welcome');
        });
    }
}

DuskTestCase.php

protected function driver()
    {
        $options = (new ChromeOptions)->addArguments([
            '--disable-gpu',
            '--headless'
        ]);

        return RemoteWebDriver::create(
            'http://selenium-hub:4444', DesiredCapabilities::chrome()->setCapability(
                ChromeOptions::CAPABILITY, $options
            )
        );
    }

docker-compose.yml

version: '3'

services:
  app:
    build:
      .
    volumes:
      - ./src:/var/www/html
    depends_on:
      - mysql
    ports:
      - 80:80

  mysql:
    image: mysql:5.7
    environment:
      MYSQL_ROOT_PASSWORD: qwerty

  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    links:
      - mysql:db
    ports:
      - 8765:80
    environment:
      MYSQL_ROOT_PASSWORD: qwerty
      PMA_HOST: mysql
    depends_on:
      - mysql

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

0 个答案:

没有答案