Commit 257e66b0 257e66b0cab0cd84fcbe4eeaa27a53e1cd6a4f63 by Nicolas Perriault

added more tests for the Mouse class

1 parent ba692c36
......@@ -52,30 +52,30 @@ var Mouse = function(casper) {
var processEvent = function(type, args) {
if (!utils.isString(type) || supportedEvents.indexOf(type) === -1) {
throw new Error('Unsupported mouse event type: ' + type);
throw new Error('Mouse.processEvent(): Unsupported mouse event type: ' + type);
}
args = Array.prototype.slice.call(args); // cast Arguments -> Array
casper.emit('mouse.' + type.replace('mouse', ''), args);
switch (args.length) {
case 0:
throw new Error('Too few arguments');
throw new Error('Mouse.processEvent(): Too few arguments');
case 1:
// selector
var selector = args[0];
if (!utils.isString(selector)) {
throw new Error('No valid CSS selector passed: ' + selector);
throw new Error('Mouse.processEvent(): No valid CSS selector passed: ' + selector);
}
casper.page.sendEvent.apply(casper.page, [type].concat(computeCenter(selector)));
break;
case 2:
// coordinates
if (!utils.isNumber(args[0]) || !utils.isNumber(args[1])) {
throw new Error('No valid coordinates passed');
throw new Error('Mouse.processEvent(): No valid coordinates passed: ' + args);
}
casper.page.sendEvent(type, args[0], args[1]);
break;
default:
throw new Error('Too many arguments');
throw new Error('Mouse.processEvent(): Too many arguments');
}
};
......
......@@ -16,15 +16,24 @@
test2: false,
test3: false,
test4: false,
test5: []
testdown: [],
testup: [],
testmove: [],
testclick: []
};
document.querySelector('#test4').onclick = function(event) {
results.test4 = true;
event.preventDefault();
};
window.onmousedown = function(e) {
results.test5 = [e.x, e.y];
}
window.onmousedown = function(event) {
results.testdown = [event.x, event.y];
};
window.onmouseup = function(event) {
results.testup = [event.x, event.y];
};
window.onmousemove = function(event) {
results.testmove = [event.x, event.y];
};
})(window);
</script>
</body>
......
......@@ -25,12 +25,22 @@
this.test.assert(results.test4, 'CasperUtils.click() has clicked an unobstrusive js handled link');
});
// mouse.down
// casper.mouse
casper.then(function() {
t.comment('Mouse.down()');
this.mouse.down(200, 100);
var results = this.getGlobal('results');
this.test.assertEquals(results.test5, [200, 100], 'Mouse.down() has clicked the specified position');
this.test.assertEquals(results.testdown, [200, 100], 'Mouse.down() has pressed button to the specified position');
t.comment('Mouse.up()');
this.mouse.up(200, 100);
results = this.getGlobal('results');
this.test.assertEquals(results.testup, [200, 100], 'Mouse.up() has released button to the specified position');
t.comment('Mouse.move()');
this.mouse.move(200, 100);
results = this.getGlobal('results');
this.test.assertEquals(results.testmove, [200, 100], 'Mouse.move() has moved to the specified position');
});
casper.run(function() {
......