Have you ever gotten tired of Selenium breaking every time your Firefox is updated? Certainly I am. For this blog post, I’ll explain how to use Selenium with their official image in docker hub.

Pros

  • Pin a browser to a specific version
  • Same testing on all operating systems
  • 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 which are easier to use but come with a 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

1. Basic working knowledge of Docker
2. A working Python enviroment with selenium installed, preferably inside a virtualenv
3. 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-hubdocker pull selenium/node-firefox

Now lets run our images.

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

This will run the image in the background and on port 444 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.

Now we can test it out.

Here is a 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, then that means our setup is a success.

VNC: Looking through the eyes

Now depending which OS you are using, you can install most VNC clients 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:

Here you can create the content that will be used within the module.