Run Selenium Test on Docker Container [ 2024 ]
Run Selenium Test on Docker Container
In this tutorial, we will learn the steps involved to run a Selenium Test on a Docker container. A Docker container image is a lightweight, standalone package of software that includes everything needed to run an application. Docker Image becomes Docker Container on the runtime when run on the Docker Engine.
Docker Hub
Open a web browser and Navigate to the Docker Hub.
We can find Docker images on the Docker Hub. Docker Hub is an image library. It’s like a repository of the Docker images.
Search Docker Hub for Selenium image. There would be several images in the search results. Each one is used for a specific purpose. Choose selenium/standalone-chrome Docker image to run a test on Google Chrome Browser.
Alternatively, to run a test on the Mozilla Firefox browser, choose the image: selenium/standalone-firefox. In this tutorial, we will use Chrome image. We will run a simple Selenium test on the Google Chrome browser
Pull the Docker Image
$ docker pull selenium/standalone-chrome
We can use the Docker images command to check the images on the local repository.
$ docker images
Start Docker Container
Run the following command:
$ docker run -d -P selenium/standalone-chrome
Check the container details with docker ps list container utility.
$ docker ps
Sample Selenium Test
The sample test will launch Mantis bug tracker URL and just print the Title of the login page on the console. Use a RemoteWebDriver instance to test against the Grid on the docker container. Since the test and the container runs on the same machine, we have used
http://localhost:49153/wd/hub
Listening port might vary. 49153 is the listening port on my machine. The docker ps command output would have the details to verify.
We will use the Chrome browser in Selenium Test. Instantiate ChromeOptions object and pass it to the RemoteWebDriver instance. ChromeOptions is used to set capabilities and customization of Chrome browser in Selenium.
ChromeOptions options = new ChromeOptions();
URL remote = new URL(“http://localhost:49153/wd/hub”);
WebDriver driver = new RemoteWebDriver(remote, options);
Code Snippet
import org.testng.annotations.Test; import java.net.URL; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeOptions; import org.openqa.selenium.remote.RemoteWebDriver; public class SeleniumOnDockerTest { @Test public void MantisTestOnDocker() throws Exception { ChromeOptions options = new ChromeOptions(); URL remoteURL = new URL("http://localhost:49153/wd/hub"); WebDriver driver = new RemoteWebDriver(remoteURL, options); driver.get("https://www.TestingDocs.com/mantis"); System.out.println(driver.getTitle() + " \nwww.TestingDocs.com -Selenium Tutorials"); System.out.println("Test was run on Docker container. Check the Docker Logs!"); } }
Sample Output
Check the Grid logs in the container. Any errors can be debugged using the logs.
Docker Logs
This step might vary on the Docker setup. On Windows, the easiest way is to launch the Docker Desktop application, select the container and click on LOGS tab to views the container logs.
That’s it. We have successfully run a selenium Test on Docker.
Related
In this tutorial, you will run a test using a single Container. We can scale this to distributed testing using Selenium Grid and multiple containers. To learn more:
Selenium Grid with Docker
https://www.testingdocs.com/selenium-grid-setup-with-docker-containers/
—
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: