DEV Community

sunim2022
sunim2022

Posted on

Run your Selenium tests inside docker container - Part 1

Selenium & Docker in Windows<br>

I started my career in manual testing with a pharmaceutical company. Outside of my application testing activities, I used to help my product team with deployments in test environments. Testing and deployments, both were manual. I always wanted to automate testing and reduce the number of hours I spent on executing multiple steps to deploy the product in test regions.
Selenium was the only automation framework I was aware of back in 2012, and so I kick-started my test automation journey with Selenium and Java last year.
I constantly had to deal with different versions and types of browsers. After researching a bit online, realized that docker could help simplify my local testing (had no clue what docker was until this point). It was time to learn docker and obviously it had to start with "docker run hello-world" :). I installed docker-desktop.


After watching countless videos on YouTube, only things I dreamed about for a week were virtual machines, containers and docker!
Went from hello-world to building my own image (not really huh !?) with Chrome & Firefox dependencies.
To run Selenium tests, I have always used
mvn test

So maven had to be the base image.
FROM maven:3.8.6-jdk-11

Added following dependencies to work with Chrome and its Driver.

# Install Chrome.
RUN wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add -
RUN sh -c 'echo "deb http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google-chrome.list'
RUN apt-get update
RUN apt-get install -y google-chrome-stable
# Install ChromeDriver.
RUN wget -N https://chromedriver.storage.googleapis.com/105.0.5195.19/chromedriver_linux64.zip -P ~/
RUN unzip ~/chromedriver_linux64.zip -d ~/
RUN rm ~/chromedriver_linux64.zip
RUN mv -f ~/chromedriver /usr/local/bin/chromedriver
RUN chmod +x /usr/local/bin/chromedriver
Enter fullscreen mode Exit fullscreen mode

Next (And thanks to Selenium team for sharing this Dockerfile and this post on stackoverflow) I was able to add Firefox dependencies to my Dockerfile.

# Install Firefox
ARG FIREFOX_VERSION=103.0.2
RUN echo "deb http://deb.debian.org/debian/ unstable main contrib non-free" >> /etc/apt/sources.list.d/debian.list
RUN apt-get update -qqy \
&& apt-get -qqy --no-install-recommends install firefox \
&& rm -rf /var/lib/apt/lists/* /var/cache/apt/* \
&& wget --no-verbose -O /tmp/firefox.tar.bz2 https://download-installer.cdn.mozilla.net/pub/firefox/releases/$FIREFOX_VERSION/linux-x86_64/en-US/firefox-$FIREFOX_VERSION.tar.bz2 \
&& apt-get -y purge firefox \
&& rm -rf /opt/firefox \
&& tar -C /opt -xjf /tmp/firefox.tar.bz2 \
&& rm /tmp/firefox.tar.bz2 \
&& mv /opt/firefox /opt/firefox-$FIREFOX_VERSION \
&& ln -fs /opt/firefox-$FIREFOX_VERSION/firefox /usr/bin/firefox
# Install GeckoDriver
ARG GECKODRIVER_VERSION=0.31.0
RUN wget --no-verbose -O /tmp/geckodriver.tar.gz https://github.com/mozilla/geckodriver/releases/download/v$GECKODRIVER_VERSION/geckodriver-v$GECKODRIVER_VERSION-linux64.tar.gz \
&& rm -rf /opt/geckodriver \
&& tar -C /opt -zxf /tmp/geckodriver.tar.gz \
&& rm /tmp/geckodriver.tar.gz \
&& mv /opt/geckodriver /opt/geckodriver-$GECKODRIVER_VERSION \
&& chmod 755 /opt/geckodriver-$GECKODRIVER_VERSION \
&& ln -fs /opt/geckodriver-$GECKODRIVER_VERSION /usr/bin/geckodriver
Enter fullscreen mode Exit fullscreen mode

You can refer to this Dockerfile for more details.
I created one sample/simple Google-search project to test my final Dockerfile.
Created the image successfully (after 22 attempts) :).

docker build -t seleniumtests-in-docker:1.0 .

Docker build logs

Time to create (my favorite) container.
docker run - rm - env "env_browser_param=Chrome" seleniumtests-in-docker:1.0

I am using environment variable "env_browser_param" to select between Chrome, Edge and Firefox browsers.

Finally, was excited to see the "Build Success" message from inside the container.

Selenium tests status

I am sure the image size can be reduced, may be for next time.

Docker image

Now I can launch multiple containers to test my app in multiple browsers. To test with a specific browser version, just change the corresponding ARG(s) in the Dockerfile, create a new image and start the container.
After learning so much about docker and its use cases, I now understand how application developers can use Docker to simplify product deployments.


I appreciate you taking time to read my "First" post.
Next: Docker adventure continues…

This blog was previously published on medium

Run your Selenium tests inside docker container (inside Jenkins container) - DIND, Part 2 …..

Top comments (0)