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
02d6f057
...
02d6f0578e3b2f81e60e82bcddbca56ebe8a1f11
authored
2012-06-09 08:45:37 +0200
by
Nicolas Perriault
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
added Casper.userAgent() to ease a more dynamic setting of UA
1 parent
ccf9afaa
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
38 additions
and
5 deletions
docs
modules/casper.js
tests/suites/casper/agent.js
docs
@
c351edc3
Subproject commit c
17e8c952e7405d3cf6f78cafb1539ad0b16c59d
Subproject commit c
351edc343dfe441eb425b2357fbb2baf7d64f22
...
...
modules/casper.js
View file @
02d6f05
...
...
@@ -37,6 +37,10 @@ var tester = require('tester');
var
utils
=
require
(
'utils'
);
var
f
=
utils
.
format
;
var
defaultUserAgent
=
phantom
.
defaultPageSettings
.
userAgent
.
replace
(
'PhantomJS'
,
f
(
"CasperJS/%s"
,
phantom
.
casperVersion
)
+
'+Phantomjs'
);
exports
.
create
=
function
create
(
options
)
{
return
new
Casper
(
options
);
};
...
...
@@ -65,8 +69,6 @@ exports.selectXPath = selectXPath;
* @param Object options Casper options
*/
var
Casper
=
function
Casper
(
options
)
{
var
DEFAULT_DIE_MESSAGE
=
"Suite explicitely interrupted without any message given."
;
var
DEFAULT_USER_AGENT
=
"Mozilla/5.0 (Windows NT 6.0) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.112 Safari/535.1"
;
// init & checks
if
(
!
(
this
instanceof
arguments
.
callee
))
{
return
new
Casper
(
options
);
...
...
@@ -91,7 +93,7 @@ var Casper = function Casper(options) {
page
:
null
,
pageSettings
:
{
localToRemoteUrlAccessEnabled
:
true
,
userAgent
:
DEFAULT_USER_AGENT
userAgent
:
defaultUserAgent
},
stepTimeout
:
null
,
timeout
:
null
,
...
...
@@ -364,7 +366,9 @@ Casper.prototype.debugPage = function debugPage() {
Casper
.
prototype
.
die
=
function
die
(
message
,
status
)
{
this
.
result
.
status
=
"error"
;
this
.
result
.
time
=
new
Date
().
getTime
()
-
this
.
startTime
;
message
=
utils
.
isString
(
message
)
&&
message
.
length
>
0
?
message
:
DEFAULT_DIE_MESSAGE
;
if
(
!
utils
.
isString
(
message
)
||
!
message
.
length
)
{
message
=
"Suite explicitely interrupted without any message given."
;
}
this
.
log
(
message
,
"error"
);
this
.
emit
(
'die'
,
message
,
status
);
if
(
utils
.
isFunction
(
this
.
options
.
onDie
))
{
...
...
@@ -1113,6 +1117,20 @@ Casper.prototype.thenOpenAndEvaluate = function thenOpenAndEvaluate(location, fn
};
/**
* Sets the user-agent string currently used when requesting urls.
*
* @param String userAgent User agent string
* @return String
*/
Casper
.
prototype
.
userAgent
=
function
userAgent
(
agent
)
{
if
(
!
this
.
started
)
{
throw
new
CasperError
(
"Casper not started, can't set userAgent"
);
}
this
.
options
.
pageSettings
.
userAgent
=
this
.
page
.
settings
.
userAgent
=
agent
;
return
this
;
};
/**
* Changes the current viewport size.
*
* @param Number width The viewport width, in pixels
...
...
tests/suites/casper/agent.js
0 → 100644
View file @
02d6f05
function
testUA
(
ua
,
match
)
{
casper
.
test
.
assertMatch
(
ua
,
match
,
'Default user agent matches '
+
match
);
}
testUA
(
casper
.
options
.
pageSettings
.
userAgent
,
/CasperJS/
);
casper
.
start
().
userAgent
(
'plop'
).
on
(
'resource.requested'
,
function
(
request
)
{
testUA
(
request
.
headers
.
filter
(
function
(
header
)
{
return
header
.
name
===
"User-Agent"
;
}).
pop
().
value
,
/plop/
);
}).
start
(
'tests/site/index.html'
).
run
(
function
()
{
this
.
test
.
done
();
});
Please
register
or
sign in
to post a comment