Grunt task running Qunit tests using Saucelabs
A Grunt task for running qunit tests using Sauce Labs' Cloudified Browsers.
The Grunt Qunit task runs qunit based test suites on [PhantomJS]. This Grunt-Saucelabs-Qunit task is very similar but runs the test suites on the cloudified browser environment provided by Sauce Labs. This ensures that subject of the test runs across different browser environment. The task also uses Sauce Connect to establish a tunnel between Sauce Labs browsers and the machine running Grunt to load local pages. This is typically useful for testing pages on localhost that are not publically accessible on the internet.
This task is available as a node package and can be installed as
npm install grunt-saucelabs-qunit. It can also be included as a devDependency in package.json in your node project.
To use the task in
grunt.js, load the npmTask.
grunt.initConfig, add the configuration that looks like the following
'saucelabs-qunit':all:username: 'saucelabs-user-name'key: 'saucelabs-key'urls: 'array or URLs to to load for QUnit'tunnelTimeout: 'A numeric value indicating the time to wait before closing all tunnels 'testTimeout: 'Milliseconds to wait before timeout for qunit test per page'testname: 'Name of the test'tags: 'Array of tags'browsers:browserName: 'opera'// Called after a qunit unit is done, per page, per browser// Return true or false, passes or fails the test// Returning does not alter the test result// For async return, callvar done = thisasync;setTimeout// Return to this test after 1000 millisecondsdone/*true or false changes the test result, does not alter the result*/;1000;
The parameters are
- username : The saucelabs username that will be used to connect to the servers. Required
- key : The Saucelabs secret key. Since this is a secret, this should not be checked into the source code and may be available as an environment variable. Grunt can access this using
- urls: An array or URLs that will be loaded in the browsers, one after another. Since SauceConnect is used, these URLs can also be localhost URLs that are available using the
servertask from grunt. Required
- testname: The name of this test, displayed on the SauceLabs dashboard. Optional
- tags: An array of tags displayed for this test on the SauceLabs dashboard. This can be the build number, commit number, etc, that can be obtained from grunt.
- browsers: An array of objects representing the various browsers on which this test should run. Optional
- testTimeout : Number of milliseconds to wait for qunit tests on each page before timeout and failing the test
- onTestComplete : A callback that is called everytime a qunit test for a page is complete. Runs per page, per browser configuration. A true or false return value passes or fails the test, undefined return value does not alter the result of the test. For async results, call
this.async()in the function. The return of
this.async()is a function that should be called once the async action is completed.
test task running from Grunt could look like
grunt.registerTask('test', 'server qunit saucelabs-qunit'); This starts a server and then runs the Qunit tests first on PhantomJS and then using the Sauce Labs browsers.
Here is an example
grunt.js file - https://gist.github.com/4061787.
Grunt tasks are usually run alongside a continuous integration system. For example, when using Travis, adding the following lines in the package.json ensures that the task is installed with
npm install is run. Registering Saucelabs in test task using
grunt.registerTask('test', 'server saucelabs-qunit'); ensures that the CI environment runs the tests using
To secure the Sauce Key, the CI environment can be configured to provide the key as an environment variable instead of specifying it file. CI Environments like Travis provide ways to add secure variables in the initial configuration.
The IndexedDBShim is a project that uses this plugin in a CI environment. Look at the .travis.yml and the grunt.js for usage example.