Prerequisites:
- Docker installed on your system.
- Basic understanding of Docker commands.
Step 1: Pull Selenium Standalone Chrome Container
The first step is to pull the Selenium standalone Chrome container from Docker Hub. This container includes a standalone instance of the Chrome browser along with the Selenium WebDriver.
docker pull selenium/standalone-chrome
This command will download the latest version of the Selenium standalone Chrome container to your local Docker environment.
Step 2: Run Selenium Container
Once the container is downloaded, you can start it using the following command:
docker run -d -p 4444:4444 --name selenium-container selenium/standalone-chrome
Explanation of Flags:
-d: Runs the container in detached mode (in the background).
-p 4444:4444: Maps port 4444 on the host to port 4444 on the container. This is where Selenium server listens for WebDriver requests.
--name selenium-container: Assigns the name "selenium-container" to the running container.
Step 3: Verify Selenium Container is Running
To verify that the Selenium container is running, execute the following command:
docker ps
You should see the selenium-container listed among the running containers.
Step 4: Write Selenium Tests
Now, you can write your Selenium tests using your preferred programming language (e.g., Java, Python). Ensure that your tests are configured to connect to the Selenium server running inside the Docker container.
Example Python Selenium Test:
from selenium import webdriver
# Connect to the Selenium server running inside the Docker container
driver = webdriver.Remote("http://localhost:4444/wd/hub", desired_capabilities={"browserName": "chrome"})
# Perform browser automation actions
driver.get("https://www.example.com")
print(driver.title)
# Close the browser
driver.quit()
Step 5: Run Selenium Tests
Execute your Selenium tests from your local environment. The tests will communicate with the Selenium server running inside the Docker container to perform browser automation actions.
Step 6: Stop and Remove the Selenium Container (Optional)
Once you're done with your testing, you can stop and remove the Selenium container using the following commands:
docker stop selenium-container
docker rm selenium-container
Conclusion:
By setting up a Docker environment with a standalone browser using a Selenium container, you can easily manage and execute browser automation tests in a controlled and reproducible manner. This approach allows for seamless integration of Selenium tests into your CI/CD pipelines and simplifies the setup process across different environments.
Top comments (0)