Created
July 16, 2012 20:07
-
-
Save iwek/3124720 to your computer and use it in GitHub Desktop.
casperjs user-agent example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var casper = require('casper').create(); | |
var webpage = "http://wordpress.org/"; | |
casper.on('resource.requested', function(resource) { | |
for (var obj in resource.headers) { | |
var name = resource.headers[obj].name; | |
var value = resource.headers[obj].value; | |
if (name == "User-Agent"){ | |
this.echo(value); | |
} | |
} | |
}); | |
casper.start(); | |
casper.userAgent('Mozilla/5.0 (Macintosh; Intel Mac OS X)'); | |
casper.thenOpen('http://google.com/', function() { | |
this.echo("I'm a Mac."); | |
}); | |
this.userAgent('Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)'); | |
this.thenOpen('http://google.com/', function() { | |
this.echo("I'm a PC."); | |
}); | |
casper.run(); | |
/* | |
//OUTPUT: does not work, last user-agent string used for both runs | |
Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1) | |
Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1) | |
Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1) | |
Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1) | |
Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1) | |
Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1) | |
Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1) | |
Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1) | |
I'm a Mac. | |
Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1) | |
Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1) | |
Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1) | |
I'm a PC. | |
Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1) | |
*/ |
You have to put your stuff in .then() blocks then you can change the userAgent on the fly
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Blog post about this gist: http://techslides.com/using-casperjs-to-make-multibrowser-screenshots/