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
e6c6640a
...
e6c6640aa17c6f165c5ec30abfd3b1cb79dd272b
authored
2012-01-10 09:29:46 +0100
by
Nicolas Perriault
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
added tests for errors
1 parent
e3bbe81d
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
52 additions
and
13 deletions
bin/bootstrap.js
tests/suites/error.js
bin/bootstrap.js
View file @
e6c6640
...
...
@@ -86,7 +86,7 @@ if (!phantom.casperLoaded) {
// Embedded, up-to-date, validatable & controlable CoffeeScript
phantom
.
injectJs
(
fs
.
pathJoin
(
phantom
.
casperPath
,
'modules'
,
'vendors'
,
'coffee-script.js'
));
//
Adding built-in capabilities to phantom object
//
Index of file sources, for error localization
phantom
.
sourceIds
=
{};
// custom global CasperError
...
...
@@ -107,19 +107,16 @@ if (!phantom.casperLoaded) {
// Stack formatting
window
.
CasperError
.
prototype
.
formatStack
=
function
()
{
var
location
;
if
(
this
.
fileName
||
this
.
sourceId
)
{
location
=
(
this
.
fileName
||
phantom
.
sourceIds
[
this
.
sourceId
]);
}
else
{
location
=
"unknown"
;
}
var
location
=
this
.
fileName
||
phantom
.
sourceIds
[
this
.
sourceId
]
||
"unknown"
;
location
+=
this
.
line
?
':'
+
this
.
line
:
0
;
return
this
.
toString
()
+
'\n '
+
(
this
.
_from
||
"anonymous"
)
+
'() at '
+
location
;
};
// Adding stack traces to CasperError
// Inspired by phantomjs-nodify: https://github.com/jgonera/phantomjs-nodify/
// TODO: remove when phantomjs has js engine upgrade
/**
* Adding pseudo stack traces to CasperError
* Inspired by phantomjs-nodify: https://github.com/jgonera/phantomjs-nodify/
* TODO: remove when phantomjs has js engine upgrade
*/
if
(
!
new
CasperError
().
hasOwnProperty
(
'stack'
))
{
Object
.
defineProperty
(
CasperError
.
prototype
,
'stack'
,
{
set
:
function
(
string
)
{
...
...
@@ -136,6 +133,12 @@ if (!phantom.casperLoaded) {
});
}
/**
* Retrieves the javascript source code from a given .js or .coffee file.
*
* @param String file The path to the file
* @param Function|null onError An error callback (optional)
*/
phantom
.
getScriptCode
=
function
(
file
,
onError
)
{
var
scriptCode
=
fs
.
read
(
file
);
if
(
/
\.
coffee$/i
.
test
(
file
))
{
...
...
@@ -152,6 +155,17 @@ if (!phantom.casperLoaded) {
return
scriptCode
;
};
/**
* Processes a given thrown Error; handles special cases and provides an
* optional callback argument.
*
* By default, the standard behavior on uncaught error is to print the
* error stack trace to the console and exit PhantomJS.
*
* @param Error error The Error instance
* @param String file A file path to associate to this error
* @param Function callback An optional callback
*/
phantom
.
processScriptError
=
function
(
error
,
file
,
callback
)
{
if
(
!
this
.
sourceIds
.
hasOwnProperty
(
error
.
sourceId
))
{
this
.
sourceIds
[
error
.
sourceId
]
=
file
;
...
...
@@ -167,9 +181,12 @@ if (!phantom.casperLoaded) {
}
};
// Patching require()
// Inspired by phantomjs-nodify: https://github.com/jgonera/phantomjs-nodify/
// TODO: remove when PhantomJS has full module support
/**
* Patching require() to allow loading of other modules than PhantomJS'
* builtin ones.
* Inspired by phantomjs-nodify: https://github.com/jgonera/phantomjs-nodify/
* TODO: remove when PhantomJS has full module support
*/
require
=
(
function
(
require
,
requireDir
)
{
var
phantomBuiltins
=
[
'fs'
,
'webpage'
,
'webserver'
];
var
phantomRequire
=
phantom
.
__orig__require
=
require
;
...
...
tests/suites/error.js
0 → 100644
View file @
e6c6640
(
function
(
t
)
{
var
error
;
function
foo
()
{
bar
();
}
function
bar
()
{
throw
new
CasperError
(
'bar'
);
}
try
{
foo
();
}
catch
(
e
)
{
error
=
e
;
}
t
.
assertType
(
error
.
stack
,
"string"
,
"CasperError() has a stack string property set"
);
t
.
assertMatch
(
error
.
stack
,
/^CasperError: bar
\s
/
,
"CasperError() has the expected stack value"
);
t
.
done
();
})(
casper
.
test
);
\ No newline at end of file
Please
register
or
sign in
to post a comment