Commit 8999c3b0 8999c3b0b4ac62c50fdaaf35e9d45ed0f01ef29d by Nicolas Perriault

introducing the 'mouse' module

1 parent 988307f6
...@@ -87,6 +87,7 @@ var Casper = function(options) { ...@@ -87,6 +87,7 @@ var Casper = function(options) {
87 warning: 'COMMENT', 87 warning: 'COMMENT',
88 error: 'ERROR' 88 error: 'ERROR'
89 }; 89 };
90 this.mouse = require('mouse').create(this);
90 this.options = utils.mergeObjects(this.defaults, options); 91 this.options = utils.mergeObjects(this.defaults, options);
91 this.page = null; 92 this.page = null;
92 this.pendingWait = false; 93 this.pendingWait = false;
...@@ -521,10 +522,10 @@ Casper.prototype = { ...@@ -521,10 +522,10 @@ Casper.prototype = {
521 throw new Error("No element matching selector found: " + selector); 522 throw new Error("No element matching selector found: " + selector);
522 } 523 }
523 var clipRect = this.evaluate(function(selector) { 524 var clipRect = this.evaluate(function(selector) {
524 return __utils__.getElementBounds(); 525 return __utils__.getElementBounds(selector);
525 }, { selector: selector }); 526 }, { selector: selector });
526 if (!utils.isClipRect(clipRect)) { 527 if (!utils.isClipRect(clipRect)) {
527 this.log('Could not fetch boundaries for element matching selector: ' + selector, "error"); 528 throw new Error('Could not fetch boundaries for element matching selector: ' + selector);
528 } 529 }
529 return clipRect; 530 return clipRect;
530 }, 531 },
...@@ -611,12 +612,7 @@ Casper.prototype = { ...@@ -611,12 +612,7 @@ Casper.prototype = {
611 * @return Casper 612 * @return Casper
612 */ 613 */
613 mouseClick: function(selector) { 614 mouseClick: function(selector) {
614 var bounds = this.getElementBounds(selector); 615 this.mouse.click(selector);
615 if (utils.isClipRect(bounds)) {
616 var x = bounds.left + Math.floor(bounds.width / 2);
617 var y = bounds.top + Math.floor(bounds.height / 2);
618 this.page.sendEvent('click', x, y);
619 }
620 return this; 616 return this;
621 }, 617 },
622 618
......
1 var utils = require('utils');
2
3 exports.create = function(casper) {
4 return new Mouse(casper);
5 };
6
7 var Mouse = function(casper) {
8 if (!utils.isCasperObject(casper)) {
9 throw new Error('Mouse() needs a Casper instance');
10 }
11
12 var supportedEvents = ['mouseup', 'mousedown', 'click', 'mousemove'];
13
14 var computeCenter = function(selector) {
15 var bounds = casper.getElementBounds(selector);
16 if (utils.isClipRect(bounds)) {
17 var x = Math.round(bounds.left + bounds.width / 2);
18 var y = Math.round(bounds.top + bounds.height / 2);
19 return [x, y];
20 }
21 };
22
23 var processEvent = function(type, args) {
24 if (!utils.isString(type) || supportedEvents.indexOf(type) === -1) {
25 throw new Error('Unsupported mouse event type: ' + type);
26 }
27 args = Array.prototype.slice.call(args); // cast Arguments -> Array
28 switch (args.length) {
29 case 0:
30 throw new Error('Too few arguments');
31 case 1:
32 // selector
33 var selector = args[0];
34 if (!utils.isString(selector)) {
35 throw new Error('No valid CSS selector passed: ' + selector);
36 }
37 casper.page.sendEvent.apply(casper.page, [type].concat(computeCenter(selector)))
38 break;
39 case 2:
40 // coordinates
41 if (!utils.isNumber(args[1]) || !utils.isNumber(args[2])) {
42 throw new Error('No valid coordinates passed');
43 }
44 casper.page.sendEvent(type, args[0], args[1])
45 break;
46 default:
47 throw new Error('Too many arguments');
48 }
49 };
50
51 this.click = function() {
52 processEvent('click', arguments);
53 },
54
55 this.down = function() {
56 processEvent('mousedown', arguments);
57 },
58
59 this.move = function() {
60 processEvent('mousemove', arguments);
61 },
62
63 this.up = function() {
64 processEvent('mouseup', arguments);
65 }
66 };
67 exports.Mouse = Mouse;
...@@ -137,7 +137,7 @@ exports.isObject = isObject; ...@@ -137,7 +137,7 @@ exports.isObject = isObject;
137 function isString(value) { 137 function isString(value) {
138 return isType(value, "string"); 138 return isType(value, "string");
139 } 139 }
140 exports.isFunction = isFunction; 140 exports.isString = isString;
141 141
142 /** 142 /**
143 * Shorthands for checking if a value is of the given type. Can check for 143 * Shorthands for checking if a value is of the given type. Can check for
......
1 (function(t) {
2 casper.start('tests/site/index.html', function(self) {
3 self.mouseClick('a[href="test.html"]');
4 });
5
6 casper.then(function(self) {
7 t.comment('Casper.mouseClick()');
8 t.assertTitle('CasperJS test target', 'Casper.mouseClick() can click on a link');
9 });
10
11 // onclick variants tests
12 casper.thenOpen('tests/site/click.html', function(self) {
13 t.comment('Casper.mouseClick()');
14 self.mouseClick('#test1');
15 self.mouseClick('#test2');
16 self.mouseClick('#test3');
17 self.mouseClick('#test4');
18 var results = self.getGlobal('results');
19 self.test.assert(results.test1, 'Casper.mouseClick() has clicked an `href="javascript:` link');
20 self.test.assert(results.test2, 'Casper.mouseClick() has clicked an `href="#"` link');
21 self.test.assert(results.test3, 'Casper.mouseClick() has clicked an `onclick=".*; return false"` link');
22 self.test.assert(results.test4, 'Casper.mouseClick() has clicked an unobstrusive js handled link');
23 });
24
25 casper.run(function(self) {
26 t.done();
27 });
28 })(casper.test);
...\ No newline at end of file ...\ No newline at end of file