Skip to content

Commit

Permalink
Fixed #3132 - Mark test scenarios passed/failed and send reason to Br…
Browse files Browse the repository at this point in the history
…owserstack while using Cucumber as a test-runner (#3133)
  • Loading branch information
gravityvi authored May 2, 2022
1 parent 1518709 commit b55344f
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 23 deletions.
10 changes: 9 additions & 1 deletion cucumber-js/_setup_cucumber_runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,15 @@ Before(function({pickle}) {
}
});

After(async function() {
After(async function(testCase) {

//send test-case result to cloud provider
const {result} = testCase;
if (this.client && result) {
const error = result.status === 'FAILED' ? new Error(result.message) : null;
await this.client.transport.testSuiteFinished(error);
}

if (this.browser) {
await this.browser.quit();
}
Expand Down
5 changes: 5 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,11 @@ Nightwatch.createClient = function({
return client.settings;
}
},
transport: {
get: function() {
return client.transport;
}
},
nightwatch_client: {
configurable: true,
get: function() {
Expand Down
40 changes: 20 additions & 20 deletions lib/transport/selenium-webdriver/browserstack.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,29 +98,29 @@ class Browserstack extends Selenium {
}
}

async testSuiteFinished(failures) {
try {
let reason = failures instanceof Error;
if (reason) {
reason = stripAnsi(`${failures.name}: ${failures.message}`);
} else {
reason = '';
async sendReasonToBrowserstack(isFailure = false, reason = '') {
reason = stripAnsi(reason);
await this.sendHttpRequest({
url: `${Browserstack.ApiUrl}/sessions/${this.sessionId}.json`,
method: 'PUT',
use_ssl: true,
data: {
status: isFailure ? 'failed' : 'passed',
reason
},
auth: {
user: this.username,
pass: this.accessKey
}
});

await this.sendHttpRequest({
url: `${Browserstack.ApiUrl}/sessions/${this.sessionId}.json`,
method: 'PUT',
use_ssl: true,
data: {
status: !!failures ? 'failed' : 'passed',
reason
},
auth: {
user: this.username,
pass: this.accessKey
}
});
}

async testSuiteFinished(failures) {

try {
const reason = failures instanceof Error ? `${failures.name}: ${failures.message}` : '';
await this.sendReasonToBrowserstack(!!failures, reason);
// eslint-disable-next-line no-console
console.log('\n ' + 'See more info, video, & screenshots on Browserstack:\n' +
' ' + Logger.colors.light_cyan(`https://automate.browserstack.com/builds/${this.buildId}/sessions/${this.sessionId}`));
Expand Down
15 changes: 13 additions & 2 deletions test/extra/cucumber-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,17 @@ module.exports = {
feature_path: path.join(__dirname, '../cucumber-integration-tests/sample_cucumber_tests/integration/sample.feature')
}
},
output: false,
silent: false
test_settings: {
browserstack: {
selenium: {
host: 'hub-cloud.browserstack.com',
port: 443
},
desiredCapabilities: {
'browserstack.user': 'test-access-user',
'browserstack.key': 'test-access-key',
browserName: 'chrome'
}
}
}
};
100 changes: 100 additions & 0 deletions test/src/runner/cucumber-integration/testCucumberWithBrowserstack.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
const path = require('path');
const assert = require('assert');
const nock = require('nock');
const common = require('../../../common.js');
const {runTests} = common.require('index.js');

xdescribe('Cucumber Browserstack integration', function() {

before(function(done) {
try {
nock.activate();
// eslint-disable-next-line no-empty
} catch (err) {
}

nock('https://hub-cloud.browserstack.com')
.post('/wd/hub/session')
.reply(201, {
status: 0,
sessionId: '1352110219202',
value: {
browserName: 'chrome'
}
});

nock('https://api.browserstack.com')
.get('/automate/builds.json')
.reply(200, []);

nock('https://hub-cloud.browserstack.com')
.post('/wd/hub/session/1352110219202/url')
.reply(200, {
value: null
});

nock('https://hub-cloud.browserstack.com')
.post('/wd/hub/session/1352110219202/elements')
.reply(200, {
status: 0,
sessionId: '1352110219202',
value: [{
'ELEMENT': '0'
}]
});

nock('https://hub-cloud.browserstack.com')
.delete('/wd/hub/session/1352110219202')
.reply(200, {
value: []
});

done();
});

after(function(done) {
nock.restore();
done();
});


it('testCucumberSampleTests - [Passed]', function() {

nock('https://api.browserstack.com')
.put('/automate/sessions/1352110219202.json', body => body.status === 'passed')
.reply(200);

const source = [path.join(__dirname, '../../../cucumber-integration-tests/sample_cucumber_tests/integration/testSample.js')];

return runTests({
source,
tags: ['@pass'],
verbose: false,
config: path.join(__dirname, '../../../extra/cucumber-config.js'),
env: 'browserstack'
}, {}).then(failures => {
assert.strictEqual(failures, false, 'Cucumber has test failures. Run with verbose to investigate.');
});
});

it('testCucumberSampleTests with failures', function() {


nock('https://api.browserstack.com')
.put('/automate/sessions/1352110219202.json', body => body.status === 'failed')
.reply(200);

const source = [path.join(__dirname, '../../../cucumber-integration-tests/sample_cucumber_tests/integration/testWithFailures.js')];

return runTests({
source,
env: 'browserstack',
tags: ['@fail'],
verbose: false,
config: path.join(__dirname, '../../../extra/cucumber-config.js')
}, {}).then(failures => {
assert.strictEqual(failures, true, 'Cucumber tests should have failed. Run with verbose to investigate.');
});
});
});

0 comments on commit b55344f

Please sign in to comment.