Testing Codeception with Selenium service

I’m trying to setup selenium standalone chrome service to test my Codeception suit.

I run chrome standalone as a service:

services:
  - mysql:latest
  - selenium/standalone-chrome:latest

And then I setup the connections for my Codeception test uses WebDriver with extension for WordPress:

WPWebDriver:
   url: 'http://localhost'
   host: 'selenium__standalone-chrome'
   browser: chrome
   port: 4444
   restart: true
   wait: 2
   adminUsername: admin
   adminPassword: 1234
   adminUrl: /wp-admin

All other tests run well but when it comes to the suite where that uses Selenium it refuses to connect:

Time: 7.55 seconds, Memory: 16.00MB

There was 1 failure:

---------
1) SampleTestCept: Test if wp is working in selenium
 Test  tests/php/acceptance/SampleTestCept.php
 Step  See "Just another WordPress site"
 Fail  Failed asserting that  on page /
--> This site can’t be reached
localhost refused to connect.
Try:
Checking the connection
Checking the proxy and the firewall
ERR_CONNECTION_REFUSED
Reload
DETAILS
--> contains "this site can't be reached".

Scenario Steps:

 2. $I->see("This site can't be reached") at tests/php/acceptance/SampleTestCept.php:6
 1. $I->amOnPage("/") at tests/php/acceptance/SampleTestCept.php:4

Any ideas of what am I doing wrong?

Selenium server imho does not have to connect to localhost, because service is “another host”. You have to use HOSTNAME environment variable to find runner hostname. In NodeJS I use next

http://' + (process.env.HOSTNAME || 'localhost') + ':' + (process.env.PORT || '9000')

Greg could you solve this issue? I’m also experiencing it. With dockerized Selenium grid and dockerized web app.

The amOnPage() method works but see() method doesn’t. Here’s my acceptance suite yml settings:

actor: AcceptanceTester
modules:
    enabled:
        - \Helper\Acceptance
        - WebDriver
    config:
        WebDriver:
            url: http://app.mywebapp.com
            browser: chrome

Also adding my docker-compose.yml

version: '2'
services:
  seleniumhub:
    container_name: seleniumhub
    image: selenium/hub
    ports:
     - "4444:4444"

  chromenode:
    container_name: chromenode
    image: selenium/node-chrome-debug
    ports:
      - 4578
    links:
      - seleniumhub
    environment:
      - HUB_PORT_4444_TCP_ADDR=seleniumhub
      - HUB_PORT_4444_TCP_PORT=4444
    extra_hosts:
      - "app.mywebapp.com:172.20.129.2"
    volumes:
      - /dev/shm:/dev/shm

networks:
  default:
    external:
      name: build_static-network

[Selenium browser Logs]
SEVERE - http://app.mywebapp.com/ - Failed to load resource: net::ERR_CONNECTION_REFUSED

As @gintsgints mentioned, the standalone-chrome container you are using doesn’t know how to connect to your application, which is, according to your webdriver configuration http://localhost.

I stumbled into the same problem when I was trying to run web tests for my symfony application. I solved it by letting the test job figure out the IP address of its own container, start a local PHP server, and change the codeception configuration so that the chrome container connects to that IP address.

The relevant lines in .gitlab-ci.yaml are:

- LOCAL_IP=$(ip route get 8.8.8.8 | awk -F"src " 'NR==1{split($2,a," ");print a[1]}')
- echo Local IP $LOCAL_IP
# Run the php built in server. Don't try to run nginx as a service, that only causes troubles. ;-)
- php -S 0.0.0.0:8080 -t public/ 2> /dev/null &
- sed -i "s/localhost:8080/$LOCAL_IP:8080/" tests/acceptance.suite.yml

(Note that in my codeception configuration, the url was configured as http://localhost:8080, as opposed to http://localhost in your case.)

I wrote a blog post on this: gitlab-ci, codeception and selenium web tests.

This is probably not the most beautiful solution around, but it works. If you happen to find a prettier solution, please let me know, because I would love to improve what I’ve got.

2 Likes

Thanks for the amazing contribution to this thread, @johanv!