Docker is nothing but a virtual machine packed in containers, we use the docker to run our selenium Grid. Docker server as a Virtual machine with a specific product like Unit with Chrome or firefox.
Most of the Companies use the docker to create their own applications just like firefox inside a container. Docker is worth a thing for cross-browser testing, instead of paying more money to cloud-based companies, it is better to use the Docker.
I know there is too much explanation for Docker but this much is enough for our automation and for all.
FYI : I am using the Win10 operating system for this tutorial because I have only one laptop, silly.
Kitematic provides the UI for the Docker, When you download docker it will be like a cli/cmd terminal, so let's download it.
But sometimes this KiteMatic may not work during those time please do use the Docker cli or Command prompt to install things.
Sometimes, you might face issue with the installation of the image in Kitematic UI, and same issues you will face with Cli/Cmd as well
DockerCli.exe -SwitchDaemon
docker run -d -P --name selenium-hub selenium/hub
docker ps
Yes, you can install the node just like a hub but there is two kinds of nodes, which one to chose.
docker run -d -P --link selenium-hub:hub selenium/node-chrome-debug
[Hub.start] - Nodes should register to http://172.17.0.2:4444/grid/register/
[Hub.start] - Clients should connect to http://172.17.0.2:4444/wd/hub
After Connecting the Node
# after node installation
[Hub.start] - Nodes should register to http://172.17.0.2:4444/grid/register/
[Hub.start] - Clients should connect to http://172.17.0.2:4444/wd/hub
[DefaultGridRegistry.add] - Registered a node http://172.17.0.3:5555
As I said earlier, you may want to see the execution of the program in the given docker. You cannot view a docker just like that, we have to connect the docker with our system using some tool.
I would recommend using the VNC viewer to connect the Docker. VNC viewer helps to connect the Remote machine with Local machine.
// install the Hub
docker pull selenium/hub
// invoke the hub
docker run -d -P --name selenium-hub selenium/hub
// install the Node
docker pull selenium/node-chrome-debug
// invoke the node
docker run -d -P --link selenium-hub:hub selenium/node-chrome-debug
// list the running process
docker ps
// list all the running process
docker ps -a
Now let's come the important part of the tutorial, Let's integrate the selenium scripts with Docker. We have to remove our browser classes and we need to add remote webdriver
Consider below selenium Test file for docker integration, this script will navigate to https://chercher.tech and prints the title of the page. We have to get the Details of the hub configuration from the Selenium-hub in the Kitematic UI.
After Connecting the Node
# after node installation
[Hub.start] - Nodes should register to http://172.17.0.2:4444/grid/register/
[Hub.start] - Clients should connect to http://172.17.0.2:4444/wd/hub
[DefaultGridRegistry.add] - Registered a node http://172.17.0.3:5555
Lets configure the Remote webdriver in selenium just like our Selenium Grid along with desired capabilities
import java.net.URL;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.Test;
public class TestJenkins {
@Test
public void runTestOnDocker() throws Exception {
DesiredCapabilities dcap = DesiredCapabilities.chrome();
String driverPath = "D:\\\\Eclipse progs\\\\driverserver\\\\geckodriver.exe";
System.setProperty("webdriver.gecko.driver", driverPath);
// Hub Port at 4444
URL url = new URL("http://localhost:4444/wd/hub");
WebDriver driver = new RemoteWebDriver(url, dcap);
// Get URL
driver.get("https://chercher.tech/");
// Print Title
System.out.println(driver.getTitle());
driver.quit();
}
}
C:\Program Files\Docker\Docker>DockerCli.exe -SwitchDaemon
seleniumAddress:'http://localhost:4444/wd/hub',
Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application’s services. Then, with a single command, you create and start all the services from your configuration.
In layman terms, Compose contains a set of commands just like a batch file in your windows operating system.
services:
NameOfHub:
image: selenium/hub
container_name: NameOfHub
privileged: true
ports:
- 4444:4444
environment:
- GRID_TIMEOUT=240000
- GRID_BROWSER_TIMEOUT=240000
NameOfNode:
image: selenium/node-chrome-debug
privileged: true
depends_on:
- NameOfHub
ports:
- 5900
environment:
- HUB_PORT_4444_TCP_ADDR=NameOfHub
- HUB_PORT_4444_TCP_PORT=4444
I am Pavankumar, Having 8.5 years of experience currently working in Video/Live Analytics project.