Selenium docker integration

What is Docker?

A Docker is similar to a virtual machine, which consists of containers. By using these containers, we will be able to create, deploy, and run the applications. Indeed they are called Docker containers. Well, you may ask, what is a container? Simple, a Docker container allows you to package up an application with all the necessary parts it needs, such as libraries, other dependencies, etc. Lastly, it ships them out as one single package. Let’s now look at some pre-requisites below.

Pre-requisites:

  • Install Docker in the local machine.
  • JDK 1.7 or above.
  • Install all the browsers on the local machine.
  • Selenium WebDriver + JAVA + TestNG test cases.
  • VNC viewer – for running the web browsers.
  • If you are running on a Windows machine, you need to have Windows 10 OS because Docker supports only in Windows 10.
  • If you are running a MAC machine, then you need to have Apple Mac OS Yosemite 10.10.3 or above.

Steps to be followed when installing docker:

  • Once you download the binaries, install the docker immediately.
  • Turn ON the virtualization in the local machine.
  • Open the Docker and click on the checkbox & expose Daemon on TCP://localhost. It allows you to make yourself vulnerable to remote execution attacks.

Once you install the Docker, open the command prompt and type.

Docker –info – this will give all the information about the installed docker.

To run the Selenium WebDriver scripts with Docker, you need to download the following images and add them to the docker container.

  • Selenium/hub – which acts as a Selenium hub
  • Selenium/node-firefox – To access the firefox browser
  • Selenium/node-chrome – To access the chrome browser
  • Selenium/node-firefox-debug – To access the firefox browser via the port
  • Selenium/node-chrome-debug – To access the Chrome browser via the port

Smarter Test Management For QA Teams

Free up to 300 Test Cases. No credit card required

Sign up for free

Commands to download the images:

  • docker pull selenium/hub: Command for selenium hub
  • docker pull selenium/node-firefox : Command for selenium/node-firefox
  • docker pull selenium/node-chrome : Command for selenium/node-chrome
  • docker pull selenium/node-firefox-debug: Command for selenium/node-firefox-debug:
  • docker pull selenium/node-chrome-debug : Command for selenium/node-chrome-debug

Once the image download is complete, you need to start the Selenium Hub and run the below command:

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

After running this command, the browser navigates to your IP address, followed by the port number and /grid/console. So the command will be…

http://ipaddress:4444/grid/console

You will see the console running on the URL.

Then you need to start the chrome or Firefox nodes. For starting the nodes, run the following commands from the command prompt.

docker run -d –link selenium-hub:hub selenium/node-chrome

docker run -d –link selenium-hub:hub selenium/node-firefox

Then you need the start the chrome or Firefox debug. For that, run the below commands.

docker run –d –P –link selenium-hub:hub selenium/node-chrome-debug

docker run –d –P –link selenium-hub:hub selenium/node-firefox-debug

Then run the command docker ps -a – This command will list all the docker containers installed in the system and running on the port number.

container

Then refresh the link http://192.168.1.1:4444/grid/console. There you can find the chrome or Firefox nodes opening under the Selenium Hub.

Setting up the browsers:

  • Run the VNC viewer.
  • Click on the new connection.
  • In the VNC server field, enter the hub URL and the chrome or Firefox browser port number. The port number for each browser will be visible in the command prompt.port numbervnc viewer
  • Then click on the “OK” button.
  • It will prompt and ask for the password. By default, the password is “secret.” Then click on “CONNECT.” Then the browser will open.

Configuring Selenium Tests:

Once all the setup is complete, we need to connect our selenium scripts with the selenium hub.

For connecting to the hub add the below line of snippet in your Selenium script.

driver = new RemoteWebDriver (new URL(“http://your ip address:portnumber/wd/hub”), capabilities);

Sample Script:

package docker;

import java.net.MalformedURLException;

import java.net.URL;

import java.sql.Driver;

import org.openqa.selenium.Platform;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

import org.openqa.selenium.remote.DesiredCapabilities;

import org.openqa.selenium.remote.RemoteWebDriver;

import org.testng.annotations.Test;

public class NewTest {

 @Test

 public void dockerimp() throws MalformedURLException {

 DesiredCapabilities capabilities = DesiredCapabilities.firefox();

 capabilities.setCapability(“version”, “”);

 capabilities.setPlatform(Platform.WINDOWS);

 RemoteWebDriver driver = new RemoteWebDriver(new URL(“http://ipaddress:4444/wd/hub”),capabilities);

 driver.get(“https://localhost.com”);

 System.out.println(driver.getTitle());

 }

 }

Once the script is updated, run it, then you will see the VNC viewer, open the respective browser, and run the test.

We hope the procedure turned out to be useful. A ton of know-how articles like this are down the pipeline, and stay subscribed to get notified about them.

References:
http://www.seleniumeasy.com/selenium-tutorials/configure-selenium-grid-using-docker
http://pragmatictestlabs.com/2016/10/13/selenium-grid-docker/
https://docker-curriculum.com/

QATouch
Prasanna Venkatesh

Prasanna Venkatesh is a Senior Quality Analyst. He enjoys the experience of successfully delivering Quality Products and Services. He is passionate about everything in QA and takes ownership of every task.

Leave a Reply