Igor Šarčević
Igor Šarčević

Reputation: 3551

How to run cucumber/selenium tests in Docker?

I am struggling to run my cucumber tests from a Docker image.

Here is my setup:

  1. I use OSX with XQuartz to run an X11 session
  2. I use an Ubuntu 14 Vagrant image for development where I forward my X11 session
  3. I am trying to run a docker image with Firefox that will use my XQuartz session for display

So far, I managed to start Firefox with the following setup:

# Dockerfile
FROM ubuntu:14.04

RUN apt-get update && apt-get install -y firefox

# Replace 1000 with something appropriate ;)
RUN export uid=1000 gid=1000 && \
     mkdir -p /home/developer && \
     echo "developer:x:${uid}:${gid}:Developer,,,:/home/dev:/bin/bash" >> /etc/passwd && \
     echo "developer:x:${uid}:" >> /etc/group && \
     echo "developer ALL=(ALL) NOPASSWD: ALL" > /etc/sudoers.d/developer && \
     chmod 0440 /etc/sudoers.d/developer && \
     chown ${uid}:${gid} -R /home/developer

USER developer
ENV HOME /home/developer
CMD /usr/bin/firefox

I can start Firefox with --net=host from my Vagrant machine:

docker build -t firefox .
docker run --net=host -ti --rm -e DISPLAY=$DISPLAY -v $HOME/.Xauthority:/home/developer/.Xauthority -v /tmp/.X11-unix:/tmp/.X11-unix:rw firefox:latest

But this is not ideal because I can't link other containers to my machine in the docker-compose.yml file. Ideally, I would like to run my docker machine without --net=host like this:

docker build -t firefox .
docker run -ti --rm -e DISPLAY=$DISPLAY -v $HOME/.Xauthority:/home/developer/.Xauthority -v /tmp/.X11-unix:/tmp/.X11-unix:rw firefox:latest

But I get the following error:

error: XDG_RUNTIME_DIR not set in the environment.
Error: cannot open display: localhost:10.0

Please help :)

Upvotes: 0

Views: 3007

Answers (1)

Leo Gallucci
Leo Gallucci

Reputation: 16722

You could simply use elgalu/docker-selenium to avoid dealing with what's already solved for you, and maintained:

docker run --rm -ti --net=host --pid=host --name=grid \
   -e SELENIUM_HUB_PORT=4444 -e TZ="US/Pacific" \
   -v /dev/shm:/dev/shm --privileged elgalu/selenium

If you need advanced features like a dashboard with video recording for example, or live preview, you can use Zalenium and start it with:

curl -sSL https://raw.githubusercontent.com/dosel/t/i/p | bash -s start -i

Upvotes: 1

Related Questions