Site icon Vinsguru

Selenium WebDriver – How To Distribute Docker Images – Part 3

Overview:

The modern cloud infrastructure, continuous integration & deployment processes etc have completely changed the way how applications are deployed in production nowadays. In order to release new features faster in Production, you need to reduce time we take in the each phase of the SDLC. As an automation lead/architect, It could be your responsibility to have a proper infrastructure to speed up the automated test execution! When it comes to infrastructure automation, Docker plays a major role there!

Testautomationguru already has few docker and selenium related articles which talks about setting up the dockerized selenium grid & running a docker container with a test.

  1. Setting up Dockerized Selenium grid.
  2. Running Automated Tests Inside A Docker Container – Part 1
  3. Selenium WebDriver – How To Run Multiple Test Suites Using Docker Compose – Part 2

Before continuing this article, I would request you to read the above 3 articles first! I am explaining few things here assuming that you have already read them.

Docker Registry:

Docker registry is simply a storage place of all the docker images through which you could share image with others. Docker registry helps us to version our images with specific tags. So, multiple versions of images could be maintained in Docker Registry and any specific version can be pulled from the registry and used.

Docker Hub:

Docker Hub is a public could based registry maintained by docker inc. You need to create an account in docker-hub to push your images. Public images are free. Private images require some fee which is not very significant!

Maven Docker Plugin:

Updating with credentials:

In order to push the docker image to Docker Hub – you need to provide the docker hub credentials as shown here.

<plugin>
    <groupId>com.spotify</groupId>
    <artifactId>dockerfile-maven-plugin</artifactId>
    <version>1.4.0</version>
    <executions>
        <execution>
            <id>default</id>
            <goals>
                <goal>build</goal>
                <goal>push</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <username>username</username>
        <password>password</password>               
        <repository>vinsdocker/containertest</repository>
        <tag>demo</tag>
    </configuration>
</plugin>

If you are not comfortable in placing the credentials in the pom file, you could place that in the  ~/.m2/settings.xml. 

<servers>
   <server>
      <id>docker.io</id>
      <username>username</username>
      <password>password</password>
   </server>
</servers>

Usually this set up should be done on the Jenkins slave node which is building the image.

Building and Pushing an Image:

Issuing following command builds the image with our selenium page objects and tests with all the dependencies.  Once the image is built, it is pushed to the docker hub.

mvn clean package dockerfile:push -DskipTests
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building container-test 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- dockerfile-maven-plugin:1.4.0:push (default-cli) @ container-test ---
[INFO] The push refers to a repository [docker.io/vinsdocker/containertest]
[INFO] Image 52bc4d47e0e5: Preparing
[INFO] Image fa5377bafc12: Preparing
[INFO] Image c43becf4cc27: Preparing
[INFO] Image a784c47d5471: Preparing
[INFO] Image d1ec1b1ff3b5: Waiting
[INFO] Image 6df67175164b: Waiting
[INFO] Image 3358360aedad: Waiting
[INFO] Image fa5377bafc12: Pushing
[INFO] Image 52bc4d47e0e5: Pushing
[INFO] Image 3358360aedad: Pushed
[INFO] Image d1ec1b1ff3b5: Pushed
[INFO] demo: digest: sha256:1dc1326cb3892c9d00202c22daa557a677cdaf2e21468b114cbe85e766ddf47b size: 2409
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 01:18 min
[INFO] Finished at: 2018-04-14T12:09:02-05:00
[INFO] Final Memory: 26M/336M
[INFO] ------------------------------------------------------------------------

You could immediately see your image in the docker-hub.

 

Now anyone can pull this image directly run the test (assuming you have selenium grid up and running & provided the ip)

sudo docker run -e SELENIUM_HUB=10.11.12.13 -e MODULE=search-module.xml -e BROWSER=chrome vinsdocker/containertest:demo

Lets try to automate our test automation build process completely.

Integrating GitHub & Jenkins:

 

That’s it. Now whenever you make a code change and push it to GitHub, it triggers a Jenkins build immediately.  The Jenkins automatically builds and pushes an image to docker-hub.  Once the image is pushed to dockerhub, any machine with docker will able to run your tests!!

 

 

Summary:

By integrating GitHub/BitBucket, Jenkins/any CI tool and DockerHub/other Registry, we are completely able to automate our test automation build process. This way, you can easily run your automated tests on any machine (assuming docker installed) without worrying about any other dependency.  This is the way to go if you plan for running yours in the cloud. All the test automation infrastructure – selenium grid and nodes to run all your modules are dockerized. So you could start instances in EC2, run the tests and terminate as and when you need!  By doing this, test automation not only saves testing effort and but also the infrastructure maintenance cost.

 

Happy Testing & Subscribe 🙂

 

Share This:

Exit mobile version