Skip to content

Instantly share code, notes, and snippets.

@adamrneary
Created April 19, 2016 19:00
Show Gist options
  • Save adamrneary/406fdb854fafeb0972345e9b0cb414d8 to your computer and use it in GitHub Desktop.
Save adamrneary/406fdb854fafeb0972345e9b0cb414d8 to your computer and use it in GitHub Desktop.
'use strict';
var https = require('https');
var color = require('colors/safe');
var Utils = require('nightwatch/lib/util/utils');
var endSession = require('../../commands/endSession');
var _client;
module.exports = {
init(client) {
_client = client;
this.start();
},
start() {
if (!this.use()) {
return;
}
postRequest({
// append passed/failed test stats to name
name: '[ Running... ]',
// make this test public to the team
'public': 'team'
});
},
end(testBuffer) {
if (!this.use()) {
return;
}
var passed = 0;
var failed = 0;
// iterate through each testcase and see if any tests failed
testBuffer.forEach((test) => {
if (_client.currentTest.results.testcases[test].failed) {
failed++;
} else {
passed++;
}
});
// send report
postRequest({
passed: failed === 0,
// append passed/failed test stats to name
name: `[ ${generateTestStr(passed, failed)} ]`,
// make this test public to the team
'public': 'team',
// provide test names that were included in this suite
'custom-data': { tests: testBuffer }
});
// reset session flag
endSession.reset();
// cleat test names from list
testBuffer.length = 0;
},
use() {
return _client.globals.test_settings.selenium_host.indexOf('saucelabs.com') >= 0;
}
};
function postRequest(data) {
if (!_client.sessionId) {
console.log(color.red('Unable to post data to saucelabs: session has been closed. :('));
return;
}
var options = _client.options;
// prepend suite name
data.name = `${Utils.getTestSuiteName(_client.currentTest.module)} ${data.name}`;
var body = JSON.stringify(data);
var req = https.request({
hostname: 'saucelabs.com',
// see https://wiki.saucelabs.com/display/DOCS/Job+Methods
path: `/rest/v1/${options.username}/jobs/${_client.sessionId}`,
method: 'PUT',
auth: `${options.username}:${options.accessKey}`,
headers : {
'Content-Type': 'application/json',
'Content-Length' : body.length
}
});
// We don't care about the response of this request, nor do we
// need to wait for it to finish in order to continue. So lets start it and move on.
req.on('error', (e) => {
console.log(`There was a problem with reporting saucelab test results: ${e.message}`);
});
req.write(body);
req.end();
}
function generateTestStr(pass, fail) {
var str = [];
pass && str.push(`${pass} Passed`);
fail && str.push(`${fail} Failed`);
return str.join(' | ');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment