Skip to content
This repository has been archived by the owner on Jul 15, 2019. It is now read-only.

Update sauce labs dashboard with test results #282

Merged
merged 7 commits into from
Dec 2, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 80 additions & 4 deletions lib/session/testsession.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ var fs = require('fs');
var path = require('path');
var FileUtil = require("../util/fileutil");
var SessionUtil = require("./sessionUtil");
var SauceLabsUtil = require("../util/sauceLabsUtil");
var CapabilityManager = require("../util/capabilitymanager");
var existsSync = fs.existsSync || path.existsSync;
var PhantomJsSetup = require("../util/phantomJsSetup.js");

Expand Down Expand Up @@ -142,12 +144,25 @@ TestSession.prototype.runTest = function (callback) {
*/
TestSession.prototype.retryTest = function (callback, error) {

var self = this;
var self = this,
isTestPassed,
isSauceLabs;

self.retryCount += 1;

isTestPassed = error || self.isFail();
isSauceLabs = self.config.isSauceLabs;

var cm = new CapabilityManager();
var browser = self.args["browser"]
|| self.config["browser"];
var capabilities = cm.getCapability(self.config.capabilities,
browser);


// checking if there is a test failures : self.isFail()
// or if there is a failure at controller level : error
if (error || self.isFail()) {
if (isTestPassed) {
// if retryCount is provided, and retries are left for this test, lets record the failure and retry the test
if (global.retryCount - self.retryCount >= 0) {
self.logger.debug('Recording failure before retrying');
Expand All @@ -164,14 +179,75 @@ TestSession.prototype.retryTest = function (callback, error) {
} else {
self.logger.debug('Recording failure');
self.recordFailure(self.retryCount, function() {
self.driver.stop(callback, error);
if (isSauceLabs) {

self.updateSauceLabs(false, self.args.testName,
self.driver.sauceLabsSessionId,
capabilities.username,
capabilities.accessKey,
self.logger,
function(){
self.driver.stop(callback, error);
});

} else {
self.driver.stop(callback, error);
}
});
}
} else {
self.driver.stop(callback);
if (isSauceLabs) {

self.updateSauceLabs(true, self.args.testName,
self.driver.sauceLabsSessionId,
capabilities.username,
capabilities.accessKey,
self.logger,
function(){
self.driver.stop(callback, error);
});

} else {
self.driver.stop(callback);
}
}
};

/**
* Update sauce labs result
* @param passed
* @param testName
* @param sessionId
* @param username
* @param accessKey
* @param callback
*/
TestSession.prototype.updateSauceLabs = function(result,
testName,
sessionId,
username,
accessKey,
logger,
callback) {

var sauceLabsUtil = new SauceLabsUtil();

sauceLabsUtil.updateJobStatus({"passed":result, "name": testName},
sessionId,
username,
accessKey,
function(err){
if (err) {
logger.debug('Error in updating sauce labs pass result status - ' + err);
} else {
logger.debug('Successfully updated sauce labs pass result status');
}
// If failed to update sauce labs , log it but don't pass back
callback(null);
return;
});
}

/**
* Check for failures in test report
* @returns {boolean}
Expand Down
44 changes: 44 additions & 0 deletions lib/util/sauceLabsUtil.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*jslint forin:true sub:true anon:true, sloppy:true, stupid:true nomen:true, node:true continue:true*/

/*
* Copyright (c) 2014, Yahoo! Inc. All rights reserved.
* Copyrights licensed under the New BSD License.
* See the accompanying LICENSE file for terms.
*/

var SauceLabs = require('saucelabs');
var log4js = require("log4js");

function SauceLabsUtil() {
this.logger = log4js.getLogger("SauceLabsUtil");
}

/**
* Update sauce labs job status
* @param result
* @param sessionId
* @param sauceUser
* @param sauceKey
* @param callback
*/
SauceLabsUtil.prototype.updateJobStatus = function(result, sessionId, sauceUser,
sauceKey, callback) {
var sauceLabs = new SauceLabs({username: sauceUser, password: sauceKey}),
self = this;

sauceLabs.updateJob(sessionId, result, function(err) {
if (err) {
callback(new Error('Error updating Sauce pass/fail status: ' + err));
return;
}

self.logger.debug('Updated result successfully to SauceLabs for session id - '
+ sessionId);

callback(null);
return;
});

}

module.exports = SauceLabsUtil;
4 changes: 4 additions & 0 deletions npm-shrinkwrap.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@
"mkdirp": "0.3.5",
"selenium-webdriver": "2.39.0",
"underscore": "1.6.0",
"clone":"0.1.11"
"clone":"0.1.11",
"saucelabs": "0.1.1"
},
"optionalDependencies": {
"mocha": "~1.18.2",
Expand Down
33 changes: 29 additions & 4 deletions tests/unit/lib/session/testsession-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,6 @@ YUI.add('testsession-tests', function (Y) {
try {
var fileUtil = new FileUtil();
fileUtil.removeDirectory(path.resolve(process.cwd(), "myReport/arrow-target"));
console.log('Cleaned up arrow-target dir.');
}
catch(e){
}
Expand Down Expand Up @@ -245,7 +244,6 @@ YUI.add('testsession-tests', function (Y) {
try {
var fileUtil = new FileUtil();
fileUtil.removeDirectory(path.resolve(process.cwd(), "myReport/arrow-target"));
console.log('Cleaned up arrow-target dir.');
}
catch(e){
}
Expand Down Expand Up @@ -291,7 +289,6 @@ YUI.add('testsession-tests', function (Y) {
try {
var fileUtil = new FileUtil();
fileUtil.removeDirectory(path.resolve(process.cwd(), "myReport/arrow-target"));
console.log('Cleaned up arrow-target dir.');
}
catch(e){
}
Expand Down Expand Up @@ -337,7 +334,6 @@ YUI.add('testsession-tests', function (Y) {
try {
var fileUtil = new FileUtil();
fileUtil.removeDirectory(path.resolve(process.cwd(), "myReport/arrow-target"));
console.log('Cleaned up arrow-target dir.');
}
catch(e){
}
Expand All @@ -346,6 +342,35 @@ YUI.add('testsession-tests', function (Y) {
}
}));

suite.add(new Y.Test.Case({

name : "Update Sauce labs result",

testUpdateSauceLabsPass: function() {

var ts = new testSession({},{},null),
log4js = require("log4js"),
mockery = require('mockery'),
logger = log4js.getLogger("TestSession"),
sauceLabsMock = require(arrowRoot + '/tests/unit/stub/SauceLabs');

sauceLabsMock.prototype.updateJob = function(result, id, callback){
callback(null);
}

mockery.registerMock('saucelabs', sauceLabsMock);
mockery.enable();

ts.updateSauceLabs("pass","testName", "SessionId", "username", "accesskey",
logger, function(error){
A.isNull(error);
mockery.deregisterMock('sauceLabs');
mockery.disable();
});
}

}));

Y.Test.Runner.add(suite);

}, '0.0.1', {requires:['test']});
Expand Down
59 changes: 59 additions & 0 deletions tests/unit/lib/util/sauceLabsUtil-tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*jslint forin:true sub:true anon:true, sloppy:true, stupid:true, nomen:true, node:true continue:true*/

/*
* Copyright (c) 2014, Yahoo! Inc. All rights reserved.
* Copyrights licensed under the New BSD License.
* See the accompanying LICENSE file for terms.
*/

YUI.add('sauceLabsUtil-tests', function (Y) {

var path = require('path'),
mockery = require('mockery'),
arrowRoot = path.join(__dirname, '../../../..'),
suite = new Y.Test.Suite("SauceLabs Util test suite"),
sauceLabsUtil;

suite.setUp = function () {
}

suite.tearDown = function () {
mockery.disable();
mockery.deregisterAll();
}

suite.add(new Y.Test.Case({
"Update Job Status failed": function(){
var result =
{"passed":true, "name": 'SauceTestName'},
sessionId = 'SauceSessionId',
sauceUserName = 'Username',
sauceAccessKey = 'AccessKey',
sauceLabsMock = require(arrowRoot + '/tests/unit/stub/SauceLabs'),
SauceLabsUtil = require(arrowRoot + '/lib/util/sauceLabsUtil.js'),
sauceLabsUtil = new SauceLabsUtil();

mockery.registerMock('saucelabs', sauceLabsMock);
mockery.enable();

sauceLabsMock.prototype.updateJob = function(sessionId, result, callback) {
callback('error');
};

sauceLabsUtil.updateJobStatus(result,
sessionId,
sauceUserName,
sauceAccessKey,
function (error){
Y.Assert.isNotNull(error);
Y.Assert.isTrue(error.message.indexOf('Error updating Sauce pass/fail status') > -1,
"Error message does not match");
mockery.deregisterMock('saucelabs');
});
}

}));

Y.Test.Runner.add(suite);

}, '0.0.1', {requires:['test']});
11 changes: 11 additions & 0 deletions tests/unit/stub/SauceLabs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
* Copyright (c) 2014, Yahoo! Inc. All rights reserved.
* Copyrights licensed under the New BSD License.
* See the accompanying LICENSE file for terms.
*/

function SauceLabs() {
}

module.exports = SauceLabs;