Commit 961e052f 961e052f7852d98df3a781f2ef1625013a8c0880 by Nicolas Perriault

fixes #282 - remote scripts loading

A new `remoteScripts` casper option has been added in order to load
remote client scripts into the page DOM environment:

```javascript
var casper = require('casper').create({
    remoteScripts: ['http://code.jquery.com/jquery-1.8.3.min.js',
                    'http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.4.2/underscore-min.js']
});
```
1 parent 96e37bf1
......@@ -10,6 +10,7 @@ XXXX-XX-XX, v1.0.0
- fixed [#215](https://github.com/n1k0/casperjs/issues/215) - added a `--fail-fast` option to the `casper test` command, in order to terminate a test suite execution as soon as any failure is encountered
- fixed [#274](https://github.com/n1k0/casperjs/issues/274) - some headers couldn't be set
- fixed [#277](https://github.com/n1k0/casperjs/issues/277) - multiline support in `ClientUtils.echo()`
- fixed [#282](https://github.com/n1k0/casperjs/issues/282) - added support for remote client scripts loading with a new `remoteScripts` casper option
- added [`Tester.assertTextDoesntExist()`](http://casperjs.org/api.html#tester.assertTextDoesntExist)
- added `Tester.assertFalse()` as an alias of `Tester.assertNot()`
- added `page.resource.requested` and `page.resource.received` events
......
Subproject commit efdaeb9e092054de1b30fa093ec2cd2435a2d894
Subproject commit 1e59e47358b1b1f55227460183ff36180dfb245f
......
......@@ -110,6 +110,7 @@ var Casper = function Casper(options) {
localToRemoteUrlAccessEnabled: true,
userAgent: defaultUserAgent
},
remoteScripts: [],
stepTimeout: null,
timeout: null,
verbose: false,
......@@ -967,7 +968,7 @@ Casper.prototype.initErrorHandler = function initErrorHandler() {
};
/**
* Injects configured client scripts.
* Injects configured local client scripts.
*
* @return Casper
*/
......@@ -1021,6 +1022,29 @@ Casper.prototype.injectClientUtils = function injectClientUtils() {
};
/**
* Loads and include remote client scripts to current page.
*
* @return Casper
*/
Casper.prototype.includeRemoteScripts = function includeRemoteScripts() {
if (this.options.remoteScripts.length === 0) {
return this;
}
this.waitStart();
this.options.remoteScripts.forEach(function(scriptUrl, i) {
this.log(f("Loading remote script: %s", scriptUrl), "debug");
this.page.includeJs(scriptUrl, function() {
this.log(f("Remote script %s loaded", scriptUrl), "debug");
if (i === this.options.remoteScripts.length - 1) {
this.log("All remote scripts loaded.", "debug");
this.waitDone();
}
}.bind(this));
}.bind(this));
return this;
};
/**
* Logs a message.
*
* @param String message The message to log
......@@ -1228,8 +1252,7 @@ Casper.prototype.run = function run(onComplete, time) {
"use strict";
this.checkStarted();
if (!this.steps || this.steps.length < 1) {
this.log("No steps defined, aborting", "error");
return this;
throw new CasperError('No steps defined, aborting');
}
this.log(f("Running suite: %d step%s", this.steps.length, this.steps.length > 1 ? "s" : ""), "info");
this.emit('run.start');
......@@ -1321,8 +1344,7 @@ Casper.prototype.start = function start(location, then) {
if (utils.isObject(this.options.viewportSize)) {
this.page.viewportSize = this.options.viewportSize;
}
this.started = true;
this.emit('started');
// timeout handling
if (utils.isNumber(this.options.timeout) && this.options.timeout > 0) {
this.log(f("Execution timeout set to %dms", this.options.timeout), "info");
setTimeout(function _check(self) {
......@@ -1332,10 +1354,12 @@ Casper.prototype.start = function start(location, then) {
}
}, this.options.timeout, this);
}
this.started = true;
this.emit('started');
if (utils.isString(location) && location.length > 0) {
return this.thenOpen(location, utils.isFunction(then) ? then : this.createStep(function _step() {
this.log("start page is loaded", "debug");
}));
}, {skipLog: true}));
}
return this;
};
......@@ -1848,7 +1872,10 @@ function createPage(casper) {
casper.options.onLoadError.call(casper, casper, casper.requestUrl, status);
}
}
// local client scripts
casper.injectClientScripts();
// remote client scripts
casper.includeRemoteScripts();
// Client-side utils injection
casper.injectClientUtils();
// history
......
......@@ -28,7 +28,11 @@ service = server.listen(testServerPort, function(request, response) {
console.log(utils.format('Test server url not found: %s (file: %s)', request.url, pageFile), "warning");
response.write("404 - NOT FOUND");
} else {
response.statusCode = 200;
var headers = {};
if (/js$/.test(pageFile)) {
headers['Content-Type'] = "application/javascript";
}
response.writeHead(200, headers);
response.write(fs.read(pageFile));
}
response.close();
......
(function() {
var elem = document.createElement('div');
elem.setAttribute('id', 'include1');
elem.appendChild(document.createTextNode('include1'));
document.querySelector('body').appendChild(elem);
})();
(function() {
var elem = document.createElement('div');
elem.setAttribute('id', 'include2');
elem.appendChild(document.createTextNode('include2'));
document.querySelector('body').appendChild(elem);
})();
/*global casper*/
/*jshint strict:false*/
casper.options.remoteScripts = [
'includes/include1.js', // local includes are actually served
'includes/include2.js', // through the local test webserver
'http://code.jquery.com/jquery-1.8.3.min.js'
];
casper.start('tests/site/index.html', function() {
this.test.assertSelectorHasText('#include1', 'include1',
'Casper.includeRemoteScripts() includes a first remote script on start');
this.test.assertSelectorHasText('#include2', 'include2',
'Casper.includeRemoteScripts() includes a second remote script on start');
this.test.assertEval(function() {
return 'jQuery' in window;
}, 'Casper.includeRemoteScripts() includes a really remote file on first step');
});
casper.thenOpen('tests/site/form.html', function() {
this.test.assertSelectorHasText('#include1', 'include1',
'Casper.includeRemoteScripts() includes a first remote script on second step');
this.test.assertSelectorHasText('#include2', 'include2',
'Casper.includeRemoteScripts() includes a second remote script on second step');
this.test.assertEval(function() {
return 'jQuery' in window;
}, 'Casper.includeRemoteScripts() includes a really remote file on second step');
});
casper.run(function() {
this.options.remoteScripts = [];
this.test.done();
});