Docker Image of your Testing Project / Dockerise test execution
Tags: automation, testing, docker, testautomation
Published: 2022-11-03 16:28:39


![Docker image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a76lhpumi6q4ormzt2y6.png)

---
I previously wrote about docker & testng, how it can be useful? but did you ever feel that why should we keep running tests in complex manner( on local machine/on virtual machine)? Can we make it simple enough for everyone to run tests with ease?!

What if we have a docker-image of our test code which when executed will test our app. you just have to do `docker run <image-name>` and that's it. your scripts shall trigger inside container and on completion you should be able to get test status on console!

Few advantages:
1. Anyone can execute scripts & get test results.
2. Development/Testing knowledge is not required.
3. Test execution is completely isolated.

---

Github repo: [Arvind142/TestNG-Modular-Automation-Framework](https://github.com/Arvind142/TestNG-Modular-Automation-Framework)

---

## Start with creating test suite, verifying POM!

### testng.xml
```
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite">
    <!-- Listeners to help with reporting -->
    <listeners>
        <!-- Listener Class declaration -->
    </listeners>
    <test name="Test">
        <packages>
            <!-- test package declaration -->
            <package name="com.core.test_package.*"></package>
        </packages>
    </test> <!-- Test -->
</suite> <!-- Suite -->
```

### Pom.xml

```
<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <suiteXmlFiles>
                        <suiteXmlFile>testng.xml</suiteXmlFile>
                    </suiteXmlFiles>
                </configuration>
            </plugin>
        </plugins>
    </pluginManagement>
</build>
```

### Trigger them to Verify it's working.
`mvn clean test -Dsurefire.suiteXmlFiles=/path/to/testng.xml`

---

## Create image of your code
### Dockerfile
```
# Base image
FROM maven:3.8.5-openjdk-11-slim
# Argument with default value
ARG suiteFile=testng.xml
# Copy code from local to image
COPY ./ /app
# Specify working directory
WORKDIR /app
# Execute command at creation of image
RUN mvn clean compile
# Command to be executed at start of container
CMD mvn test -Dsurefire.suiteXmlFiles=$suiteFile -e
```

### Build your image
`docker build -t arvindc142/framework:v1 .`


![Build SS](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qzkr27iqlv4t2nejbv1f.png)

---

## Run you image
`docker run arvindc142/framework:v1`

## Run different test suite (other than testng.xml, which was metioned as default suite)
`docker run -e suiteFile=testng-karate.xml arvindc142/framework:v1`

## Remember to publish image to image repo so that it is available for others to leverage.

`docker image push arvindc142/framework:v1`

---

## What about test reports?
since tests were executed in container then reports are within container, how can i get them? Volume Mapping

### Volume Mapping

Volumes are the preferred mechanism for persisting data generated by and used by Docker containers. While bind mounts are dependent on the directory structure and OS of the host machine, volumes are completely managed by Docker.

_Note:_ In my case reports are generated in test-output folder, I’ll map test-output of my image with my machine’s folder (i.e., test-results) so that I can get test result there! (**Use PowerShell on windows)**

`docker run -v $PWD/test-results:/app/test-output arvindc142/framework:v1`

_Before Snapshot:_

![Before SS](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fic2c09wlmo88ginyvnx.png)

_After executing docker run with -v option:_

![After ss](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qskumsyq1hepo5qomunp.png)


![RPT](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ue4hhtllovb0fyk1rv6t.png)


---
## BONUS!!!
Let’s add selenium grid with code for UI test script execution.

above setup were only for test execution it didn’t cover selenium-based UI script execution.

Note: Remote url will be http://selenium-hub:4444/wd/hub inside framework container.

Container can connect to hub container with host as selenium-hub, i’ve also exposed port 4444 to watch script execution from your machine. you can access hub with http://localhost:4444/ui

### Hub Snapshot:

![grid](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w5wylw180qcj8knybbna.png)

### Docker-compose for Selenium grid and our test script execution
```
version: '3.7'
# compose version details
services:
  #-SELENIUM GRID SETUP FOR AUTOMATION EXECUTION-
  selenium-hub:
    image: selenium/hub:latest
    container_name: selenium-hub
    ports:
      - 4444:4444
  chrome:
    image: selenium/node-chrome:latest
    shm_size: 2gb
    depends_on:
      - selenium-hub
    environment:
      - SE_EVENT_BUS_HOST=selenium-hub
      - SE_EVENT_BUS_PUBLISH_PORT=4442
      - SE_EVENT_BUS_SUBSCRIBE_PORT=4443
  edge:
    image: selenium/node-edge:latest
    shm_size: 2gb
    depends_on:
      - selenium-hub
    environment:
      - SE_EVENT_BUS_HOST=selenium-hub
      - SE_EVENT_BUS_PUBLISH_PORT=4442
      - SE_EVENT_BUS_SUBSCRIBE_PORT=4443
  firefox:
    image: selenium/node-firefox:latest
    shm_size: 2gb
    depends_on:
      - selenium-hub
    environment:
      - SE_EVENT_BUS_HOST=selenium-hub
      - SE_EVENT_BUS_PUBLISH_PORT=4442
      - SE_EVENT_BUS_SUBSCRIBE_PORT=4443
  #-SELENIUM GRID services ended-

  #-Framework image-
  # service name(container name)
  modular-automation-framework:
    # which container to use for building
    image: arvindc142/framework:v1
    # to copy project into container
    volumes:
      - './test-results/:/app/test-output/'
```
### To spin up all containers
`docker-compose up -d`


![spin up](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yhj4jsxp133oa5hiwepk.png)

## Get all containers
`docker ps -a`

## Print container logs on console
you can get container-id’s with docker ps command
`docker logs -f <container-id>`

## To stop & remove containers
`docker-compose down`

![Close DOwn](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i306o53q4wu5cv27pk5w.png)

---
## References

[Run TestNG from commandline](https://www.baeldung.com/testng-run-command-line)

[Github TestNG Project](https://github.com/Arvind142/TestNG-Modular-Automation-Framework)

[Volume Mapping](https://blog.yipl.com.np/volume-mapping-a-way-to-make-persistent-data-storage-in-docker-6a47c951a0e7)

[Docker-Selenium](https://github.com/SeleniumHQ/docker-selenium)

[docker hub arvindc142/framework](https://hub.docker.com/r/arvindc142/framework/tags)