added client side utils proxy method caller
Showing
3 changed files
with
74 additions
and
30 deletions
... | @@ -247,9 +247,7 @@ Casper.prototype.back = function back() { | ... | @@ -247,9 +247,7 @@ Casper.prototype.back = function back() { |
247 | */ | 247 | */ |
248 | Casper.prototype.base64encode = function base64encode(url, method, data) { | 248 | Casper.prototype.base64encode = function base64encode(url, method, data) { |
249 | "use strict"; | 249 | "use strict"; |
250 | return this.evaluate(function _evaluate(url, method, data) { | 250 | return this.callUtils("getBase64", url, method, data); |
251 | return __utils__.getBase64(url, method, data); | ||
252 | }, url, method, data); | ||
253 | }; | 251 | }; |
254 | 252 | ||
255 | /** | 253 | /** |
... | @@ -268,6 +266,27 @@ Casper.prototype.bypass = function bypass(nb) { | ... | @@ -268,6 +266,27 @@ Casper.prototype.bypass = function bypass(nb) { |
268 | }; | 266 | }; |
269 | 267 | ||
270 | /** | 268 | /** |
269 | * Invokes a client side utils object method within the remote page, with arguments. | ||
270 | * | ||
271 | * @param {String} method Method name | ||
272 | * @return {...args} Arguments | ||
273 | * @return {Mixed} | ||
274 | * @throws {CasperError} If invokation failed. | ||
275 | */ | ||
276 | Casper.prototype.callUtils = function callUtils(method) { | ||
277 | "use strict"; | ||
278 | var args = [].slice.call(arguments, 1); | ||
279 | var result = this.evaluate(function(method, args) { | ||
280 | return __utils__.__call(method, args); | ||
281 | }, method, args); | ||
282 | if (utils.isObject(result) && result.__isCallError) { | ||
283 | throw new CasperError(f("callUtils(%s) with args %s thrown an error: %s", | ||
284 | method, args, result.message)); | ||
285 | } | ||
286 | return result; | ||
287 | }; | ||
288 | |||
289 | /** | ||
271 | * Proxy method for WebPage#render. Adds a clipRect parameter for | 290 | * Proxy method for WebPage#render. Adds a clipRect parameter for |
272 | * automatically set page clipRect setting values and sets it back once | 291 | * automatically set page clipRect setting values and sets it back once |
273 | * done. If the cliprect parameter is omitted, the full page viewport | 292 | * done. If the cliprect parameter is omitted, the full page viewport |
... | @@ -715,9 +734,7 @@ Casper.prototype.evaluateOrDie = function evaluateOrDie(fn, message, status) { | ... | @@ -715,9 +734,7 @@ Casper.prototype.evaluateOrDie = function evaluateOrDie(fn, message, status) { |
715 | Casper.prototype.exists = function exists(selector) { | 734 | Casper.prototype.exists = function exists(selector) { |
716 | "use strict"; | 735 | "use strict"; |
717 | this.checkStarted(); | 736 | this.checkStarted(); |
718 | return this.evaluate(function _evaluate(selector) { | 737 | return this.callUtils("exists", selector); |
719 | return __utils__.exists(selector); | ||
720 | }, selector); | ||
721 | }; | 738 | }; |
722 | 739 | ||
723 | /** | 740 | /** |
... | @@ -742,9 +759,7 @@ Casper.prototype.exit = function exit(status) { | ... | @@ -742,9 +759,7 @@ Casper.prototype.exit = function exit(status) { |
742 | Casper.prototype.fetchText = function fetchText(selector) { | 759 | Casper.prototype.fetchText = function fetchText(selector) { |
743 | "use strict"; | 760 | "use strict"; |
744 | this.checkStarted(); | 761 | this.checkStarted(); |
745 | return this.evaluate(function _evaluate(selector) { | 762 | return this.callUtils("fetchText", selector); |
746 | return __utils__.fetchText(selector); | ||
747 | }, selector); | ||
748 | }; | 763 | }; |
749 | 764 | ||
750 | /** | 765 | /** |
... | @@ -1002,9 +1017,7 @@ Casper.prototype.getElementBounds = function getElementBounds(selector) { | ... | @@ -1002,9 +1017,7 @@ Casper.prototype.getElementBounds = function getElementBounds(selector) { |
1002 | if (!this.exists(selector)) { | 1017 | if (!this.exists(selector)) { |
1003 | throw new CasperError("No element matching selector found: " + selector); | 1018 | throw new CasperError("No element matching selector found: " + selector); |
1004 | } | 1019 | } |
1005 | var clipRect = this.evaluate(function _evaluate(selector) { | 1020 | var clipRect = this.callUtils("getElementBounds", selector); |
1006 | return __utils__.getElementBounds(selector); | ||
1007 | }, selector); | ||
1008 | if (!utils.isClipRect(clipRect)) { | 1021 | if (!utils.isClipRect(clipRect)) { |
1009 | throw new CasperError('Could not fetch boundaries for element matching selector: ' + selector); | 1022 | throw new CasperError('Could not fetch boundaries for element matching selector: ' + selector); |
1010 | } | 1023 | } |
... | @@ -1023,9 +1036,7 @@ Casper.prototype.getElementInfo = function getElementInfo(selector) { | ... | @@ -1023,9 +1036,7 @@ Casper.prototype.getElementInfo = function getElementInfo(selector) { |
1023 | if (!this.exists(selector)) { | 1036 | if (!this.exists(selector)) { |
1024 | throw new CasperError(f("Cannot get informations from %s: element not found.", selector)); | 1037 | throw new CasperError(f("Cannot get informations from %s: element not found.", selector)); |
1025 | } | 1038 | } |
1026 | return this.evaluate(function(selector) { | 1039 | return this.callUtils("getElementInfo", selector); |
1027 | return __utils__.getElementInfo(selector); | ||
1028 | }, selector); | ||
1029 | }; | 1040 | }; |
1030 | 1041 | ||
1031 | /** | 1042 | /** |
... | @@ -1040,9 +1051,7 @@ Casper.prototype.getElementsInfo = function getElementsInfo(selector) { | ... | @@ -1040,9 +1051,7 @@ Casper.prototype.getElementsInfo = function getElementsInfo(selector) { |
1040 | if (!this.exists(selector)) { | 1051 | if (!this.exists(selector)) { |
1041 | throw new CasperError(f("Cannot get information from %s: no elements found.", selector)); | 1052 | throw new CasperError(f("Cannot get information from %s: no elements found.", selector)); |
1042 | } | 1053 | } |
1043 | return this.evaluate(function(selector) { | 1054 | return this.callUtils("getElementsInfo", selector); |
1044 | return __utils__.getElementsInfo(selector); | ||
1045 | }, selector); | ||
1046 | }; | 1055 | }; |
1047 | 1056 | ||
1048 | /** | 1057 | /** |
... | @@ -1057,9 +1066,7 @@ Casper.prototype.getElementsBounds = function getElementBounds(selector) { | ... | @@ -1057,9 +1066,7 @@ Casper.prototype.getElementsBounds = function getElementBounds(selector) { |
1057 | if (!this.exists(selector)) { | 1066 | if (!this.exists(selector)) { |
1058 | throw new CasperError("No element matching selector found: " + selector); | 1067 | throw new CasperError("No element matching selector found: " + selector); |
1059 | } | 1068 | } |
1060 | return this.evaluate(function _evaluate(selector) { | 1069 | return this.callUtils("getElementsBounds", selector); |
1061 | return __utils__.getElementsBounds(selector); | ||
1062 | }, selector); | ||
1063 | }; | 1070 | }; |
1064 | 1071 | ||
1065 | /** | 1072 | /** |
... | @@ -1074,9 +1081,7 @@ Casper.prototype.getFormValues = function(selector) { | ... | @@ -1074,9 +1081,7 @@ Casper.prototype.getFormValues = function(selector) { |
1074 | if (!this.exists(selector)) { | 1081 | if (!this.exists(selector)) { |
1075 | throw new CasperError(f('Form matching selector "%s" not found', selector)); | 1082 | throw new CasperError(f('Form matching selector "%s" not found', selector)); |
1076 | } | 1083 | } |
1077 | return this.evaluate(function(selector) { | 1084 | return this.callUtils("getFormValues", selector); |
1078 | return __utils__.getFormValues(selector); | ||
1079 | }, selector); | ||
1080 | }; | 1085 | }; |
1081 | 1086 | ||
1082 | /** | 1087 | /** |
... | @@ -1328,9 +1333,7 @@ Casper.prototype.mouseEvent = function mouseEvent(type, selector) { | ... | @@ -1328,9 +1333,7 @@ Casper.prototype.mouseEvent = function mouseEvent(type, selector) { |
1328 | if (!this.exists(selector)) { | 1333 | if (!this.exists(selector)) { |
1329 | throw new CasperError(f("Cannot dispatch %s event on nonexistent selector: %s", type, selector)); | 1334 | throw new CasperError(f("Cannot dispatch %s event on nonexistent selector: %s", type, selector)); |
1330 | } | 1335 | } |
1331 | if (this.evaluate(function(type, selector) { | 1336 | if (this.callUtils("mouseEvent", type, selector)) { |
1332 | return window.__utils__.mouseEvent(type, selector); | ||
1333 | }, type, selector)) { | ||
1334 | return true; | 1337 | return true; |
1335 | } | 1338 | } |
1336 | // fallback onto native QtWebKit mouse events | 1339 | // fallback onto native QtWebKit mouse events |
... | @@ -1941,9 +1944,7 @@ Casper.prototype.viewport = function viewport(width, height, then) { | ... | @@ -1941,9 +1944,7 @@ Casper.prototype.viewport = function viewport(width, height, then) { |
1941 | Casper.prototype.visible = function visible(selector) { | 1944 | Casper.prototype.visible = function visible(selector) { |
1942 | "use strict"; | 1945 | "use strict"; |
1943 | this.checkStarted(); | 1946 | this.checkStarted(); |
1944 | return this.evaluate(function _evaluate(selector) { | 1947 | return this.callUtils("visible", selector); |
1945 | return __utils__.visible(selector); | ||
1946 | }, selector); | ||
1947 | }; | 1948 | }; |
1948 | 1949 | ||
1949 | /** | 1950 | /** | ... | ... |
... | @@ -59,6 +59,26 @@ | ... | @@ -59,6 +59,26 @@ |
59 | // public members | 59 | // public members |
60 | this.options = options || {}; | 60 | this.options = options || {}; |
61 | this.options.scope = this.options.scope || document; | 61 | this.options.scope = this.options.scope || document; |
62 | |||
63 | /** | ||
64 | * Calls a method part of the current prototype, with arguments. | ||
65 | * | ||
66 | * @param {String} method Method name | ||
67 | * @param {Array} args arguments | ||
68 | * @return {Mixed} | ||
69 | */ | ||
70 | this.__call = function __call(method, args) { | ||
71 | if (method === "__call") { | ||
72 | return; | ||
73 | } | ||
74 | try { | ||
75 | return this[method].apply(this, args); | ||
76 | } catch (err) { | ||
77 | err.__isCallError = true; | ||
78 | return err; | ||
79 | } | ||
80 | }; | ||
81 | |||
62 | /** | 82 | /** |
63 | * Clicks on the DOM element behind the provided selector. | 83 | * Clicks on the DOM element behind the provided selector. |
64 | * | 84 | * | ... | ... |
tests/suites/casper/callutils.js
0 → 100644
1 | /*global casper*/ | ||
2 | /*jshint strict:false*/ | ||
3 | casper.test.begin('Casper.callUtils()', 2, function(test) { | ||
4 | casper.start("tests/site/index.html", function(){ | ||
5 | this.evaluate(function() { | ||
6 | /*global __utils__*/ | ||
7 | __utils__.testCallUtils = function() { | ||
8 | return [].slice.call(arguments); | ||
9 | }; | ||
10 | }); | ||
11 | |||
12 | test.assertEquals(casper.callUtils("testCallUtils", "a", "b", "c"), | ||
13 | ["a", "b", "c"], | ||
14 | "Casper.callUtils() invokes a client side utility"); | ||
15 | |||
16 | test.assertThrows(casper.callUtils, ["xxx", "a", "b", "c"], | ||
17 | "Casper.callUtils() raises an error if used inappropriately"); | ||
18 | }); | ||
19 | |||
20 | casper.run(function() { | ||
21 | test.done(); | ||
22 | }); | ||
23 | }); |
-
Please register or sign in to post a comment