I want to use docker
to run selenium
tests within a jenkins
instance. To test this, I am creating a docker image with the following Dockerfile
:
FROM python:2.7-slim
WORKDIR /selenium
ADD . /selenium
# Install any needed packages specified in requirements.txt
RUN pip install --trusted-host pypi.python.org -r requirements.txt
# Install some basic stuff
RUN apt-get update -qqy
RUN apt-get install -y wget xvfb bzip2 zip unzip
# Install all the chrome libraries...
RUN apt-get install -y gconf-service libasound2 libatk1.0-0 libcairo2 libcups2 libfontconfig1 libgdk-pixbuf2.0-0 libgtk-3-0 libnspr4 libpango-1.0-0 libxss1 fonts-liberation libappindicator1 libnss3 lsb-release xdg-utils
# Set firefox version and installation directory through environment variables.
ENV FIREFOX_VERSION 45.0
ENV FIREFOX_DIR /usr/bin/firefox
ENV FIREFOX_FILENAME $FIREFOX_DIR/firefox.tar.bz2
# Download the firefox of specified version from Mozilla and untar it.
RUN mkdir -p $FIREFOX_DIR
RUN wget -q --continue --output-document $FIREFOX_FILENAME "https://ftp.mozilla.org/pub/firefox/releases/${FIREFOX_VERSION}/linux-x86_64/en-US/firefox-${FIREFOX_VERSION}.tar.bz2"
RUN tar -xaf "$FIREFOX_FILENAME" --strip-components=1 --directory "$FIREFOX_DIR"
# Prepend firefox dir to PATH
ENV PATH $FIREFOX_DIR:$PATH
# Install Chrome
ENV LOCALEXE /usr/local/bin
ENV CHROMEDRIVERZIP=chromedriver_linux64.zip
ENV CHROMEDRIVER=chromedriver
RUN wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
RUN dpkg -i google-chrome-stable_current_amd64.deb; apt-get -fy install
RUN wget "http://chromedriver.storage.googleapis.com/2.33/${CHROMEDRIVERZIP}"
RUN unzip ${CHROMEDRIVERZIP}
RUN mv ${CHROMEDRIVER} ${LOCALEXE}
# Prepare the output directories
RUN mkdir -p /selenium/out
RUN ln -sf /dev/stdout /selenium/out/out.log
RUN ln -sf /dev/stderr /selenium/out/err.log
RUN Xvfb :99 -ac -screen 0 1280x1024x16 &
ENV DISPLAY :99
# Run app.py when the container launches
CMD ["nosetests", "-s", "test1.py"]
and in the actual test file (test1.py
) I also have the lines
from pyvirtualdisplay import Display
display = Display(visible=0, size=(800, 800))
display.start()
before I create the selenium webdriver, based on suggestions from here and here and here. However, after having created the docker image and running it with the following commands
docker run --rm selenium1
docker run --rm -e DISPLAY=$DISPLAY selenium1
docker run --rm -e DISPLAY=$DISPLAY -v /tmp/.X11-unix:/tmp/.X11-unix selenium1
I always get the following error:
Traceback (most recent call last):
File "/usr/local/lib/python2.7/site-packages/nose/case.py", line 197, in runTest
self.test(*self.arg)
File "/selenium/test1.py", line 43, in test_fac
driver = webdriver.Chrome(chrome_options=chrome_options)
File "/usr/local/lib/python2.7/site-packages/selenium/webdriver/chrome/webdriver.py", line 69, in __init__
desired_capabilities=desired_capabilities)
File "/usr/local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 151, in __init__
self.start_session(desired_capabilities, browser_profile)
File "/usr/local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 240, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "/usr/local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 308, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python2.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 194, in check_response
raise exception_class(message, screen, stacktrace)
WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally
(Driver info: chromedriver=2.33.506092 (733a02544d189eeb751fe0d7ddca79a0ee28cce4),platform=Linux 4.10.0-37-generic x86_64)
How to fix it? How to make sure I can use chrome
(and firefox
) for selenium tests in a docker container?
Versions:
- docker
- selenium-3.7.0
- nose-1.3.7
- pyvirtualdisplay-0.2.1
- xvfb 2:1.16.4-1+deb8u2
- chromedriver=2.33.506092
- Linux 4.10.0-37-generic x86_64
- google-chrome: 62.0.3202.89-1