Selenium Grid & Docker container Implementation

Selenium Grid & Docker container Implementation
  • KiwiQAKiwiQA
  • May 12, 2016

Share blog

What is Docker?

Docker is a lightweight container (read: a scaled-down VM) that provides a fast and programmatic way to run distributed applications. Selenium Grid is distributed system of nodes for running tests. Instead of running your grid across multiple machines or VMs, you can run them all on a single large machine using Docker. The two are practically destined for each other.

What is Selenium Grid?

Selenium Grid is a great way to speed up your tests by running them in parallel on multiple machines. However, rolling your own grid also means maintaining it. Setting up the right browser / OS combinations across many virtual machines (or – even worse – physical machines) and making sure each is running the Selenium Server correctly is a huge pain.

 

Also Read: What Do You Need to Know About Selenium Testing?

 

How to run selenium grid with Docker:

You can check at below link to see information.

  1. https://docs.docker.com/compose/install/
  2. https://docs.docker.com/installation/
  • Here’s an overview of what we’re going to do:
    1. Create Docker images for your Selenium Grid hub and node(s)
    2. Add Java to the hub to run the Selenium server jar
    3. Add Java, plus Firefox and Xvfb to the node
    4. Create a docker-compose.yml file to define how the images will interact with each other
    5. Start docker-compose and scale out to as many nodes as you need – or that your machine can handlet

Create a base image for your selenium server and the hub

let’s make a base Dockerfile we can use for both:

FROM ubuntu

ENV VERSION 2.44.0

RUN apt-get update -qqy 
&& apt-get -qqy --no-install-recommends install 
software-properties-common 
&& rm -rf /var/lib/apt/lists/*
RUN add-apt-repository -y ppa:webupd8team/java

RUN echo debconf shared/accepted-oracle-license-v1-1 select true | debconf-set-selections
RUN echo debconf shared/accepted-oracle-license-v1-1 seen true | debconf-set-selections

RUN apt-get update -qqy 
&& apt-get -qqy --no-install-recommends install 
oracle-java7-installer 
&& rm -rf /var/lib/apt/lists/*

RUN wget http://selenium-release.storage.googleapis.com/${VERSION%.*}/selenium-server-standalone-${VERSION}.jar

Let’s Build and Tag it

docker build -t selenium/base .

The Grid is basically a single hub connected to any number of different nodes. So, our hub image should look something like this:

FROM selenium/base

EXPOSE 4444

ADD start_grid.sh /var/start_grid.sh
RUN chmod 755 /var/start_grid.sh

CMD ["/bin/bash", "/var/start_grid.sh"]

And in the same directory as the Dockerfile, your run.sh script:

java -jar selenium-server-standalone-${VERSION}.jar -role hub

Install specific version of Firefox

we need a single Dockerfile for all of our nodes. In this example, every node will be a copy of this Dockerfile. It follows the same format as above – start from our selenium/base image and install necessary apps: Firefox, Xvfb, etc. However, there’s one catch here. we need to install a specific version of Firefox. specifying a specific version of Firefox in our Dockerfile is a good rule of thumb:

FROM selenium/base

ENV FIREFOX_MINOR 34.0.5

RUN apt-get update -qqy 
&& apt-get -qqy --no-install-recommends install 
firefox 
xvfb 
bzip2 
&& rm -rf /var/lib/apt/lists/*

RUN [ -e /usr/bin/firefox ] && rm /usr/bin/firefox
ADD https://ftp.mozilla.org/pub/mozilla.org/firefox/releases/${FIREFOX_MINOR}/linux-x86_64/en-US/firefox-${FIREFOX_MINOR}.tar.bz2 /tmp/
RUN apt-get install -q -y libdbus-glib-1-2
RUN tar -xvjf /tmp/firefox-${FIREFOX_MINOR}.tar.bz2 -C /opt/
RUN chmod -R +x /opt/firefox/
RUN ln -s /opt/firefox/firefox /usr/bin/firefox

ADD register-node.sh /var/register-node.sh
RUN chmod 755 /var/register-node.sh

CMD ["/bin/bash", "/var/register-node.sh"]

And our register-node.sh script:

#!/bin/bash
xvfb-run --server-args=":99.0 -screen 0 2000x2000x16 -ac" 
java -jar selenium-server-standalone-${VERSION}.jar 
-role node 
-hub http://$HUB_1_PORT_4444_TCP_ADDR:4444/grid/register 
-browser browserName=firefox

Define Your selenium Grid network with Docker compose

Once we have these files created, let’s make a docker-compose.yml file in the same directory as our selenium_grid folder. The contents of the file should be as follows:

hub:
  image: selenium_grid/hub
  ports:
    - "4444:4444"
firefox:
  image: selenium_grid/firefox
  links:
    - hub
  expose:
    - "5555"

Docker compose-Up

From here, all we have to do is start it up:

#!/bin/bash
docker-compose up -d
docker-compose scale firefox=5

You should now have a Selenium Grid consisting of one hub and five Firefox nodes. (You can see them running here: http://<boot2docker_ip>:4444/grid/console)

Docker compose-Up & down

Want to scale your grid up or down? It’s easy: just type docker-compose scale firefox=20. Too much? Scale it back down size with docker-compose scale firefox=10.

Let’s stop everything. And for good measure, let’s completely remove the images too:

docker-compose stop
docker-compose rm

From here, there are a lot of possibilities for setting up your Grid and integrating it with other Continuous Integration systems.

Article Original Source: https://www.conductor.com/nightlight/running-selenium-grid-using-docker-compose/

Stay updated with our newsletter

Subscribe to our newsletter for some hand-picked insights and trends! Join our community and be the first to know about what's exciting in software testing.

Our Blogs

(Re)discover the QA & software testing world with our blogs

Welcome to the testing tales that explore the depths of software quality assurance. Find valuable insights, industry trends, and best practices for professionals and enthusiasts.

Reflecting on 2024: Heartfelt Thank You from the CEO
Latest Blog. January 1, 2025

Reflecting on 2024: Heartfelt Thank You from the CEO

Dear Employees, Partners, Customers, and Stakeholders, As we bid farewell to another remarkable year and prepare to embrace new opportunities, I want to take a moment to extend my heartfelt gratitude to everyone who has contributed to KiwiQA’s success. To Our Employees: The Heart and Soul of KiwiQA Your passion, dedication, and resilience are the […]

Read More
Safeguarding Sensitive Business Data: A Comprehensive Guide To Key Security Testing Types
Latest Blog. December 23, 2024

Safeguarding Sensitive Business Data: A Comprehensive Guide To Key Security Testing Types

As digital spaces are enlarging their scopes and opportunities, they are also becoming vulnerable to the malicious effects of cyber threats. More fraudsters are surfacing over the internet to accomplish their evil tactics. It includes breaching over the financial gain of a layman or acquiring all the data of a leading industry. So, it is […]

Read More
Elevate Your E-commerce Testing with Automation: A Complete Guide Using K-FAST and Enginuity
Latest Blog. December 17, 2024

Elevate Your E-commerce Testing with Automation: A Complete Guide Using K-FAST and Enginuity

Are you a passionate entrepreneur who started your venture online to groove all over the world? Then, the world of e-commerce must be fascinating for you. To showcase your innovative products, you must have initiated an online store where the targeted audiences will be able to scroll over your page. So, is the interface of […]

Read More
Top Mobile Automation Testing Tools for 2024: Boost App Performance
Latest Blog. November 14, 2024

Top Mobile Automation Testing Tools for 2024: Boost App Performance

Mobile application testing has an important place in the ecosystem of digital application systems today. Mobile phones and tablets are everywhere, and people are more inclined to use mobile apps than other applications and software. Mobile apps were touted to generate more than $932 billion in revenue by 2023, and this year, this revenue has […]

Read More

Get in touch

Let’s accomplish (in)credible projects together.

Fill out and submit the form below, we will get back to you with a plan.

Don’t hesitate, mate. SAY HELLO

ISO Certifications

CRN: 22318-Q15-001
CRN:22318-ISN-001
CRN:22318-IST-001