Test Automation with Selenium and Docker – Framework Design
In the rapidly evolving world of software development, the need for efficient and reliable testing methodologies cannot be overstated. Test automation has become a cornerstone for Agile and DevOps practices, enabling teams to deliver high-quality software at speed. This blog post explores building a robust test automation framework with Selenium and Docker, addressing key challenges faced by test automation engineers today.
Why Use Docker in Test Automation?
Docker offers a lightweight, portable, and consistent environment for running applications, making it an ideal choice for test automation. Here’s why integrating Docker into your Selenium testing framework is advantageous:
Test Automation Framework with Selenium and Docker
Let’s create a test automation framework that leverages Docker for enhanced efficiency.
Prerequisites
Please ensure you have the following installed on your machine:
- Java (JDK 8 or higher)
- Maven
- Docker
- Docker Compose
- Git
Step 1: Set Up Your Project Structure
Create a new Maven project structure. Here’s a suggested layout:
/test-automation-framework
|-- /src
| |-- /main
| | `-- /java
| |-- /test
| | `-- /java
|-- /docker
| `-- Dockerfile
|-- pom.xml
Step 2: Create the Maven pom.xml File
The pom.xml file defines the dependencies required for the project. Below is a sample configuration with Selenium and TestNG to create Test Automation Framework with Selenium and Docker
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>test-automation-framework</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.14.0</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.4.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>5.4.1</version>
</dependency>
</dependencies>
</project>
Step 3: Create the Dockerfile
The Dockerfile will help you create a Docker image that contains all the necessary tools to run your Selenium tests. Here’s an example Dockerfile to create Test Automation Framework with Selenium and Docker
# Use the official Maven image as a base
FROM maven:3.8.6-openjdk-11 AS build
# Set the working directory
WORKDIR /app
# Copy the pom.xml and install dependencies
COPY pom.xml .
RUN mvn dependency:go-offline
# Copy the source code and package the application
COPY src ./src
RUN mvn clean package
# Use a lightweight image for runtime
FROM openjdk:11-jre-slim
WORKDIR /app
# Copy the jar file from the build stage
COPY --from=build /app/target/test-automation-framework-1.0-SNAPSHOT.jar app.jar
# Specify the command to run the application
ENTRYPOINT ["java", "-jar", "app.jar"]
Step 4: Write Sample Selenium Tests
In the src/test/java directory, create a sample Selenium test class:
package com.example.tests;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
public class SampleTest {
private WebDriver driver;
@BeforeClass
public void setUp() {
System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
driver = new ChromeDriver();
}
@Test
public void testGoogleSearch() {
driver.get("https://www.google.com");
// Add your assertions here
}
@AfterClass
public void tearDown() {
driver.quit();
}
}
Step 5: Docker Compose for Multi-Container Setup
Now, In order to run tests in parallel using multiple containers.
- create a docker-compose.yml file to create Test Automation Framework with Selenium and Docker
version: '3.8'
services:
selenium:
image: selenium/standalone-chrome
ports:
- "4444:4444"
test:
build: .
depends_on:
- selenium
environment:
- SELENIUM_HOST=selenium
Step 6: Run Your Tests in Docker
To build your Docker image and run the tests, execute the following commands:
1. Build the Docker image:
docker-compose build
2. Run the tests:
docker-compose up --abort-on-container-exit
By leveraging Selenium and Docker, you can create a robust test automation framework that enhances test reliability and execution speed while integrating seamlessly into your CI/CD pipeline. This approach not only resolves common testing issues but also fosters a culture of quality within your development process.
Next Steps
Follow this guide and you can create a cutting-edge test automation framework.