Skip to content
Toggle navigation
Toggle navigation
This project
Loading...
Sign in
John McEleney
/
casperjs
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Network
Create a new issue
Builds
Commits
Issue Boards
Files
Commits
Network
Compare
Branches
Tags
Commit
1932b913
...
1932b913623cec169179cb37c8dbabf2d0871ad8
authored
2011-12-22 12:18:28 +0100
by
Nicolas Perriault
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
attempt to add a dowload feature to Casper
1 parent
e4c19cd7
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
106 additions
and
13 deletions
lib/casper.js
lib/clientutils.js
tests/suites/casper/clientutils.js
tests/suites/casper/encode.js
lib/casper.js
View file @
1932b91
...
...
@@ -293,6 +293,13 @@
return
this
.
exit
(
Number
(
status
)
>
0
?
Number
(
status
)
:
1
);
},
download
:
function
(
url
,
targetPath
)
{
var
fs
=
require
(
'fs'
);
var
cu
=
new
phantom
.
Casper
.
ClientUtils
();
fs
.
write
(
targetPath
,
cu
.
decode
(
this
.
base64encode
(
url
)),
'w'
);
return
this
;
},
/**
* Iterates over the values of a provided array and execute a callback
* for each item.
...
...
lib/clientutils.js
View file @
1932b91
...
...
@@ -30,6 +30,18 @@
* Casper client-side helpers.
*/
phantom
.
Casper
.
ClientUtils
=
function
()
{
var
BASE64_ENCODE_CHARS
=
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
;
var
BASE64_DECODE_CHARS
=
new
Array
(
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
62
,
-
1
,
-
1
,
-
1
,
63
,
52
,
53
,
54
,
55
,
56
,
57
,
58
,
59
,
60
,
61
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
0
,
1
,
2
,
3
,
4
,
5
,
6
,
7
,
8
,
9
,
10
,
11
,
12
,
13
,
14
,
15
,
16
,
17
,
18
,
19
,
20
,
21
,
22
,
23
,
24
,
25
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
26
,
27
,
28
,
29
,
30
,
31
,
32
,
33
,
34
,
35
,
36
,
37
,
38
,
39
,
40
,
41
,
42
,
43
,
44
,
45
,
46
,
47
,
48
,
49
,
50
,
51
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
);
/**
* Clicks on the DOM element behind the provided selector.
*
...
...
@@ -56,36 +68,82 @@
};
/**
* Decodes a base64 encoded string. Succeeds where window.atob() fails.
*
* @param String str The base64 encoded contents
* @return string
*/
this
.
decode
=
function
(
str
)
{
// var c1, c2, c3, c4, i = 0, len = str.length, out = "";
// while (i < len) {
// do {
// c1 = BASE64_DECODE_CHARS[str.charCodeAt(i++) & 0xff];
// } while (i < len && c1 == -1);
// if (c1 == -1) {
// break;
// }
// do {
// c2 = BASE64_DECODE_CHARS[str.charCodeAt(i++) & 0xff];
// } while (i < len && c2 == -1);
// if (c2 == -1) {
// break;
// }
// out += String.fromCharCode((c1 << 2) | ((c2 & 0x30) >> 4));
// do {
// c3 = str.charCodeAt(i++) & 0xff;
// if (c3 == 61)
// return out;
// c3 = BASE64_DECODE_CHARS[c3];
// } while (i < len && c3 == -1);
// if (c3 == -1) {
// break;
// }
// out += String.fromCharCode(((c2 & 0XF) << 4) | ((c3 & 0x3C) >> 2));
// do {
// c4 = str.charCodeAt(i++) & 0xff;
// if (c4 == 61) {
// return out;
// }
// c4 = BASE64_DECODE_CHARS[c4];
// } while (i < len && c4 == -1);
// if (c4 == -1) {
// break;
// }
// out += String.fromCharCode(((c3 & 0x03) << 6) | c4);
// }
// return out;
};
/**
* Base64 encodes a string, even binary ones. Succeeds where
* window.btoa() fails.
*
* @param String str
* @param String str
The string content to encode
* @return string
*/
this
.
encode
=
function
(
str
)
{
var
CHARS
=
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
;
var
out
=
""
,
i
=
0
,
len
=
str
.
length
,
c1
,
c2
,
c3
;
while
(
i
<
len
)
{
c1
=
str
.
charCodeAt
(
i
++
)
&
0xff
;
if
(
i
==
len
)
{
out
+=
CHARS
.
charAt
(
c1
>>
2
);
out
+=
CHARS
.
charAt
((
c1
&
0x3
)
<<
4
);
out
+=
BASE64_ENCODE_
CHARS
.
charAt
(
c1
>>
2
);
out
+=
BASE64_ENCODE_
CHARS
.
charAt
((
c1
&
0x3
)
<<
4
);
out
+=
"=="
;
break
;
}
c2
=
str
.
charCodeAt
(
i
++
);
if
(
i
==
len
)
{
out
+=
CHARS
.
charAt
(
c1
>>
2
);
out
+=
CHARS
.
charAt
(((
c1
&
0x3
)
<<
4
)
|
((
c2
&
0xF0
)
>>
4
));
out
+=
CHARS
.
charAt
((
c2
&
0xF
)
<<
2
);
out
+=
BASE64_ENCODE_
CHARS
.
charAt
(
c1
>>
2
);
out
+=
BASE64_ENCODE_
CHARS
.
charAt
(((
c1
&
0x3
)
<<
4
)
|
((
c2
&
0xF0
)
>>
4
));
out
+=
BASE64_ENCODE_
CHARS
.
charAt
((
c2
&
0xF
)
<<
2
);
out
+=
"="
;
break
;
}
c3
=
str
.
charCodeAt
(
i
++
);
out
+=
CHARS
.
charAt
(
c1
>>
2
);
out
+=
CHARS
.
charAt
(((
c1
&
0x3
)
<<
4
)
|
((
c2
&
0xF0
)
>>
4
));
out
+=
CHARS
.
charAt
(((
c2
&
0xF
)
<<
2
)
|
((
c3
&
0xC0
)
>>
6
));
out
+=
CHARS
.
charAt
(
c3
&
0x3F
);
out
+=
BASE64_ENCODE_
CHARS
.
charAt
(
c1
>>
2
);
out
+=
BASE64_ENCODE_
CHARS
.
charAt
(((
c1
&
0x3
)
<<
4
)
|
((
c2
&
0xF0
)
>>
4
));
out
+=
BASE64_ENCODE_
CHARS
.
charAt
(((
c2
&
0xF
)
<<
2
)
|
((
c3
&
0xC0
)
>>
6
));
out
+=
BASE64_ENCODE_
CHARS
.
charAt
(
c3
&
0x3F
);
}
return
out
;
};
...
...
tests/suites/casper/clientutils.js
0 → 100644
View file @
1932b91
(
function
(
t
)
{
t
.
comment
(
'ClientUtils.encode()'
);
var
fs
=
require
(
'fs'
);
var
utils
=
new
phantom
.
Casper
.
ClientUtils
();
var
testCases
=
{
'an empty string'
:
''
,
'a word'
:
'plop'
,
'an utf8 string'
:
'<chè!cs!<egfhèqzgrqgzf'
,
'song lyrics'
:
(
"Voilà l'été, j'aperçois le soleil\n"
+
"Les nuages filent et le ciel s'éclaircit\n"
+
"Et dans ma tête qui bourdonnent?\n"
+
"Les abeilles!"
),
'a file contents'
:
fs
.
read
(
phantom
.
casperPath
+
'/tests/site/images/phantom.png'
)
};
for
(
var
what
in
testCases
)
{
var
source
=
testCases
[
what
];
var
encoded
=
utils
.
encode
(
source
);
t
.
assertEquals
(
utils
.
decode
(
encoded
),
source
,
'ClientUtils can encode and decode '
+
what
);
}
t
.
done
();
})(
casper
.
test
);
tests/suites/casper/encode.js
View file @
1932b91
...
...
@@ -2,11 +2,15 @@
t
.
comment
(
'Casper.base64encode()'
);
casper
.
start
(
'tests/site/index.html'
,
function
(
self
)
{
var
image
=
self
.
base64encode
(
'file://'
+
phantom
.
casperPath
+
'/tests/site/images/phantom.png'
);
var
imageUrl
=
'file://'
+
phantom
.
casperPath
+
'/tests/site/images/phantom.png'
;
var
image
=
self
.
base64encode
(
imageUrl
);
t
.
assertEquals
(
image
.
length
,
6160
,
'Casper.base64encode() can retrieve base64 contents'
);
self
.
download
(
imageUrl
,
'logo.png'
);
var
fs
=
require
(
'fs'
);
t
.
assert
(
fs
.
exists
(
'logo.png'
),
'Casper.download() downloads a file'
);
});
casper
.
run
(
function
(
self
)
{
t
.
done
();
});
})(
casper
.
test
);
\ No newline at end of file
})(
casper
.
test
);
...
...
Please
register
or
sign in
to post a comment