Run your Selenium tests inside docker container - Part 1
Tags: selenium, docker, java, seleniumwebdriver
Published: 2022-08-27 14:10:00

![Selenium & Docker in Windows
](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w1q9s6z6hif36oyxvukg.png)
 

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
```

Next (And thanks to Selenium team for sharing this Dockerfile and this [post ](https://stackoverflow.com/questions/65813715/how-to-apt-get-install-firefox-on-an-openjdk11-docker-base-image-without-no-in/65833662#65833662)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
```

You can refer to this [Dockerfile ](https://github.com/sunim2022/Jenkins_Docker/blob/9b55a490d3d83590a3eed3b064d73397a42d9de1/selenium-in-docker/Dockerfile)for more details.
I created one sample/simple Google-search [project ](https://github.com/sunim2022/Jenkins_Docker/tree/main/selenium-in-docker)to test my final Dockerfile.
Created the image successfully (after 22 attempts) :).

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

![Docker build logs](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lwlj4183zwg9o6q98lkj.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cbdaxuo4ry36xarzvf8h.png)

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

![Docker image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o1o85h82ez074lik4isa.png)

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](https://medium.com/@suni.mothadikala/run-your-selenium-tests-inside-docker-container-part-1-9dfab15d1c86)

_**[Run your Selenium tests inside docker container (inside Jenkins container) - DIND, Part 2 …..](https://dev.to/sunim2022/run-your-selenium-tests-inside-docker-container-in-jenkins-part-2-2iek)**_