Automated GUI testing is a must for core buisness functionalities in an application. Running them frequently in an automated manner on Continuous Integration environment guarantees reliable results and gives confidence that system isn’t broken.
Test suite should be written in a way that does not require human attention, generates test report and is repetitive to allow investigation of results. That leads to the need of deploying tests on Continuous Integration server (e.g. Jenkins) and running them regurarly. Having Jenkins deployed on Linux without GUI you can’t run web browser in usual windowed mode – you need to use so called headless mode. Headless software is a software available to work without a Graphical User Interface.
The configuration is built upon following components:
- Protractor tests – regular test suites that are fired on usual configuration with GUI
- Grunt configuration to run protractor tests
- Firefox browser installed on Jenkins’ Linux Operating System (CentOS in my case)
- Xvfb installed on Jenkins’ host OS. Xvfb is a software that emulates GUI for non GUI systems
- Grunt plugins set to run Protractor tests in a non GUI environment
- Jenkins job configuration to run your job in a scheduled, regular manner
Find the configuration details below.
1. Protractor tests
These are the test suites you run on your regular development machine and need to run them on Jenkins.
2. Grunt configuration
My tests are fired from command line with grunt-protractor-runner plugin (https://www.npmjs.com/package/grunt-protractor-runner). Plugin provides the protractor: task and requires config file where you specify all the options: spec files (suites), your system URL (baseUrl), as well as any custom params (params) that you can refer in your specs or browser you run your test against. Check the configuration details and instructions on plugin page.
3. Firefox browser
You can use the browsers of your choice. In my case this is the firefox. On CentOS the installation command is:
yum install firefox
This is the key component of running tests in headless mode. Xvfb is a virtual display frame buffer for X – the display system used by Linux. It provides a fake display buffer for graphical programs. This way it allows any program to run headlessly.
To install Xvfb on CentOS execute:
yum install xorg-X11-server-Xvfb
Xvfb usage is simple. To open certain url in browser execute:
xvfb-run firefox https://looksok.wordpress.com
This way you make sure that xvfb is available on your PATH and it opens the firefox application in headless mode with an url as a param.
4. Grunt plugins
To tie it up all together I use Grunt with plugins that starts xvfb buffer: grunt-env (https://github.com/jsoverson/grunt-env) and grunt-shell (https://github.com/cri5ti/grunt-shell-spawn). Whole configuration is described here: https://gist.github.com/nwinkler/f0928740e7ae0e7477dd
The idea behind that config is to execute shell command that create a new display before test. After the test is done, the display is killed.
5. Jenkins job configuration
This is an usual part of the setup. Simply make a new job that will fire up your protractor tests on a schedule you define. In my case Jenkins executes the grunt task:
In most cases that’s all the Jenkins job does. If your your system is already built and running, that Jenkins config enough. You can just target your tests against your running instance.
In my case Jenkins task is more complex. First it checkouts the repository for newest code version, then builds artifacts, starts all system components, fires tests and kills system that it has started. And it all works as a charm :)