Have you gotten tired of Selenium breaking on every update on your Firefox? Certainly I have. For this blog post, I'll explain how to use Selenium with their official image in docker hub.

Pros:
  • Pin a browser to specific version.
  • Same testing environment on all operating system.
  • No more holding upgrade for your browser.
  • Easy scaling.
Cons:
  • Adds another stack of your learning(which can be a good thing depending on the person)

There are also other paid solutions available which are easier to use but comes with price. If you're okay with that then I highly recommend them as they don't need a lot of setup to do and they easily scale with your stack.

Requirements

  • Basic working knowledge of Docker.
  • A working Python environment with selenium installed, preferably inside a virtualenv.
  • Working knowledge of selenium and selenium grid.

Overview:

To use the image, we'll need to pull the image, run it and then point our tests to the browser running in the container

Let's do the first step in pulling our images that we need to run.

  • docker pull selenium-hub
  • docker pull selenium/node-firefox

Now lets run our image.

  • docker run -d -p 4444:4444 --name selenium-hub selenium/hub

This will run the image on the background and on port 4444 with a name of selenium-hub.

The selenium-hub doesn't have a browser yet, hence we need to connect a browser to it. Here is how you can do this.

  • docker run -d -p -p 5900:5900--link selenium-hub:hub selenium/node-firefox-debug

The --link selenium-hub:hub is very important to connect our firefox node to the main selenium hub.

Lets test it out

Here is quick snippet to test if our setup is running.

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

browser = webdriver.Remote(
   command_executor='http://127.0.0.1:4444/wd/hub',
   desired_capabilities=DesiredCapabilities.FIREFOX)

browser.get('https://www.python.org/')

assert 'Welcome' in browser.title

browser.quit()

If there's no output from the terminal means our setup is success.

VNC: Looking through the eyes. :P

Now depending which OS you are using, you can install most of the VNC client to connect to the container.

In my case I'm using a cross platform client called TigerVNC

So open the application and connect to this address: 127.0.0.1:5900

Now run your functional test, you should see something like this: