UNPKG

@cbt/karma-cbt-launcher

Version:

Custom launcher for Karma that allows testing remotely through CrossBrowserTesting.

66 lines (55 loc) 1.89 kB
'use strict'; var request = require('request'); var cbtTunnel = require('./tunnel'); var session = require('./session'); var api = 'https://crossbrowsertesting.com/api/v3/selenium/'; function setScoreOrDescription(testId, action, value, callback) { var jsonReq = { 'selenium_test_id': testId, 'format': 'json', 'action': action }; if (action === 'set_score') { jsonReq.score = value; } else if (action === 'set_description') { jsonReq.description = value; } else { callback('action must be "set_score" or "set_description"'); } var options = { method: 'PUT', uri: api + testId, json: jsonReq, auth: { username: cbtTunnel.username, password: cbtTunnel.authkey } }; // TODO: handle failures here request(options).on('error', function (err) { callback('unable to modify score or description:\n' + err); }); } function CbtReporter(logger) { var log = logger.create('cbt-reporter'); function setScore(score, testId) { setScoreOrDescription(testId, 'set_score', score, function (err) { log(err); }); } function setDescription(description, testId) { setScoreOrDescription(testId, 'set_description', description, function (err) { log(err); }); } this.onBrowserComplete = function (browser) { var res = browser.lastResult; var score = res.failed ? 'fail' : 'pass'; var seleniumId = session.activeSessions[browser.id]; log.info('marking test [' + seleniumId + '] as ' + score + 'ed on CBT'); setScore(score, seleniumId); var description = res.success + ' test(s) passed, ' + res.failed + ' test(s) failed'; setDescription(description, seleniumId); }; } module.exports = CbtReporter;