attempt to add a dowload feature to Casper
Showing
4 changed files
with
105 additions
and
12 deletions
... | @@ -293,6 +293,13 @@ | ... | @@ -293,6 +293,13 @@ |
293 | return this.exit(Number(status) > 0 ? Number(status) : 1); | 293 | return this.exit(Number(status) > 0 ? Number(status) : 1); |
294 | }, | 294 | }, |
295 | 295 | ||
296 | download: function(url, targetPath) { | ||
297 | var fs = require('fs'); | ||
298 | var cu = new phantom.Casper.ClientUtils(); | ||
299 | fs.write(targetPath, cu.decode(this.base64encode(url)), 'w'); | ||
300 | return this; | ||
301 | }, | ||
302 | |||
296 | /** | 303 | /** |
297 | * Iterates over the values of a provided array and execute a callback | 304 | * Iterates over the values of a provided array and execute a callback |
298 | * for each item. | 305 | * for each item. | ... | ... |
... | @@ -30,6 +30,18 @@ | ... | @@ -30,6 +30,18 @@ |
30 | * Casper client-side helpers. | 30 | * Casper client-side helpers. |
31 | */ | 31 | */ |
32 | phantom.Casper.ClientUtils = function() { | 32 | phantom.Casper.ClientUtils = function() { |
33 | var BASE64_ENCODE_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; | ||
34 | var BASE64_DECODE_CHARS = new Array( | ||
35 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | ||
36 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | ||
37 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, | ||
38 | 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, | ||
39 | -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, | ||
40 | 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, | ||
41 | -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, | ||
42 | 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1 | ||
43 | ); | ||
44 | |||
33 | /** | 45 | /** |
34 | * Clicks on the DOM element behind the provided selector. | 46 | * Clicks on the DOM element behind the provided selector. |
35 | * | 47 | * |
... | @@ -56,36 +68,82 @@ | ... | @@ -56,36 +68,82 @@ |
56 | }; | 68 | }; |
57 | 69 | ||
58 | /** | 70 | /** |
71 | * Decodes a base64 encoded string. Succeeds where window.atob() fails. | ||
72 | * | ||
73 | * @param String str The base64 encoded contents | ||
74 | * @return string | ||
75 | */ | ||
76 | this.decode = function(str) { | ||
77 | // var c1, c2, c3, c4, i = 0, len = str.length, out = ""; | ||
78 | // while (i < len) { | ||
79 | // do { | ||
80 | // c1 = BASE64_DECODE_CHARS[str.charCodeAt(i++) & 0xff]; | ||
81 | // } while (i < len && c1 == -1); | ||
82 | // if (c1 == -1) { | ||
83 | // break; | ||
84 | // } | ||
85 | // do { | ||
86 | // c2 = BASE64_DECODE_CHARS[str.charCodeAt(i++) & 0xff]; | ||
87 | // } while (i < len && c2 == -1); | ||
88 | // if (c2 == -1) { | ||
89 | // break; | ||
90 | // } | ||
91 | // out += String.fromCharCode((c1 << 2) | ((c2 & 0x30) >> 4)); | ||
92 | // do { | ||
93 | // c3 = str.charCodeAt(i++) & 0xff; | ||
94 | // if (c3 == 61) | ||
95 | // return out; | ||
96 | // c3 = BASE64_DECODE_CHARS[c3]; | ||
97 | // } while (i < len && c3 == -1); | ||
98 | // if (c3 == -1) { | ||
99 | // break; | ||
100 | // } | ||
101 | // out += String.fromCharCode(((c2 & 0XF) << 4) | ((c3 & 0x3C) >> 2)); | ||
102 | // do { | ||
103 | // c4 = str.charCodeAt(i++) & 0xff; | ||
104 | // if (c4 == 61) { | ||
105 | // return out; | ||
106 | // } | ||
107 | // c4 = BASE64_DECODE_CHARS[c4]; | ||
108 | // } while (i < len && c4 == -1); | ||
109 | // if (c4 == -1) { | ||
110 | // break; | ||
111 | // } | ||
112 | // out += String.fromCharCode(((c3 & 0x03) << 6) | c4); | ||
113 | // } | ||
114 | // return out; | ||
115 | }; | ||
116 | |||
117 | /** | ||
59 | * Base64 encodes a string, even binary ones. Succeeds where | 118 | * Base64 encodes a string, even binary ones. Succeeds where |
60 | * window.btoa() fails. | 119 | * window.btoa() fails. |
61 | * | 120 | * |
62 | * @param String str | 121 | * @param String str The string content to encode |
63 | * @return string | 122 | * @return string |
64 | */ | 123 | */ |
65 | this.encode = function(str) { | 124 | this.encode = function(str) { |
66 | var CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; | ||
67 | var out = "", i = 0, len = str.length, c1, c2, c3; | 125 | var out = "", i = 0, len = str.length, c1, c2, c3; |
68 | while (i < len) { | 126 | while (i < len) { |
69 | c1 = str.charCodeAt(i++) & 0xff; | 127 | c1 = str.charCodeAt(i++) & 0xff; |
70 | if (i == len) { | 128 | if (i == len) { |
71 | out += CHARS.charAt(c1 >> 2); | 129 | out += BASE64_ENCODE_CHARS.charAt(c1 >> 2); |
72 | out += CHARS.charAt((c1 & 0x3) << 4); | 130 | out += BASE64_ENCODE_CHARS.charAt((c1 & 0x3) << 4); |
73 | out += "=="; | 131 | out += "=="; |
74 | break; | 132 | break; |
75 | } | 133 | } |
76 | c2 = str.charCodeAt(i++); | 134 | c2 = str.charCodeAt(i++); |
77 | if (i == len) { | 135 | if (i == len) { |
78 | out += CHARS.charAt(c1 >> 2); | 136 | out += BASE64_ENCODE_CHARS.charAt(c1 >> 2); |
79 | out += CHARS.charAt(((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4)); | 137 | out += BASE64_ENCODE_CHARS.charAt(((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4)); |
80 | out += CHARS.charAt((c2 & 0xF) << 2); | 138 | out += BASE64_ENCODE_CHARS.charAt((c2 & 0xF) << 2); |
81 | out += "="; | 139 | out += "="; |
82 | break; | 140 | break; |
83 | } | 141 | } |
84 | c3 = str.charCodeAt(i++); | 142 | c3 = str.charCodeAt(i++); |
85 | out += CHARS.charAt(c1 >> 2); | 143 | out += BASE64_ENCODE_CHARS.charAt(c1 >> 2); |
86 | out += CHARS.charAt(((c1 & 0x3) << 4) | ((c2 & 0xF0) >> 4)); | 144 | out += BASE64_ENCODE_CHARS.charAt(((c1 & 0x3) << 4) | ((c2 & 0xF0) >> 4)); |
87 | out += CHARS.charAt(((c2 & 0xF) << 2) | ((c3 & 0xC0) >> 6)); | 145 | out += BASE64_ENCODE_CHARS.charAt(((c2 & 0xF) << 2) | ((c3 & 0xC0) >> 6)); |
88 | out += CHARS.charAt(c3 & 0x3F); | 146 | out += BASE64_ENCODE_CHARS.charAt(c3 & 0x3F); |
89 | } | 147 | } |
90 | return out; | 148 | return out; |
91 | }; | 149 | }; | ... | ... |
tests/suites/casper/clientutils.js
0 → 100644
1 | (function(t) { | ||
2 | t.comment('ClientUtils.encode()'); | ||
3 | |||
4 | var fs = require('fs'); | ||
5 | var utils = new phantom.Casper.ClientUtils(); | ||
6 | var testCases = { | ||
7 | 'an empty string': '', | ||
8 | 'a word': 'plop', | ||
9 | 'an utf8 string': '<chè!cs!<egfhèqzgrqgzf', | ||
10 | 'song lyrics': ("Voilà l'été, j'aperçois le soleil\n" + | ||
11 | "Les nuages filent et le ciel s'éclaircit\n" + | ||
12 | "Et dans ma tête qui bourdonnent?\n" + | ||
13 | "Les abeilles!"), | ||
14 | 'a file contents': fs.read(phantom.casperPath + '/tests/site/images/phantom.png') | ||
15 | }; | ||
16 | |||
17 | for (var what in testCases) { | ||
18 | var source = testCases[what]; | ||
19 | var encoded = utils.encode(source); | ||
20 | t.assertEquals(utils.decode(encoded), source, 'ClientUtils can encode and decode ' + what); | ||
21 | } | ||
22 | |||
23 | t.done(); | ||
24 | })(casper.test); |
... | @@ -2,8 +2,12 @@ | ... | @@ -2,8 +2,12 @@ |
2 | t.comment('Casper.base64encode()'); | 2 | t.comment('Casper.base64encode()'); |
3 | 3 | ||
4 | casper.start('tests/site/index.html', function(self) { | 4 | casper.start('tests/site/index.html', function(self) { |
5 | var image = self.base64encode('file://' + phantom.casperPath + '/tests/site/images/phantom.png'); | 5 | var imageUrl = 'file://' + phantom.casperPath + '/tests/site/images/phantom.png'; |
6 | var image = self.base64encode(imageUrl); | ||
6 | t.assertEquals(image.length, 6160, 'Casper.base64encode() can retrieve base64 contents'); | 7 | t.assertEquals(image.length, 6160, 'Casper.base64encode() can retrieve base64 contents'); |
8 | self.download(imageUrl, 'logo.png'); | ||
9 | var fs = require('fs'); | ||
10 | t.assert(fs.exists('logo.png'), 'Casper.download() downloads a file'); | ||
7 | }); | 11 | }); |
8 | 12 | ||
9 | casper.run(function(self) { | 13 | casper.run(function(self) { | ... | ... |
-
Please register or sign in to post a comment