Skip to content

Commit

Permalink
feat: add saucelabs reporter (#213)
Browse files Browse the repository at this point in the history
  • Loading branch information
tsaleksandrova authored Jul 7, 2020
1 parent be34ab4 commit cdec11b
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 1 deletion.
26 changes: 26 additions & 0 deletions docs/config/cloud.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,29 @@ Some additional [SauceLabs specific capabilities](https://wiki.saucelabs.com/dis
* maxDuration - max duration of the whole test suite execution in seconds, default is 30min. Could be extended to 10800sec = 3hours for extremely long tests.
* idleTimeout - max duration for a single interaction. This is the time that the application needs to process the longest interaction like a navigation after a click. Default is 90sec and could be extended to 600sec for extremely slow
applications.

## Test annotations
SauceLabs gives you the option to annotate tests and make their execution logs more comrehensive.
UIVeri5 has a SauceLabs reporter that adds a default set of annotations - spec names, actions, expectation results, suite result, etc.
To enable it, simply add it to the `reporters` configuration:
```js
exports.config = {
reporters: [
{name: './reporter/saucelabsReporter'}
]
}
```

If you want to set a name, tags or CI build number for your test, you can do so in the browser capabilities:
```js
exports.config = {
browsers: [{
capabilities: {
// here you can add details for your saucelabs test execution
name: "my-test",
tags: ["UIVeri5"],
build: process.env.BUILD_NUMBER
}
}]
};
```
2 changes: 1 addition & 1 deletion package-lock.json

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

78 changes: 78 additions & 0 deletions src/reporter/saucelabsReporter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
function JasmineSaucelabsReporter(config, instanceConfig, logger, collector) {
this.config = config;
this.instanceConfig = instanceConfig;
this.logger = logger;
this.collector = collector;
}

JasmineSaucelabsReporter.prototype.suiteStarted = function () {
browser.executeScript('sauce:context=Suite started: ' + this.collector.getCurrentSuite().name);
};

JasmineSaucelabsReporter.prototype.specStarted = function () {
browser.executeScript('sauce:context=Spec started: ' + this.collector.getCurrentSpec().name);
};

JasmineSaucelabsReporter.prototype.specDone = function () {
browser.executeScript('sauce:context=Spec done: ' + this.collector.getCurrentSpec().name);
};

JasmineSaucelabsReporter.prototype.suiteDone = function () {
browser.executeScript('sauce:context=Suite done: ' + this.collector.getCurrentSuite().name);
};

JasmineSaucelabsReporter.prototype.jasmineDone = function () {
var overview = this.collector.getOverview();
browser.executeScript('sauce:job-result=' + overview.status);
};

JasmineSaucelabsReporter.prototype.register = function (jasmineEnv) {
jasmineEnv.addReporter(this);

['click', 'sendKeys'].forEach(this._registerOnAction.bind(this));
this._registerOnExpectation();
};

JasmineSaucelabsReporter.prototype._registerOnAction = function (action) {
var originalAction = protractorModule.parent.parent.exports.WebElement.prototype[action];

protractorModule.parent.parent.exports.WebElement.prototype[action] = function () {
var element = this;
var actionValue = arguments[0];

// TODO: save the locator which was used to find the element
return element.getAttribute('id').then(function (elementId) {
var onAction = function () {
browser.executeScript('sauce:context=Perform action: ' + action + ' with value "' + actionValue + '" on "' + elementId + '"');
};
return originalAction.call(element, actionValue).then(onAction, onAction);
});
};
};

// should be called after browser.getProcessedConfig()
JasmineSaucelabsReporter.prototype._registerOnExpectation = function () {
var originalAddExpectationResult = jasmine.Spec.prototype.addExpectationResult;

jasmine.Spec.prototype.addExpectationResult = function (passed, expectation) {
var sExpectation = 'Expectation ' + (passed ? 'passed' : 'failed') + '.';

if (expectation.matcherName) {
sExpectation += ' Expected "' + expectation.actual + '" ' + expectation.matcherName + ' "' + expectation.expected + '".';
}
if (expectation.message) {
sExpectation += ' Message: "' + expectation.message + '". ';
}
if (expectation.error) {
sExpectation += ' Error: "' + expectation.error + '". ';
}

browser.executeScript('sauce:context=' + sExpectation);

return originalAddExpectationResult.apply(this, arguments);
};
};

module.exports = function (config, instanceConfig, logger, collector) {
return new JasmineSaucelabsReporter(config, instanceConfig, logger, collector);
};

0 comments on commit cdec11b

Please sign in to comment.