Search code examples
javadockerseleniumdocker-composejar

Run selenium tests in docker container from JAR file


I currently have a docker-compose file with 3 services:

  • selenium-hub
  • chrome-node
  • a container containing my selenium JUnit tests.

I want to selenium-test (w/ JUnit) my JAR file (containing these tests) with a command in the container's Dockerfile. I thought about using the selenium-server.jar but I can't seem to figure out what the exec command should be (in the Dockerfile).

Anyone can help me out?

My docker-compose.yml:

version: "3"
services:
  chrome:
    image: selenium/node-chrome:4.1.4-20220427
    shm_size: 2gb
    container_name: chrome
    depends_on:
      - selenium-hub
    environment:
      - SE_EVENT_BUS_HOST=selenium-hub
      - SE_EVENT_BUS_PUBLISH_PORT=4442
      - SE_EVENT_BUS_SUBSCRIBE_PORT=4443
  selenium-hub:
    image: selenium/hub:4.1.4-20220427
    container_name: selenium-hub
    ports:
      - "4442:4442"
      - "4443:4443"
      - "4444:4444"
  application:
    build: .
    container_name: application
    ports:
      - "4447:4447"
    depends_on:
      - chrome

And my Dockerfile:

#
# Build stage
#
FROM maven:3.6.0-jdk-11-slim AS build
COPY src .
COPY pom.xml .
RUN mvn -f pom.xml jar:test-jar

#
# Package stage
#
FROM openjdk:11-jre-slim
COPY --from=build target/playground-project-selenium-1.0-SNAPSHOT-tests.jar /usr/local/lib/demo.jar
ADD selenium-server.jar .
ENTRYPOINT exec java -jar selenium-server.jar --ext /usr/local/lib/demo.jar:selenium/node-chrome standalone --port 4447
EXPOSE 4447

Solution

  • After some sessions of trial-and-error, I've managed to answer my own question. I created a so-called Fat-JAR (using the maven assembly plugin) containing all tests, resources and dependencies and I used JUnit 4 with the org.junit.runner.JUnitCore class to run a test suite.

    Now, my selenium UI tests are fully running in a Docker container using selenium hub and chrome driver in separate containers.