Selenium Grid Setup with Docker Containers [ 2024 ]
Selenium Grid Setup with Docker Containers
In this tutorial, we will learn the steps for running Selenium Grid Setup with Docker Containers, an app containerization platform.
Let’s get started to set up the grid.
Pull Docker Images
Navigate to the https://hub.docker.com URL and search for Selenium Hub.
We need to pull docker images from the Docker Repository Hub to get started with the Selenium grid setup.
Selenium Hub
Pull the Selenium Hub docker image.
\> docker pull selenium/hub
Selenium Nodes
Pull some Selenium Node docker images. In this example, we will use two nodes.
- selenium/node-firefox
- selenium/node-chrome
Pull the selenium nodes from the remote repository.
\> docker pull selenium/node-firefox
\> docker pull selenium/node-chrome
We can check the images using the docker local repository command.
\> docker images
Start Docker Containers
Start Selenium Hub
\> docker run -d -p 4444:4444 -P –name selenium-hub selenium/hub
Start Nodes
Start nodes and connect them with the Selenium Hub.
docker run -d –link selenium-hub:hub -P –name chrome selenium/node-chrome
docker run -d –link selenium-hub:hub -P –name firefox selenium/node-firefox
Verify Grid Console
Open the Browser and navigate to the Grid console URL.
http://localhost:4444/grid/console
Sample Test
Run a sample test against the Grid Hub URL. The test just opens an URL and prints the title of the Web page. If we run multiple tests the hub would distribute the tests on the nodes.
package com.testingdocs.docker.demo; import org.testng.annotations.Test; import java.net.URL; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeOptions; import org.openqa.selenium.firefox.FirefoxOptions; import org.openqa.selenium.remote.RemoteWebDriver; public class SeleniumGridOnDockerTest { @Test public void FBTestTestOnDocker() throws Exception { FirefoxOptions fOptions = new FirefoxOptions(); URL remote = new URL("http://localhost:4444/wd/hub"); WebDriver driver = new RemoteWebDriver(remote, fOptions); driver.get("https://www.facebook.com"); System.out.println(driver.getTitle() + " \nwww.TestingDocs.com -Selenium Tutorials"); System.out.println("Selenium Grid using Docker container." + "Check the Docker Logs for more information!"); } }
Sample Output
Execute the test and verify the Docker container logs.
Selenium Tutorials
Selenium WebDriver Tutorials on this website can be found at:
https://www.testingdocs.com/selenium-webdriver-tutorial
For more details on the Selenium, visit the official website at: