From b55344f924e649129ac157ac34a19bc01a8f75b4 Mon Sep 17 00:00:00 2001 From: Ravi Sawlani Date: Tue, 3 May 2022 01:56:28 +0530 Subject: [PATCH] Fixed #3132 - Mark test scenarios passed/failed and send reason to Browserstack while using Cucumber as a test-runner (#3133) --- cucumber-js/_setup_cucumber_runner.js | 10 +- lib/index.js | 5 + .../selenium-webdriver/browserstack.js | 40 +++---- test/extra/cucumber-config.js | 15 ++- .../testCucumberWithBrowserstack.js | 100 ++++++++++++++++++ 5 files changed, 147 insertions(+), 23 deletions(-) create mode 100644 test/src/runner/cucumber-integration/testCucumberWithBrowserstack.js diff --git a/cucumber-js/_setup_cucumber_runner.js b/cucumber-js/_setup_cucumber_runner.js index 8cf5e08458..6255382896 100644 --- a/cucumber-js/_setup_cucumber_runner.js +++ b/cucumber-js/_setup_cucumber_runner.js @@ -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(); } diff --git a/lib/index.js b/lib/index.js index 6c599d59e1..741430d129 100644 --- a/lib/index.js +++ b/lib/index.js @@ -131,6 +131,11 @@ Nightwatch.createClient = function({ return client.settings; } }, + transport: { + get: function() { + return client.transport; + } + }, nightwatch_client: { configurable: true, get: function() { diff --git a/lib/transport/selenium-webdriver/browserstack.js b/lib/transport/selenium-webdriver/browserstack.js index 4a21fade8c..9ffb63f853 100644 --- a/lib/transport/selenium-webdriver/browserstack.js +++ b/lib/transport/selenium-webdriver/browserstack.js @@ -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}`)); diff --git a/test/extra/cucumber-config.js b/test/extra/cucumber-config.js index 8d637537d9..df952e2431 100644 --- a/test/extra/cucumber-config.js +++ b/test/extra/cucumber-config.js @@ -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' + } + } + } }; \ No newline at end of file diff --git a/test/src/runner/cucumber-integration/testCucumberWithBrowserstack.js b/test/src/runner/cucumber-integration/testCucumberWithBrowserstack.js new file mode 100644 index 0000000000..e99ac769b3 --- /dev/null +++ b/test/src/runner/cucumber-integration/testCucumberWithBrowserstack.js @@ -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.'); + }); + }); +}); +