• +43 660 1453541
  • contact@germaniumhq.com

Getting Started with Germanium III


Getting Started with Germanium III

The 3rd part of our 3 part series in which we’ll explore how to get started in using germanium. We’ll assume the only thing you have is python and a browser, and we’ll go from writing the first test, to integrating the test suite in a CI/CD system, in our case Jenkins.

This series is broken down into:

  1. Installation and the first test

  2. Test structure with behave

  3. Integration in CI/CD (this article)

Integration in CI/CD

This is the last part of our series on how to get started with Germanium.

In this last entry we will see how to integrate our test running into our Jenkins pipeline.

Fortunately that is quite easy.

Run Tests in a Container

First we need to create a container that can execute our tests. We could just have a python installed on the Jenkins, and have germanium as a dependency, but that’s just a bad idea. We don’t want our build system to have an invisible dependency, and we want it self contained into the Jenkinsfile and Dockerfile.

Our Dockerfile is then simply:

FROM python:3.6
RUN pip install behave germanium

We will save this file as Dockerfile.test.

To run it we will need to just call inside the Jenkinsfile:

docker.build('our_test_container', '-f Dockerfile.test .')
      .inside {
    sh """
        behave
    """
}

If you would try to run the tests now the tests would fail, since they would try to open chrome inside the container. In order to fix that we need to have a Selenium grid available.

Using The Selenium Grid

When running the tests in the container we need to have a Selenium Grid. The easiest way to have one installed, is to use the instructions from https://germaniumhq.com/get and use docker to create one.

That would set up a hub with a node that can spin up to 10 chrome and firefox browsers simultaneously.

The tests need just a slight change. Instead of doing a regular:

open_browser("chrome")

We will change to using potentially the remote webdriver hub. This way we can still run them locally, but when they run on Jenkins, will have injected the hub URL.

test_browser = os.environ.get('TEST_BROWSER', 'chrome')  # we pass a default
open_browser(test_browser)

Thus on the Jenkinsfile we can just set it:

docker.build('our_test_container', '-f Dockerfile.test .')
      .inside {
    sh """
        export TEST_BROWSER="chrome:http://hub.ip.address:4444/wd/hub"
        behave
    """
}

At this stage we already have full integration tests, that would fail the pipeline in case one of the tests would fail.

Let’s integrate the test reporting.

Use JUnit Reporting

Next we need to output from the tests into a format that Jenkins can consume. Out of the box Jenkins supports JUnit xml reports. In order to do that we will tell behave to output JUnit files inside our Jenkinsfile.

For that we only need to tell behave to output JUnit reports:

behave --junit

While the outputs are on the node, we also need to import them into the build in order to have them visible in the project and build dashboards.

This is again very simple. We can just run the JUnit pipeline step that will do the archival of the reports for the current build. Simply add the following into the Jenkinsfile.

stage('Integration tests') {
    node {
        deleteDir()
        checkout scm

        docker.build('our_test_container', '-f Dockerfile.test .')
              .inside {
            try {
                sh """
                    export TEST_BROWSER="chrome:http://hub.ip.address:4444/wd/hub"
                    behave --junit
                """
            } finally {
                junit 'reports/*.xml'
            }
        }
    }
}

If you notice there is a try/finally around the sh script. The reason for that is that sh will throw an exception when a program returns an exit code different than 0. Since when tests fail behave will return a non-zero exit code, we make sure we’re archiving the test results.

That’s it.

We have now a pipeline that depends only on the selenium hub and docker, fully containerized.

Conclusions

In this article we learned how to:

  1. Create a Jenkins testing pipeline that is fully containerized

  2. Install a Selenium grid.

  3. Get to run our tests headlessly

  4. Integrate the Jenkins JUnit reporting