Commit b84771ef b84771ef9e18c062cf5d7905c0f114a84d89bec9 by Nicolas Perriault

better implementation of Casper.download()

1 parent 1932b913
...@@ -293,10 +293,20 @@ ...@@ -293,10 +293,20 @@
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 /**
297 * Downloads a resource and saves it on the filesystem.
298 *
299 * @param String url The url of the resource to download
300 * @param String targetPath The destination file path
301 * @return Casper
302 */
296 download: function(url, targetPath) { 303 download: function(url, targetPath) {
297 var fs = require('fs');
298 var cu = new phantom.Casper.ClientUtils(); 304 var cu = new phantom.Casper.ClientUtils();
299 fs.write(targetPath, cu.decode(this.base64encode(url)), 'w'); 305 try {
306 require('fs').write(targetPath, cu.decode(this.base64encode(url)), 'w');
307 } catch (e) {
308 this.log("Error while downloading " + url + " to " + targetPath + ": " + e, "error");
309 }
300 return this; 310 return this;
301 }, 311 },
302 312
......
...@@ -74,44 +74,44 @@ ...@@ -74,44 +74,44 @@
74 * @return string 74 * @return string
75 */ 75 */
76 this.decode = function(str) { 76 this.decode = function(str) {
77 // var c1, c2, c3, c4, i = 0, len = str.length, out = ""; 77 var c1, c2, c3, c4, i = 0, len = str.length, out = "";
78 // while (i < len) { 78 while (i < len) {
79 // do { 79 do {
80 // c1 = BASE64_DECODE_CHARS[str.charCodeAt(i++) & 0xff]; 80 c1 = BASE64_DECODE_CHARS[str.charCodeAt(i++) & 0xff];
81 // } while (i < len && c1 == -1); 81 } while (i < len && c1 == -1);
82 // if (c1 == -1) { 82 if (c1 == -1) {
83 // break; 83 break;
84 // } 84 }
85 // do { 85 do {
86 // c2 = BASE64_DECODE_CHARS[str.charCodeAt(i++) & 0xff]; 86 c2 = BASE64_DECODE_CHARS[str.charCodeAt(i++) & 0xff];
87 // } while (i < len && c2 == -1); 87 } while (i < len && c2 == -1);
88 // if (c2 == -1) { 88 if (c2 == -1) {
89 // break; 89 break;
90 // } 90 }
91 // out += String.fromCharCode((c1 << 2) | ((c2 & 0x30) >> 4)); 91 out += String.fromCharCode((c1 << 2) | ((c2 & 0x30) >> 4));
92 // do { 92 do {
93 // c3 = str.charCodeAt(i++) & 0xff; 93 c3 = str.charCodeAt(i++) & 0xff;
94 // if (c3 == 61) 94 if (c3 == 61)
95 // return out; 95 return out;
96 // c3 = BASE64_DECODE_CHARS[c3]; 96 c3 = BASE64_DECODE_CHARS[c3];
97 // } while (i < len && c3 == -1); 97 } while (i < len && c3 == -1);
98 // if (c3 == -1) { 98 if (c3 == -1) {
99 // break; 99 break;
100 // } 100 }
101 // out += String.fromCharCode(((c2 & 0XF) << 4) | ((c3 & 0x3C) >> 2)); 101 out += String.fromCharCode(((c2 & 0XF) << 4) | ((c3 & 0x3C) >> 2));
102 // do { 102 do {
103 // c4 = str.charCodeAt(i++) & 0xff; 103 c4 = str.charCodeAt(i++) & 0xff;
104 // if (c4 == 61) { 104 if (c4 == 61) {
105 // return out; 105 return out;
106 // } 106 }
107 // c4 = BASE64_DECODE_CHARS[c4]; 107 c4 = BASE64_DECODE_CHARS[c4];
108 // } while (i < len && c4 == -1); 108 } while (i < len && c4 == -1);
109 // if (c4 == -1) { 109 if (c4 == -1) {
110 // break; 110 break;
111 // } 111 }
112 // out += String.fromCharCode(((c3 & 0x03) << 6) | c4); 112 out += String.fromCharCode(((c3 & 0x03) << 6) | c4);
113 // } 113 }
114 // return out; 114 return out;
115 }; 115 };
116 116
117 /** 117 /**
......
...@@ -11,7 +11,7 @@ ...@@ -11,7 +11,7 @@
11 "Les nuages filent et le ciel s'éclaircit\n" + 11 "Les nuages filent et le ciel s'éclaircit\n" +
12 "Et dans ma tête qui bourdonnent?\n" + 12 "Et dans ma tête qui bourdonnent?\n" +
13 "Les abeilles!"), 13 "Les abeilles!"),
14 'a file contents': fs.read(phantom.casperPath + '/tests/site/images/phantom.png') 14 'a file contents': fs.read(phantom.casperPath + '/tests/site/alert.html')
15 }; 15 };
16 16
17 for (var what in testCases) { 17 for (var what in testCases) {
......
1 (function(t) { 1 (function(t) {
2 t.comment('Casper.base64encode()');
3
4 casper.start('tests/site/index.html', function(self) { 2 casper.start('tests/site/index.html', function(self) {
5 var imageUrl = 'file://' + phantom.casperPath + '/tests/site/images/phantom.png'; 3 var imageUrl = 'file://' + phantom.casperPath + '/tests/site/images/phantom.png';
6 var image = self.base64encode(imageUrl); 4 var image = self.base64encode(imageUrl);
5 var fs = require('fs');
6
7 t.comment('Casper.base64encode()');
7 t.assertEquals(image.length, 6160, 'Casper.base64encode() can retrieve base64 contents'); 8 t.assertEquals(image.length, 6160, 'Casper.base64encode() can retrieve base64 contents');
9
10 t.comment('Casper.download()');
8 self.download(imageUrl, 'logo.png'); 11 self.download(imageUrl, 'logo.png');
9 var fs = require('fs');
10 t.assert(fs.exists('logo.png'), 'Casper.download() downloads a file'); 12 t.assert(fs.exists('logo.png'), 'Casper.download() downloads a file');
13 if (fs.exists('logo.png')) {
14 fs.remove('logo.png');
15 }
11 }); 16 });
12 17
13 casper.run(function(self) { 18 casper.run(function(self) {
......