What is karma-phantomjs-launcher?
The karma-phantomjs-launcher is an npm package that allows you to use PhantomJS, a headless WebKit scriptable with a JavaScript API, as a browser for running tests with the Karma test runner. This is particularly useful for running tests in a continuous integration environment where you may not have access to a graphical browser.
What are karma-phantomjs-launcher's main functionalities?
Running Tests in a Headless Browser
This configuration file for Karma sets up PhantomJS as the browser for running tests. It uses the Jasmine framework and includes the necessary plugins for both Jasmine and PhantomJS.
module.exports = function(config) {
config.set({
browsers: ['PhantomJS'],
frameworks: ['jasmine'],
files: [
'src/**/*.js',
'test/**/*.spec.js'
],
plugins: [
'karma-jasmine',
'karma-phantomjs-launcher'
]
});
};
Continuous Integration Setup
This configuration is tailored for continuous integration environments. It uses the Mocha framework and sets the singleRun option to true, ensuring that Karma will run the tests once and then exit.
module.exports = function(config) {
config.set({
browsers: ['PhantomJS'],
frameworks: ['mocha'],
files: [
'src/**/*.js',
'test/**/*.spec.js'
],
plugins: [
'karma-mocha',
'karma-phantomjs-launcher'
],
singleRun: true
});
};
Other packages similar to karma-phantomjs-launcher
karma-chrome-launcher
The karma-chrome-launcher package allows you to use Google Chrome or Chromium as a browser for running tests with Karma. Unlike PhantomJS, Chrome provides a full browser environment, which can be useful for testing features that require a more complete browser implementation.
karma-firefox-launcher
The karma-firefox-launcher package enables you to use Mozilla Firefox as a browser for running tests with Karma. Similar to the Chrome launcher, it provides a full browser environment, which can be beneficial for testing compatibility with Firefox-specific features.
karma-safari-launcher
The karma-safari-launcher package allows you to use Safari as a browser for running tests with Karma. This is particularly useful for testing on macOS environments where Safari is the default browser.
karma-phantomjs-launcher
Launcher for PhantomJS.
Installation
The easiest way is to keep karma-phantomjs-launcher
as a devDependency in your package.json
,
by running
$ npm install --save-dev karma-phantomjs-launcher
Configuration
module.exports = function(config) {
config.set({
browsers: ['PhantomJS', 'PhantomJS_custom'],
customLaunchers: {
'PhantomJS_custom': {
base: 'PhantomJS',
options: {
windowName: 'my-window',
settings: {
webSecurityEnabled: false
},
},
flags: ['--load-images=true'],
debug: true
}
},
phantomjsLauncher: {
exitOnResourceError: true
}
})
}
The options
attribute allows you to initialize properties on
the phantomjs page
object, so
options: {
windowName: 'my-window',
settings: {
webSecurityEnabled: false
},
}
is equivalent to:
var webPage = require('webpage')
var page = webPage.create()
page.windowName = 'my-window'
page.settings.webSecurityEnabled = false
You can pass list of browsers as a CLI argument too:
$ karma start --browsers PhantomJS_custom
If you set the debug
option to true
, you will be instructed to launch a web browser to
bring up the debugger. Note that you will want to put debugger;
statements in your JavaScript
to hit breakpoints. You should be able to put breakpoints in both your test code and your client
code. Note that the debug
option automatically adds the --remote-debugger-port=9000
and
--remote-debugger-autorun=yes
switches to PhantomJS.
For more information on Karma see the homepage.