forthported Casper.mouseClick() from 0.6 branch
Showing
6 changed files
with
96 additions
and
15 deletions
... | @@ -5,6 +5,7 @@ XXXX-XX-XX, v0.4.3 | ... | @@ -5,6 +5,7 @@ XXXX-XX-XX, v0.4.3 |
5 | ------------------ | 5 | ------------------ |
6 | 6 | ||
7 | - fixed #32 - `ClientUtils.click()` handles `<a href='javascript:'>` links | 7 | - fixed #32 - `ClientUtils.click()` handles `<a href='javascript:'>` links |
8 | - fixed #34 - avoid having previously loaded DOM contents being still active on run complete | ||
8 | 9 | ||
9 | 2011-12-25, v0.4.2 | 10 | 2011-12-25, v0.4.2 |
10 | ------------------ | 11 | ------------------ | ... | ... |
... | @@ -172,17 +172,7 @@ | ... | @@ -172,17 +172,7 @@ |
172 | */ | 172 | */ |
173 | captureSelector: function(targetFile, selector) { | 173 | captureSelector: function(targetFile, selector) { |
174 | return this.capture(targetFile, this.evaluate(function(selector) { | 174 | return this.capture(targetFile, this.evaluate(function(selector) { |
175 | try { | 175 | return __utils__.getElementBounds(); |
176 | var clipRect = document.querySelector(selector).getBoundingClientRect(); | ||
177 | return { | ||
178 | top: clipRect.top, | ||
179 | left: clipRect.left, | ||
180 | width: clipRect.width, | ||
181 | height: clipRect.height | ||
182 | }; | ||
183 | } catch (e) { | ||
184 | __utils__.log("Unable to fetch bounds for element " + selector, "warning"); | ||
185 | } | ||
186 | }, { selector: selector })); | 176 | }, { selector: selector })); |
187 | }, | 177 | }, |
188 | 178 | ||
... | @@ -591,6 +581,26 @@ | ... | @@ -591,6 +581,26 @@ |
591 | }, | 581 | }, |
592 | 582 | ||
593 | /** | 583 | /** |
584 | * Emulates a click on an HTML element matching a given CSS3 selector, | ||
585 | * using the mouse pointer. | ||
586 | * | ||
587 | * @param String selector A DOM CSS3 compatible selector | ||
588 | * @return Casper | ||
589 | */ | ||
590 | mouseClick: function(selector) { | ||
591 | var bounds = this.evaluate(function(selector) { | ||
592 | return __utils__.getElementBounds(selector); | ||
593 | }, { selector: selector }); | ||
594 | if (isClipRect(bounds)) { | ||
595 | var x = bounds.left + Math.floor(bounds.width / 2); | ||
596 | var y = bounds.top + Math.floor(bounds.height / 2); | ||
597 | this.page.sendEvent('click', x, y); | ||
598 | } | ||
599 | return this; | ||
600 | }, | ||
601 | |||
602 | |||
603 | /** | ||
594 | * Opens a page. Takes only one argument, the url to open (using the | 604 | * Opens a page. Takes only one argument, the url to open (using the |
595 | * callback argument would defeat the whole purpose of Casper | 605 | * callback argument would defeat the whole purpose of Casper |
596 | * actually). | 606 | * actually). | ... | ... |
... | @@ -347,6 +347,27 @@ | ... | @@ -347,6 +347,27 @@ |
347 | }; | 347 | }; |
348 | 348 | ||
349 | /** | 349 | /** |
350 | * Retrieves bounding rect coordinates of the HTML element matching the | ||
351 | * provided CSS3 selector | ||
352 | * | ||
353 | * @param String selector | ||
354 | * @return Object or null | ||
355 | */ | ||
356 | this.getElementBounds = function(selector) { | ||
357 | try { | ||
358 | var clipRect = document.querySelector(selector).getBoundingClientRect(); | ||
359 | return { | ||
360 | top: clipRect.top, | ||
361 | left: clipRect.left, | ||
362 | width: clipRect.width, | ||
363 | height: clipRect.height | ||
364 | }; | ||
365 | } catch (e) { | ||
366 | this.log("Unable to fetch bounds for element " + selector, "warning"); | ||
367 | } | ||
368 | }; | ||
369 | |||
370 | /** | ||
350 | * Logs a message. | 371 | * Logs a message. |
351 | * | 372 | * |
352 | * @param String message | 373 | * @param String message | ... | ... |
... | @@ -180,6 +180,16 @@ function fillBlanks(text, pad) { | ... | @@ -180,6 +180,16 @@ function fillBlanks(text, pad) { |
180 | return text; | 180 | return text; |
181 | } | 181 | } |
182 | 182 | ||
183 | function isClipRect(value) { | ||
184 | return isType(value, "cliprect") || ( | ||
185 | isType(value, "object") && | ||
186 | isType(value.top, "number") && | ||
187 | isType(value.left, "number") && | ||
188 | isType(value.width, "number") && | ||
189 | isType(value.height, "number") | ||
190 | ); | ||
191 | } | ||
192 | |||
183 | /** | 193 | /** |
184 | * Checks if a file is apparently javascript compatible (.js or .coffee). | 194 | * Checks if a file is apparently javascript compatible (.js or .coffee). |
185 | * | 195 | * | ... | ... |
tests/suites/casper/mouseclick.js
0 → 100644
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 |
1 | (function(t) { | 1 | (function(t) { |
2 | t.comment('fileExt()'); | 2 | t.comment('fileExt()'); |
3 | |||
4 | (function() { | 3 | (function() { |
5 | var testCases = { | 4 | var testCases = { |
6 | 'foo.ext': 'ext', | 5 | 'foo.ext': 'ext', |
... | @@ -17,7 +16,6 @@ | ... | @@ -17,7 +16,6 @@ |
17 | })(); | 16 | })(); |
18 | 17 | ||
19 | t.comment('fillBlanks()'); | 18 | t.comment('fillBlanks()'); |
20 | |||
21 | (function() { | 19 | (function() { |
22 | testCases = { | 20 | testCases = { |
23 | 'foo': 'foo ', | 21 | 'foo': 'foo ', |
... | @@ -30,8 +28,22 @@ | ... | @@ -30,8 +28,22 @@ |
30 | } | 28 | } |
31 | })(); | 29 | })(); |
32 | 30 | ||
33 | t.comment('isJsFile()'); | 31 | t.comment('isClipRect()'); |
32 | (function() { | ||
33 | testCases = [ | ||
34 | [{}, false], | ||
35 | [{top: 2}, false], | ||
36 | [{top: 2, left: 2, width: 2, height: 2}, true], | ||
37 | [{top: 2, left: 2, height: 2, width: 2}, true], | ||
38 | [{top: 2, left: 2, width: 2, height: new Date()}, false] | ||
39 | ]; | ||
34 | 40 | ||
41 | testCases.forEach(function(testCase) { | ||
42 | t.assertEquals(isClipRect(testCase[0]), testCase[1], 'isClipRect() checks for a ClipRect'); | ||
43 | }); | ||
44 | })(); | ||
45 | |||
46 | t.comment('isJsFile()'); | ||
35 | (function() { | 47 | (function() { |
36 | testCases = { | 48 | testCases = { |
37 | '': false, | 49 | '': false, |
... | @@ -47,7 +59,6 @@ | ... | @@ -47,7 +59,6 @@ |
47 | })(); | 59 | })(); |
48 | 60 | ||
49 | t.comment('mergeObjects()'); | 61 | t.comment('mergeObjects()'); |
50 | |||
51 | (function() { | 62 | (function() { |
52 | testCases = [ | 63 | testCases = [ |
53 | { | 64 | { | ... | ... |
-
Please register or sign in to post a comment