Commit 8999c3b0 8999c3b0b4ac62c50fdaaf35e9d45ed0f01ef29d by Nicolas Perriault

introducing the 'mouse' module

1 parent 988307f6
......@@ -87,6 +87,7 @@ var Casper = function(options) {
warning: 'COMMENT',
error: 'ERROR'
};
this.mouse = require('mouse').create(this);
this.options = utils.mergeObjects(this.defaults, options);
this.page = null;
this.pendingWait = false;
......@@ -521,10 +522,10 @@ Casper.prototype = {
throw new Error("No element matching selector found: " + selector);
}
var clipRect = this.evaluate(function(selector) {
return __utils__.getElementBounds();
return __utils__.getElementBounds(selector);
}, { selector: selector });
if (!utils.isClipRect(clipRect)) {
this.log('Could not fetch boundaries for element matching selector: ' + selector, "error");
throw new Error('Could not fetch boundaries for element matching selector: ' + selector);
}
return clipRect;
},
......@@ -611,12 +612,7 @@ Casper.prototype = {
* @return Casper
*/
mouseClick: function(selector) {
var bounds = this.getElementBounds(selector);
if (utils.isClipRect(bounds)) {
var x = bounds.left + Math.floor(bounds.width / 2);
var y = bounds.top + Math.floor(bounds.height / 2);
this.page.sendEvent('click', x, y);
}
this.mouse.click(selector);
return this;
},
......
var utils = require('utils');
exports.create = function(casper) {
return new Mouse(casper);
};
var Mouse = function(casper) {
if (!utils.isCasperObject(casper)) {
throw new Error('Mouse() needs a Casper instance');
}
var supportedEvents = ['mouseup', 'mousedown', 'click', 'mousemove'];
var computeCenter = function(selector) {
var bounds = casper.getElementBounds(selector);
if (utils.isClipRect(bounds)) {
var x = Math.round(bounds.left + bounds.width / 2);
var y = Math.round(bounds.top + bounds.height / 2);
return [x, y];
}
};
var processEvent = function(type, args) {
if (!utils.isString(type) || supportedEvents.indexOf(type) === -1) {
throw new Error('Unsupported mouse event type: ' + type);
}
args = Array.prototype.slice.call(args); // cast Arguments -> Array
switch (args.length) {
case 0:
throw new Error('Too few arguments');
case 1:
// selector
var selector = args[0];
if (!utils.isString(selector)) {
throw new Error('No valid CSS selector passed: ' + selector);
}
casper.page.sendEvent.apply(casper.page, [type].concat(computeCenter(selector)))
break;
case 2:
// coordinates
if (!utils.isNumber(args[1]) || !utils.isNumber(args[2])) {
throw new Error('No valid coordinates passed');
}
casper.page.sendEvent(type, args[0], args[1])
break;
default:
throw new Error('Too many arguments');
}
};
this.click = function() {
processEvent('click', arguments);
},
this.down = function() {
processEvent('mousedown', arguments);
},
this.move = function() {
processEvent('mousemove', arguments);
},
this.up = function() {
processEvent('mouseup', arguments);
}
};
exports.Mouse = Mouse;
......@@ -137,7 +137,7 @@ exports.isObject = isObject;
function isString(value) {
return isType(value, "string");
}
exports.isFunction = isFunction;
exports.isString = isString;
/**
* Shorthands for checking if a value is of the given type. Can check for
......
(function(t) {
casper.start('tests/site/index.html', function(self) {
self.mouseClick('a[href="test.html"]');
});
casper.then(function(self) {
t.comment('Casper.mouseClick()');
t.assertTitle('CasperJS test target', 'Casper.mouseClick() can click on a link');
});
// onclick variants tests
casper.thenOpen('tests/site/click.html', function(self) {
t.comment('Casper.mouseClick()');
self.mouseClick('#test1');
self.mouseClick('#test2');
self.mouseClick('#test3');
self.mouseClick('#test4');
var results = self.getGlobal('results');
self.test.assert(results.test1, 'Casper.mouseClick() has clicked an `href="javascript:` link');
self.test.assert(results.test2, 'Casper.mouseClick() has clicked an `href="#"` link');
self.test.assert(results.test3, 'Casper.mouseClick() has clicked an `onclick=".*; return false"` link');
self.test.assert(results.test4, 'Casper.mouseClick() has clicked an unobstrusive js handled link');
});
casper.run(function(self) {
t.done();
});
})(casper.test);
\ No newline at end of file