Skip to content

Instantly share code, notes, and snippets.

@markjames
Created January 12, 2012 10:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markjames/1599793 to your computer and use it in GitHub Desktop.
Save markjames/1599793 to your computer and use it in GitHub Desktop.
// Browsermob Script
// This test emulates a single user visiting the homepage,
// and downloading any necessary assets
var httpSiteRoot = 'http://mydomain.cat/';
var assetRoot = 'http://mydomain.cat/static/';
var uploadRoot = 'https://s3-eu-west-1.amazonaws.com/mydomain.cat/';
// ==============================================
// Utils
// ==============================================
var Utils = {};
Utils.getRandomInt = function(min, max) {
return String(Math.floor(Math.random() * (max - min + 1)) + min);
};
Utils.rand = function(min, max) {
return String(Math.floor(Math.random() * (max - min + 1)) + min);
};
Utils.randomString = function( len ) {
var chars = "0123456789abcdef";
var randomstring = '';
var i=len;
for (; i > 0; i--) {
var rnum = Math.floor(Math.random() * chars.length);
randomstring += chars.substring(rnum,rnum+1);
}
return randomstring;
};
Utils.pickRandomFrom = function(array) {
return array[Utils.getRandomInt(0, array.length-1)];
};
function Mob( browserMob, client ) {
this.browserMob = browserMob;
this.client = client;
}
Mob.prototype.expect = function( url, code, content ) {
var resp;
if (typeof code === 'undefined' ) {
code = 200;
}
if (typeof content === 'undefined' ) {
resp = this.client.get( url );
if( code !== resp.getInfo().getStatusCode() ) {
throw "Request to " + url + " failed: Expecting response code " + code + " but received code " + resp.getInfo().getStatusCode();
}
return resp;
} else {
resp = this.client.get( url, content );
if( code !== resp.getInfo().getStatusCode() ) {
throw "Request to " + url + " failed: Expecting response code " + code + " but received code " + resp.getInfo().getStatusCode();
}
if( !resp.isContentMatched() ) {
throw "Request to " + url + " failed: Expected content " + content + " not found";
}
return resp;
}
};
// ==============================================
// Setup
// ==============================================
var c = browserMob.openHttpClient();
c.setFollowRedirects(true);
browserMob.beginTransaction();
var mob = new Mob( browserMob, c );
// ==============================================
// Step 1: Opening the Homepage
//
// This step opens the survey page and all associated images
// ==============================================
browserMob.beginStep("Opening the Homepage");
mob.expect( httpSiteRoot + "", 200, "This text should be present." );
mob.expect( assetRoot + "icons/favicon.ico", 200 );
mob.expect( assetRoot + "fonts/webfont.woff", 200 );
mob.expect( assetRoot + "icons/twitter.gif", 200 );
mob.expect( assetRoot + "images/body_bg.gif", 200 );
mob.expect( assetRoot + "scripts/libs/modernizr-2.0.6.min.js", 200 );
mob.expect( assetRoot + "styles/style.css", 200 );
browserMob.endStep();
// ==============================================
// Step 2: Try and save some results
//
//
// ==============================================
browserMob.beginStep("Saving Result");
resp = c.post( httpSiteRoot + "save.php", 'success', {
"id": 1,
"score": Utils.rand(1,5)
});
if( !resp.isContentMatched() ) {
throw "Unable to Save Data";
}
browserMob.endStep();
// ==============================================
// FIN
// ==============================================
browserMob.endTransaction();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment