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. 

  • If settings.xml is not present in the above location, then copy from MVN_HOME/conf/settings.xml and place it inside the .m2 directory.
  • Then add the credentials as shown here.
<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.

Screenshot from 2018-04-14 12-37-07

 

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:

  • First crate an empty repo in GitHub

Screenshot from 2018-04-14 12-52-03

  • Push your maven project for automated tests into this repo. Copy the URL to clone this repo.

Screenshot from 2018-04-14 12-55-09

  • Create a Jenkins job to build an image from this repo & use the above copied URL.

Screenshot from 2018-04-14 09-00-26

  • Ensure that Jenkins can build an image successfully on the slave/node.  Add the dockerhub credentials in the settings,xml on this node. Ensure that you are able to push the image to docker hub directly.

Screenshot from 2018-04-14 09-10-28

  • Go to Manage Jenkins / Configure System to create a webhook for GitHub integration (You must be Jenkins admin to do that). Copy this URL

Screenshot from 2018-04-14 09-20-14

 

  • Go to GitHub. Go to Settings for the project repo. Add the Webhook url you had copied.

Screenshot from 2018-04-14 09-20-34

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!!

 

Screenshot from 2018-04-14 13-14-06

 

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:

14 thoughts on “Selenium WebDriver – How To Distribute Docker Images – Part 3

  1. It says dockerfile is null. I would suggest you to clone the given project in github and try if it works first. then on top of that make your changes

    1. Thanks Vlns. Given project works perfectly post cloning. But when I tried push docker images to my hub credentials, I am getting above error. I have just updated my docker userID and password. Help me where I need to pass dockerfile. details.

  2. Thanks Vlns. Given project works perfectly post cloning. But when I tried push docker images to my hub credentials, I am getting above error. I have just updated my docker userID and password. Do you know do I need to put ant docker file path in pom or any other location

    1. Possible. I am not sure if somebody has already created a docker image with IE on windows OS. If it is not there, You can also connect an existing windows machine with IE with your docker selenium grid.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.