Commit 02d6f057 02d6f0578e3b2f81e60e82bcddbca56ebe8a1f11 by Nicolas Perriault

added Casper.userAgent() to ease a more dynamic setting of UA

1 parent ccf9afaa
Subproject commit c17e8c952e7405d3cf6f78cafb1539ad0b16c59d
Subproject commit c351edc343dfe441eb425b2357fbb2baf7d64f22
......
......@@ -37,6 +37,10 @@ var tester = require('tester');
var utils = require('utils');
var f = utils.format;
var defaultUserAgent = phantom.defaultPageSettings.userAgent
.replace('PhantomJS', f("CasperJS/%s", phantom.casperVersion) + '+Phantomjs');
exports.create = function create(options) {
return new Casper(options);
};
......@@ -65,8 +69,6 @@ exports.selectXPath = selectXPath;
* @param Object options Casper options
*/
var Casper = function Casper(options) {
var DEFAULT_DIE_MESSAGE = "Suite explicitely interrupted without any message given.";
var DEFAULT_USER_AGENT = "Mozilla/5.0 (Windows NT 6.0) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.112 Safari/535.1";
// init & checks
if (!(this instanceof arguments.callee)) {
return new Casper(options);
......@@ -91,7 +93,7 @@ var Casper = function Casper(options) {
page: null,
pageSettings: {
localToRemoteUrlAccessEnabled: true,
userAgent: DEFAULT_USER_AGENT
userAgent: defaultUserAgent
},
stepTimeout: null,
timeout: null,
......@@ -364,7 +366,9 @@ Casper.prototype.debugPage = function debugPage() {
Casper.prototype.die = function die(message, status) {
this.result.status = "error";
this.result.time = new Date().getTime() - this.startTime;
message = utils.isString(message) && message.length > 0 ? message : DEFAULT_DIE_MESSAGE;
if (!utils.isString(message) || !message.length) {
message = "Suite explicitely interrupted without any message given.";
}
this.log(message, "error");
this.emit('die', message, status);
if (utils.isFunction(this.options.onDie)) {
......@@ -1113,6 +1117,20 @@ Casper.prototype.thenOpenAndEvaluate = function thenOpenAndEvaluate(location, fn
};
/**
* Sets the user-agent string currently used when requesting urls.
*
* @param String userAgent User agent string
* @return String
*/
Casper.prototype.userAgent = function userAgent(agent) {
if (!this.started) {
throw new CasperError("Casper not started, can't set userAgent");
}
this.options.pageSettings.userAgent = this.page.settings.userAgent = agent;
return this;
};
/**
* Changes the current viewport size.
*
* @param Number width The viewport width, in pixels
......
function testUA(ua, match) {
casper.test.assertMatch(
ua, match, 'Default user agent matches ' + match
);
}
testUA(casper.options.pageSettings.userAgent, /CasperJS/);
casper.start().userAgent('plop').on('resource.requested', function(request) {
testUA(request.headers.filter(function(header) {
return header.name === "User-Agent";
}).pop().value, /plop/);
}).start('tests/site/index.html').run(function() {
this.test.done();
});