Playwright + Selenium Grid (docker yaml) - error connect timeout

328 Views Asked by At

I am using the docker-compose from the docker site to spin up a selenium grid (below) - and the sample playwright tests which come with playwright. I can run the tests locally, but when I try to use the selenium grid I get:

Error: browserType.launch: WebSocket error: connect ETIMEDOUT 10.0.1.3:4444
=========================== logs ===========================
<selenium> connecting to http://localhost:4444/
<selenium> connected to sessionId=6d694b7555264036b595224d8f91e394
<selenium> using selenium v4
<selenium> retrieved endpoint ws://10.0.1.3:4444/session/6d694b7555264036b595224d8f91e394/se/cdp for sessionId=6d694b7555264036b595224d8f91e394
<ws connecting> ws://10.0.1.3:4444/session/6d694b7555264036b595224d8f91e394/se/cdp
<ws error> ws://10.0.1.3:4444/session/6d694b7555264036b595224d8f91e394/se/cdp error connect ETIMEDOUT 10.0.1.3:4444
<ws connect error> ws://10.0.1.3:4444/session/6d694b7555264036b595224d8f91e394/se/cdp connect ETIMEDOUT 10.0.1.3:4444
<ws disconnected> ws://10.0.1.3:4444/session/6d694b7555264036b595224d8f91e394/se/cdp code=1006 reason=
<selenium> disconnecting from sessionId=6d694b7555264036b595224d8f91e394
<selenium> disconnected from sessionId=6d694b7555264036b595224d8f91e394
============================================================

I have had this working with webdriverIO - though due to hardware failure I lost my notes.

I have tried this with both windows and macOS, both have a firewall. Do I need to open ports on the firewall? (Not a network guru)

Any other suggestions? I have started the grid as per the comments and can see the grid is up when I goto localhost:4444/ui

# To start Docker in Swarm mode, you need to run `docker swarm init`
# To deploy the Grid, `docker stack deploy -c docker-compose-v3-swarm.yml grid`
# Stop with `docker stack rm grid`
# Stop swarm mode `docker swarm leave --force`

version: "3.7"

services:
  chrome:
    image: selenium/node-chrome:4.15.0-20231110
    shm_size: 2gb
    environment:
      - SE_EVENT_BUS_HOST=selenium-hub
      - SE_EVENT_BUS_PUBLISH_PORT=4442
      - SE_EVENT_BUS_SUBSCRIBE_PORT=4443
    deploy:
      replicas: 1
    entrypoint: bash -c 'SE_OPTS="--host $$HOSTNAME" /opt/bin/entry_point.sh'

  edge:
    image: selenium/node-edge:4.15.0-20231110
    shm_size: 2gb
    environment:
      - SE_EVENT_BUS_HOST=selenium-hub
      - SE_EVENT_BUS_PUBLISH_PORT=4442
      - SE_EVENT_BUS_SUBSCRIBE_PORT=4443
    deploy:
      replicas: 1
    entrypoint: bash -c 'SE_OPTS="--host $$HOSTNAME" /opt/bin/entry_point.sh'

  firefox:
    image: selenium/node-firefox:4.15.0-20231110
    shm_size: 2gb
    environment:
      - SE_EVENT_BUS_HOST=selenium-hub
      - SE_EVENT_BUS_PUBLISH_PORT=4442
      - SE_EVENT_BUS_SUBSCRIBE_PORT=4443
    deploy:
      replicas: 1
    entrypoint: bash -c 'SE_OPTS="--host $$HOSTNAME" /opt/bin/entry_point.sh'

  selenium-hub:
    image: selenium/hub:4.15.0-20231110
    ports:
      - "4442:4442"
      - "4443:4443"
      - "4444:4444"

1

There are 1 best solutions below

4
On

the official documentation for playwright https://playwright.dev/docs/selenium-grid specifies how to run it.

Few things that your compose are missing:

  • SE_NODE_GRID_URL - this is required for proper communication in playwright.
  • SE_NODE_MAX_SESSIONS - its good to set it to 1, for UI tests to not have conflicts between tests

Here is an example working compose file. (Just put proper version of hub/nodes) You can find more about env variables from selenium in links below.

docker-compose.yml

version: "3.7"
services:
  selenium-hub:
    image: selenium/hub:4.15.0
    container_name: selenium-hub
    shm_size: "1g"
    ports:
      - "4442:4442"
      - "4443:4443"
      - "4444:4444"
    environment:
      SE_SESSION_REQUEST_TIMEOUT: 1800
    networks:
      - selenium
  chrome:
    image: selenium/node-chrome:119.0
    shm_size: "2g"
    ports:
      - "7900"
    depends_on:
      - selenium-hub
    environment:
      HUB_HOST: selenium-hub
      SE_EVENT_BUS_HOST: selenium-hub
      SE_EVENT_BUS_PUBLISH_PORT: 4442
      SE_EVENT_BUS_SUBSCRIBE_PORT: 4443
      SE_NODE_PORT: 4444
      SE_NODE_GRID_URL: http://localhost:4444/wd/hub
      SE_NODE_MAX_SESSIONS: 1
      SCREEN_WIDTH: 1024
      SCREEN_HEIGHT: 768
      SCREEN_DEPTH: 24
      DBUS_SESSION_BUS_ADDRESS: "/dev/null"
      SE_OPTS: "--session-timeout 900"
      JAVA_OPTS: "-Xmx2g"
    links:
      - selenium-hub:hub
    volumes:
      - "/dev/shm:/dev/shm"
    networks:
      - selenium
  firefox:
    image: selenium/node-firefox:119.0
    shm_size: "2g"
    ports:
      - "7900"
    depends_on:
      - selenium-hub
    environment:
      HUB_HOST: selenium-hub
      SE_EVENT_BUS_HOST: selenium-hub
      SE_EVENT_BUS_PUBLISH_PORT: 4442
      SE_EVENT_BUS_SUBSCRIBE_PORT: 4443
      SE_NODE_PORT: 4444
      SE_NODE_GRID_URL: http://localhost:4444/wd/hub
      SE_NODE_MAX_SESSIONS: 1
      SCREEN_WIDTH: 1024
      SCREEN_HEIGHT: 768
      SCREEN_DEPTH: 24
      DBUS_SESSION_BUS_ADDRESS: "/dev/null"
      SE_OPTS: "--session-timeout 900"
      JAVA_OPTS: "-Xmx2g"
    links:
      - selenium-hub:hub
    volumes:
      - "/dev/shm:/dev/shm"
    networks:
      - selenium
networks:
  selenium: