Andris Mudiayi
Andris Mudiayi

Reputation: 469

Connection refused error with selenium running in docker

While trying nto dockerise selenium End2End tests using the selenium docker image 'selenium/standalone' i get the error : Error retrieving a new session from the selenium server Connection refused! Is selenium server started? ye selenium server starts up according to the console output..any ideas ?

FROM selenium/standalone-chrome
USER root
# installing node
RUN apt-get update
RUN apt-get install -y curl
RUN curl -sL https://deb.nodesource.com/setup_7.x | bash
RUN apt-get install -y nodejs  
RUN node -v
RUN npm -v
# Installing Yarn
#RUN rm -r /usr/local/bin/yarn
RUN npm install -g -y yarn
ENV PATH $PATH:/usr/local/bin/yarn
#copying files
WORKDIR /app
COPY . .
# debug
RUN ls -alh .
#installing yarn
RUN yarn install 
EXPOSE 4444
RUN yarn
CMD yarn test

Upvotes: 4

Views: 6224

Answers (4)

Makan dianka
Makan dianka

Reputation: 111

Try to set environnement of SE_ENABLE_TRACING to false in docke-compose.

exemple using commandline with docker run: -e SE_ENABLE_TRACING=false

Upvotes: 0

Muhammadyk
Muhammadyk

Reputation: 319

To effectively run your Selenium automation solution, I'd suggest you now make use of a Selenium grid that enables you to create a Selenium Hub and attach different nodes (browsers) to the grid. Below is an illustration of how the selenium grid works Selenium Grid

creating a lean Docker container. You will also require two files.

  • Dockerfile
  • docker-compose.yml

This is an updated version of your Dockerfile

   FROM node:20-slim
   USER root

   # installing node
   RUN apt-get update && apt-get install -y curl

   # Installing Yarn
   RUN npm install -g -y yarn
   ENV PATH $PATH:/usr/local/bin/yarn

   #copying files
   WORKDIR /usr/src/app
   COPY . .

   # debug
   #installing yarn
   RUN yarn install 
   RUN yarn
   CMD yarn test

***This is your docker-compose.yml ***

    version: "3.10"
services:

  selenium-hub:
    image: selenium/hub:latest
    container_name: selenium-hub
    ports:
      - "4444:4444"
      - "4442:4442"
      - "4443:4443"
    volumes:
      - mydata:/app/data
  


  ChromeService-docu:
    image: selenium/node-chrome:latest
    shm_size: "2gb"
    ports:
      - "5900"  
      - "7900"
    environment:
      - SE_EVENT_BUS_HOST=selenium-hub
      - SE_EVENT_BUS_PUBLISH_PORT=4442
      - SE_EVENT_BUS_SUBSCRIBE_PORT=4443
      - SE_NODE_MAX_SESSIONS=2
    depends_on:
      - selenium-hub
  
  docu-api:
    build: .
    ports:
      - "8000:8000"
    depends_on:
      - selenium-hub
    volumes:
      - mydata:/app/data

volumes:
  mydata:

with this it would ensure you setup the Selenium grid configuration properly and starts up a lean Docker resources. to run the container you can use the command below

docker-compose up --build

Upvotes: 1

Andris Mudiayi
Andris Mudiayi

Reputation: 469

i used a selenium/node-chrome image, but what resolved it was making sure my chromedriver + selenium server + nightwatch were set to the latest versions in my package.json

Upvotes: 0

Tarun Lalwani
Tarun Lalwani

Reputation: 146510

The problem is your approach of solving this. See you are inheriting your image from selenium/standalone-chrome which is supposed to run a Selenium browser. Now is this image you are adding your tests and specifying the CMD to run the tests.

When you build and launch this image, you don't get any browser because the CMD has been overridden by you to run the test. When we build in docker we keep dependent services in different containers. It is preferred to run 1 service/process per container in most case. In your case when the test is run the browser server process is missing, so that is the reason for connection refused.

So you need to be running two containers here. One for selenium/standalone-chrome and one for your test.

Also your image should inherit from node: and not from selenium chrome image. You should not have node -v and npm -v commands also while building images. They create extra layers in your final image

FROM node:7
USER root
# installing node
RUN apt-get update && apt-get install -y curl
# Installing Yarn
RUN npm install -g -y yarn
ENV PATH $PATH:/usr/local/bin/yarn
#copying files
WORKDIR /app
COPY . .
# debug
#installing yarn
RUN yarn install 
RUN yarn
CMD yarn test

Now you need to create a docker-compose file to run a composition which has both your test and chrome

version: '3'
services:
  chrome:
    image: selenium/standalone-chrome
  tests:
    build: .
    depends_on:
      - chrome

Install docker-compose and run docker-compose up command to run the above composition. Also in your tests make sure to use the URL as http://chrome:4444/wd/hub and use the Remote webdriver and not the local driver.

Upvotes: 5

Related Questions