Skip to content

Instantly share code, notes, and snippets.

@JasonGhent
Created February 25, 2015 16:30
Show Gist options
  • Save JasonGhent/fd564e8e848a1fe431f4 to your computer and use it in GitHub Desktop.
Save JasonGhent/fd564e8e848a1fe431f4 to your computer and use it in GitHub Desktop.
BrowserStack job
siteshot: function (email, done) {
var oses = {'ios': [], 'Windows': [], 'OS X': [], 'other': []};
var successes = oses;
var browsers = require(process.cwd() + '/meta/browsers.js');
var settings = { url: 'http://example.com/' };
async.mapSeries(browsers, function (browser, shotDone) {
settings.browsers = [browser];
request({
url: 'http://www.browserstack.com/screenshots',
method: 'POST',
auth: { user: "[email protected]", pass: "PASSWORD_HERE" },
json: settings
},
function (err, response, body) {
//working
function checkStatus(b) {
if (job && !allDone) {
request.get(url + '.json', function (e, r, b) {
b = JSON.parse(b);
//console.log(job+ ' : ' +b.state);
if (b.state == 'done') {
allDone = true;
checkStatus(b);
}
else {
setTimeout(function () { //recheck every 30s
checkStatus();
}, 10000);
}
});
}
else { //ALL DONE
var s, ss = b.screenshots[0];
if (ss.browser_version === null) {ss.browser_version = ''; }
else { ss.browser_version = ' ' + ss.browser_version; }
switch (ss.os) {
case "ios":
s = '<div style= "display:inline-block"><img src= "' + ss.thumb_url + '" style= "vertical-align:middle;"/></div>' +
'<div style= "display:inline-block;padding-left:20px;">' +
'<a href= "' + ss.image_url + '">' + ss.device + ': ' + ss.os + ' ' + ss.os_version + ' ' + ss.browser + ss.browser_version + '</a>' +
'</div>';
break;
case "Windows":
s = '<div style= "display:inline-block"><img src= "' + ss.thumb_url + '" style= "vertical-align:middle;"/></div>' +
'<div style= "display:inline-block;padding-left:20px;">' +
'<a href= "' + ss.image_url + '">' + ss.os + ' ' + ss.os_version + ' ' + ss.browser + ss.browser_version + '</a>' +
'</div>';
break;
case "OS X":
s = '<div style= "display:inline-block"><img src= "' + ss.thumb_url + '" style= "vertical-align:middle;"/></div>' +
'<div style= "display:inline-block;padding-left:20px;">' +
'<a href= "' + ss.image_url + '">' + ss.os + ' ' + ss.os_version + ' ' + ss.browser + ss.browser_version + '</a>' +
'</div>';
break;
default:
ss.os = 'other';
s = '<div style= "display:inline-block"><img src= "' + ss.thumb_url + '" style= "vertical-align:middle;"/></div>' +
'<div style= "display:inline-block;padding-left:20px;">' +
'<a href= "' + ss.image_url + '">' + ss.device + ': ' + ss.os + ' ' + ss.os_version + ' ' + ss.browser + ss.browser_version + '</a>' +
'</div>';
break;
}
successes[ss.os].push(s);
shotDone(null, browser);
}
}
if (err) { shotDone(null, err); }
else {
if (body.message) {
if (browsers.indexOf(browser) === 0) {
// first job failed. Unlikely for failure to resolve
// before spinning through remaining requests, but
// this provides immediate user feedback
done(null, {response: body.message});
}
shotDone(null, {response: body});
}
else {
if (browsers.indexOf(browser) === 0) {
// first job started at least. Notify caller.
done(null, {response: 'Generating screenshots for ' + email + '. Expect an email when done.'});
}
var allDone = false;
var job = body.job_id;
var url = 'http://www.browserstack.com/screenshots/' + job;
checkStatus();
}
}
}
);
},
function () {
var message = '';
for (var i in successes) {
message += '<ul>Screenshots for ' + i + ': ';
for (var j in successes[i]) {
var ss = successes[i][j];
message += '<li>' + ss + '</li>';
}
message += '</ul>';
}
var options = {
to: { email: email },
subject: "Site Screenshots",
template: "feedback"
};
var data = {
info: 'Screenshots have been generated for: ',
message: message,
};
var emailer = new Emailer(options, data);
emailer.send();
});
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment