I'm trying to run a phantom.js test using karma and grunt and I get:
PhantomJS 2.1.1 (Mac OS X 0.0.0) ERROR
Error: Module name "webpage" has not been loaded yet for context: _. Use require([])
http://requirejs.org/docs/errors.html#notloaded
at vendor/bower/requirejs/require.js:140
The karma config from the Gruntfile.js looks like this:
karma: {
options: {
basePath: "./",
singleRun: true,
captureTimeout: 7000,
autoWatch: true,
reporters: karmaOptions.reporters,
browsers: ["PhantomJS", "PhantomJS_custom"],
// you can define custom flags
customLaunchers: {
'PhantomJS_custom': {
base: 'PhantomJS',
options: {
windowName: 'my-window',
settings: {
webSecurityEnabled: false
},
},
flags: ['--load-images=true'],
debug: true
}
},
phantomjsLauncher: {
// Have phantomjs exit if a ResourceError is encountered (useful if karma exits without killing phantom)
exitOnResourceError: true
},
files: [ "node_modules/karma-jasmine-async/src/jasmine.async.js", "vendor/bower/requirejs/require.js", "vendor/bower/underscore/underscore.js", "node_modules/phantomjs-prebuilt/bin/phantom.js", "test/runner.js",
{
pattern: "<%= rootFolderName %>/**/*.*",
included: false
},
{
pattern: "test/specs/*.*",
included: false
},
{
pattern: "test/mocks/*.*",
included: false
},
{
pattern: "vendor/**/*.js",
included: false
},
{
pattern: "vendor/**/*.html",
included: false
},
{
pattern: "vendor/**/*.xml",
included: false
}
],
exclude: [
'dist/**',
'vendor/**/*Spec.js'
]
},
// This creates a server that will automatically run your tests when you
// save a file and display results in the terminal.
daemon: {
options: {
singleRun: false
}
},
// This is useful for running the tests just once.
run: {
options: {
singleRun: true
}
},
run_functional: {
options: {
windowName: 'my-window',
settings: {
webSecurityEnabled: false
}
}
}
[...]
grunt.registerTask("test", ["karma:run_functional"]);
The test.js looks like this:
console.log("starting");
var webPage = require('webpage');
var page = webPage.create();
page.windowName = 'my-window';
page.settings.webSecurityEnabled = false;
page.open('http://google.com', function(status) {
console.log("Status: " + status);
if(status === "success") {
debugger;
console.log("success");
}
phantom.exit();
});
Config.js file for configuring require.js looks like this:
require.config({
paths: {
"vendor": "vendor",
"node_modules": "node_modules",
"almond": "node_modules/almond/almond",
"underscore": "node_modules/lodash/dist/lodash.underscore",
"jquery": "node_modules/jquery/jquery",
"jquery-ui": "node_modules/jquery-ui-dist/jquery-ui",
"backbone": "node_modules/backbone/backbone",
"layoutmanager": "node_modules/layoutmanager/backbone.layoutmanager",
"backbone.subroute": "node_modules/backbone.subroute/backbone.subroute",
"bootstrap": "node_modules/bootstrap/dist/js/bootstrap",
"i18next": "node_modules/i18next-client/i18next.amd",
"text": "node_modules/requirejs-text/text",[...]
When I run grunt test from the command line I get the error above:
Error: Module name "webpage" has not been loaded yet for context: _. Use require([])
So the problem is that this webpage module that is part of phantom.js was not loaded by require.js. in test.js Where should I load it? in config.js? should I load the entire phantom.js or just this webpage module?
I did a refactor and my test.js is like this now:
var phantom = require('phantom');
phantom.create(function(ph){
ph.createPage(function(page) {
page.windowName = 'my-window';
page.settings.webSecurityEnabled = false;
page.open("http://www.google.com", function(status) {
page.render("google.pdf", function(){
console.log("page rendered");
ph.exit();
})
})
})
});
And now I get:
PhantomJS 2.1.1 (Mac OS X 0.0.0) ERROR Error: Module name "phantom" has not been loaded yet for context: _. Use >require([])
Require js should pick it up from config.js to which I appended:
"phantom": "node_modules/phantomjs-prebuilt/bin/phantom.js"
But for some reason require js doesn't see include phantom js. Why is that happening.