Skip to content

Instantly share code, notes, and snippets.

@iwek
Created July 16, 2012 20:07
Show Gist options
  • Save iwek/3124720 to your computer and use it in GitHub Desktop.
Save iwek/3124720 to your computer and use it in GitHub Desktop.
casperjs user-agent example
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)
*/
@iwek
Copy link
Author

iwek commented Jul 17, 2012

@soundyogi
Copy link

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