Skip to content

Instantly share code, notes, and snippets.

@nwinkler
Last active July 26, 2017 02:03
Show Gist options
  • Save nwinkler/f0928740e7ae0e7477dd to your computer and use it in GitHub Desktop.
Save nwinkler/f0928740e7ae0e7477dd to your computer and use it in GitHub Desktop.
Start Xvfb from Grunt

Running Xvfb from Grunt

This Gist shows how to start and stop Xvfb from a Grunt script as part of your build. This is useful if you want to run unit tests in a browser in a headless setting.

Prerequisites

  • Make sure that Xvfb is installed on your system, e.g. through your Linux distribution's package manager.
  • Make sure that the Xvfb executable is on your PATH.

Required Packages

Install the following packages:

npm install --save-dev grunt-env
npm install --save-dev grunt-shell-spawn

The first is required for setting the required DISPLAY environment variable, the second for starting/stopping Xvfb.

Gruntfile

In your Gruntfile, set up the configurations for the two plugins and create a task for starting Xvfb before running your tests, and then stopping it when your tests have run.

The below example Gruntfile.js contains a standard configuration for running the tests using Protractor. Adjust as necessary.

Xvfb will be started in asynchronous mode in order to keep it running while the tests are executed.

Running the tests

To run the tests without Xvfb, e.g. on your local machine, simply run

grunt protractor:run

To run the tests with Xvfb, e.g. on your headless build server, run the following instead:

grunt protractor-xvfb
module.exports = function (grunt) {
grunt.initConfig({
protractor: {
options: {
keepAlive: true,
configFile: "protractor.conf.js"
},
run: {}
},
shell: {
xvfb: {
command: 'Xvfb :99 -ac -screen 0 1600x1200x24',
options: {
async: true
}
}
},
env: {
xvfb: {
DISPLAY: ':99'
}
}
});
grunt.registerTask('protractor-xvfb', [
'shell:xvfb',
'env:xvfb',
'protractor:run',
'shell:xvfb:kill'
]);
}
@rfranca86
Copy link

Hi,

I'm trying to use grunt with protractor and xvfb, but when I used "grunt protractor-xvfb":

Running tasks: protractor-xvfb
Running "protractor-xvfb" task
Warning: Task "shell" not found. Use --force to continue.

My grunt version:
grunt-cli v0.1.13
grunt v0.4.5

My protractor version:
Version 2.1.0

I have Xvfb installed already

And I installed grunt-env and grunt-shell-spawn like you said.

Do you know what can be ?

@B8li
Copy link

B8li commented Jun 16, 2015

Hi,
Looks like you have wrong configuration in Gruntfile.js.
Can you paste your Gruntfile.js so I can give you some hint ?

@rfranca86
Copy link

My Gruntfile.js:

module.exports = function(grunt) {

grunt.initConfig({
    protractor: {
        options: {
            keepAlive: true,
            configFile: "protractor.conf.js"
        },
        run: {}
    },

    shell: {
        xvfb: {
            command: 'Xvfb :99 -ac -screen 0 1600x1200x24',
            options: {
                async: true
            }
        }
    },
    env: {
        xvfb: {
            DISPLAY: ':99'
        }
    }

});

grunt.registerTask('protractor-xvfb',[
    'shell:xvfb',
    'env:xvfb',
    'protractor:run',
    'shell:xvfb:kill'
    ]);

// assim funciona
grunt.registerTask('test', 'Test task.', function() {
grunt.log.writeln('Lorem ipsum dolor sit amet.');
});

}

@Zhang
Copy link

Zhang commented Jun 25, 2015

@rfranca86 - seems like your not loading your tasks
grunt.loadNpmTasks('grunt-shell-spawn');
grunt.loadNpmTasks('grunt-env');

@Zhang
Copy link

Zhang commented Jun 25, 2015

Thanks for this guide, super helpful when running headless chrome. I'm running into an issue where the Xvfb process isn't fully killed, and on each instance of the grunt protractor-xvfb task, a left-over process is occupying my display port. I was wondering if you had a solution for that

@subun123
Copy link

subun123 commented Aug 21, 2016

Please help. I am blocked here.

I am executing grunt protractor:run and getting an error as /Users/submishr/Desktop/16.6Testing/Explore/Dummy/node_modules/grunt-protractor-runner/node_modules/selenium-webdriver/error.js:27
super(opt_error);
^
WebDriverError: no such session

Browser is opening for only 3 secs and then getting closed and test is getting failed.

Below is my gulp file:-

module.exports = function (grunt) {

grunt.initConfig({
    protractor: {
        options: {
            keepAlive: true,
            //webdriverManagerUpdate: true,
            //nodeBin: 'node',
            configFile: "Conf/Conf.js"
        },
        run: {}

    },
    shell: {
        xvfb: {
            command: 'Xvfb :99 -ac -screen 0 1600x1200x24',
            options: {
                async: true
            }
        }
    },
    env: {
        xvfb: {
            DISPLAY: ':99'
        }
    }
});
grunt.loadNpmTasks('grunt-protractor-runner');
grunt.loadNpmTasks('grunt-shell-spawn');
grunt.loadNpmTasks('grunt-env');
grunt.loadNpmTasks('grunt-protractor-webdriver');
grunt.registerTask('protractor-chrome', ['protractor:chrome']);
grunt.registerTask('protractor-xvfb', [
    'shell:xvfb',
    'env:xvfb',
    'protractor:run',
    'shell:xvfb:kill'
]);
grunt.registerTask('test', 'Test task.', function() {
grunt.log.writeln('Lorem ipsum dolor sit amet.');
});

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment