5

I am trying to use codeception to run tests for a php web site which was developed using docker containers. I created a test folder in the web container and put there codecept.phar.

This is the project's setup:

  • docker-compose.yml:

    version: '3'
      services:
        db:
          image: mariadb
          restart: always
          volumes:
            - ./db:/var/lib/mysql
          ports:
            - '3306:3306'
          environment:
            MYSQL_ROOT_PASSWORD: root
        web:
          build: .
          restart: always
          tty: true
          volumes:
            - ./src:/var/www
            - ./build/php.ini:/usr/local/etc/php/php.ini
          ports:
            - '80:80'
          depends_on:
            - db
        chrome:
          image: selenium/standalone-chrome
          restart: always
          ports:
            - '4444:4444'
            - '5900:5900'
          depends_on:
            - web
    
  • acceptance.suite.yml

    actor: AcceptanceTester
    modules:
      enabled:
        - WebDriver:
            url: web
            host: chrome
            browser: chrome
            wait: 15
            window_size: false
        - \Helper\Acceptance
    

I start the containers with:

docker-compose up

And then I attach shell to the web container and run the tests with:

php codecept.phar build && php codecept.phar run --steps

I am running a simple test that basically tries to check an element exists and takes a screenshot: - test1.php:

$I = new AcceptanceTester($scenario);
$I->amOnUrl('http://127.0.0.1');
$I->makeScreenshot();
$I->waitForElement(".modal");

But the test is not running properly because chrome container cannot connect to web container. The screenshot shows a page that says:

This site can't be reached
127.0.0.1 refused to connect

And this is the error shown in the attached shell running codeception:

[Facebook\WebDriver\Exception\NoSuchElementException] no such element: Unable to locate element: {"method":"css selector","selector":".modal"}
(Session info: chrome=68.0.3440.84)
(Driver info: chromedriver=2.41.578700 (2f1ed5f9343c13f73144538f15c00b370eda6706),platform=Linux 4.9.0-7-amd64 x86_64)

Although I am able to connect to http://127.0.0.1:4444/wd/hub

This is the output I get from the shell running the "docker-compose up":

chrome_1 | INFO [GridLauncherV3.launch] - Selenium build info: version: '3.14.0', revision: 'aacccce0'
chrome_1 | INFO [GridLauncherV3$1.launch] - Launching a standalone Selenium Server on port 4444
chrome_1 | INFO::main: Logging initialized @286ms to org.seleniumhq.jetty9.util.log.StdErrLog
chrome_1 | INFO [SeleniumServer.boot] - Selenium Server is up and running on port 4444
chrome_1 | INFO [ActiveSessionFactory.apply] - Capabilities are: {"browserName": "chrome"}
chrome_1 | INFO [ActiveSessionFactory.lambda$apply$11] - Matched factory org.openqa.selenium.remote.server.ServicedSession$Factory (provider: org.openqa.selenium.chrome.ChromeDriverService)
chrome_1 | Starting ChromeDriver 2[.14513.5517089780809 .(328f61]e[dS5EfV9E3R4E3]c:1 3bfi7n3d1(4)4 5r3e8tfu1r5nce0d0 ba3n7 0eerdrao6r7,0 6e) onr rpnoor=t9 91:7 2C9a4n
chrome_1 | Onnloyt  laoscsailg nc ornenqeucetsitoends  aadrder easlsl o(w9e9d).
chrome_1 | INFO [ProtocolHandshake.createSession] - Detected dialect: OSS
chrome_1 | INFO [RemoteSession$Factory.lambda$performHandshake$0] - Started new session ed60fb03c497f98a7e23bdede05c4bb9 (org.openqa.selenium.chrome.ChromeDriverService)
chrome_1 | INFO [ActiveSessions$1.onStop] - Removing session ed60fb03c497f98a7e23bdede05c4bb9 (org.openqa.selenium.chrome.ChromeDriverService)
chrome_1 | INFO [ActiveSessionFactory.apply] - Capabilities are: {"browserName": "chrome"}
chrome_1 | INFO [ActiveSessionFactory.lambda$apply$11] - Matched factory org.openqa.selenium.remote.server.ServicedSession$Factory (provider: org.openqa.selenium.chrome.ChromeDriverService)
chrome_1 | Starting ChromeDriver 2.41.578700 (2f1ed5f9343c13f73144538f15c00b370eda6706) on port 17381
chrome_1 | Only local connections are allowed.
chrome_1 | [1535110012.491][SEVERE]: bind() returned an error, errno=99: Cannot assign requested address (99)
chrome_1 | INFO [ProtocolHandshake.createSession] - Detected dialect: OSS
chrome_1 | INFO [RemoteSession$Factory.lambda$performHandshake$0] - Started new session 3c49c360624e02460995193c50f43bd3 (org.openqa.selenium.chrome.ChromeDriverService)
chrome_1 | INFO [ActiveSessions$1.onStop] - Removing session 3c49c360624e02460995193c50f43bd3 (org.openqa.selenium.chrome.ChromeDriverService)

I think that set up a network for the docker-compose containers should solve the problem. I tried to follow the docker documentation (Network configuration reference) to set the network as "host" , but it seems to be outdated, as names are not allowed in version 3.

Also tried to set a link from chrome to web, and run chrome without compose (docker run --net=host selenium/standalone-chrome) but that made no change.

Would you know a way to make this work? Thanks for your help!

KarlR
  • 1,545
  • 12
  • 28
nanaoa
  • 51
  • 1
  • 2

4 Answers4

1

Try to allow non localhost connection into chrome, by set JAVA_OPTS

    chrome:
      image: selenium/standalone-chrome
      restart: always
      ports:
        - '4444:4444'
        - '5900:5900'
      environment:
        - JAVA_OPTS=-Dwebdriver.chrome.whitelistedIps=
      depends_on:
        - web
GetoX
  • 4,225
  • 2
  • 33
  • 30
1

The solution is simple.

Refer this Using selenium/standalone-chrome in docker-compose connecting with Python's selenium

You need to set up the network like this

version: '3.8'

networks:
    web:
      external: true
      driver:  bridge

services:
    chrome:
        image: selenium/standalone-chrome:latest
        hostname: chrome
        networks:
          - web
        privileged: true
        shm_size: 2g

    framework:
        build: .
        networks:
            - web
        depends_on: 
            - chrome

In your case, you can modify this according to your need. Remember you can connect only at http://chrome:4444/ when you're running code inside docker.

Abhishek Puri
  • 345
  • 3
  • 6
  • 1
    returns: ERROR: Network web declared as external, but could not be found. Please create the network manually using `docker network create web` and try again. – Robert Sinclair Apr 21 '21 at 06:22
0

Have you tried network_mode: "host" in your docker-compose?

Carsten
  • 56
  • 5
0
This site can't be reached
127.0.0.1 refused to connect

The reason why selenium is unable to reach your web server is that it is on other container and not on the same container, So to fix this you need to provide the name of the specific container and port like :

$I->amOnUrl('http://web:80'); 

But I am still sceptical as you have already mapped Host port 80 and your container port 80, so if above does not work, try

$I->amOnUrl('http://host.docker.internal:80'); 

here docker.host.internal will refer to your host system port 80 so since you mapped it to the web container it should definitely work. Let me know if this works for you

Farhaan Shaik
  • 123
  • 1
  • 2
  • 8