From c244a33f35dfa5c5ecee7e9963a0902a4d1f1ad7 Mon Sep 17 00:00:00 2001 From: this-is-shivamsingh <140813296+this-is-shivamsingh@users.noreply.github.com> Date: Thu, 16 Nov 2023 15:58:02 +0530 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Implement=20Warning=20+=20Disable?= =?UTF-8?q?=20Percy=20for=20unsupported=20combinations=20(#1411)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * handled error handling cases * handled errorif status is 200 * revert a small change * lint fix * added test cases * lint fix * add coverage * resolved comments * resolved comments --------- Co-authored-by: Chinmay Maheshwari --- .../src/providers/automateProvider.js | 15 +++++++- .../test/providers/automateProvider.test.js | 38 +++++++++++++++++-- 2 files changed, 48 insertions(+), 5 deletions(-) diff --git a/packages/webdriver-utils/src/providers/automateProvider.js b/packages/webdriver-utils/src/providers/automateProvider.js index b3879be28..882b71902 100644 --- a/packages/webdriver-utils/src/providers/automateProvider.js +++ b/packages/webdriver-utils/src/providers/automateProvider.js @@ -83,12 +83,25 @@ export default class AutomateProvider extends GenericProvider { percyBuildUrl: this.buildInfo.url, state: 'begin' }); + // Selenium Hub, set status error Code to 13 if an error is thrown + // Handling error with Selenium dialect is != W3C + if (result?.status === 13) throw new Error(result?.value || 'Got invalid error response'); this._markedPercy = result.success; return result; } catch (e) { log.debug(`[${name}] : Could not mark Automate session as percy`); log.error(`[${name}] : error: ${e.toString()}`); - return null; + /** + * - Handling Error when dialect is W3C + * ERROR response format from SeleniumHUB `{ + * sessionId: ..., + * status: 13, + * value: { error: '', message: ''} + * } + */ + const errResponse = (e?.response?.body && JSON.parse(e?.response?.body)?.value) || {}; + const errMessage = errResponse?.message || errResponse?.error || e?.message || e?.error || e?.value || e.toString(); + throw new Error(errMessage); } }); } diff --git a/packages/webdriver-utils/test/providers/automateProvider.test.js b/packages/webdriver-utils/test/providers/automateProvider.test.js index 1cf3de417..8dcdddbeb 100644 --- a/packages/webdriver-utils/test/providers/automateProvider.test.js +++ b/packages/webdriver-utils/test/providers/automateProvider.test.js @@ -149,20 +149,50 @@ describe('AutomateProvider', () => { spyOn(Driver.prototype, 'getCapabilites'); }); - it('supresses exception and does not throw', async () => { + it('throw error', async () => { const automateProvider = new AutomateProvider('1234', 'https://localhost/command-executor', { platform: 'win' }, {}, 'client', 'environment', {}, percyBuildInfo); await automateProvider.createDriver(); - automateProvider.driver.executeScript = jasmine.createSpy().and.rejectWith(new Error('Random network error')); - await automateProvider.percyScreenshotBegin('abc'); + spyOn(Driver.prototype, 'executeScript').and.returnValue(Promise.reject(new Error('Random network error'))); + await expectAsync(automateProvider.percyScreenshotBegin('abc')).toBeRejectedWithError('Random network error'); }); it('marks the percy session as success', async () => { const automateProvider = new AutomateProvider('1234', 'https://localhost/command-executor', { platform: 'win' }, {}, 'client', 'environment', {}, percyBuildInfo); await automateProvider.createDriver(); - automateProvider.driver.executeScript = jasmine.createSpy().and.returnValue(Promise.resolve({ success: true })); + spyOn(Driver.prototype, 'executeScript').and.returnValue(Promise.resolve({ success: true })); await automateProvider.percyScreenshotBegin('abc'); expect(automateProvider._markedPercy).toBeTruthy(); }); + + it('throw error if statusCode:13', async () => { + const automateProvider = new AutomateProvider('1234', 'https://localhost/command-executor', { platform: 'win' }, {}, {}, 'client', 'environment', {}, percyBuildInfo); + await automateProvider.createDriver(); + spyOn(Driver.prototype, 'executeScript').and.returnValue(Promise.resolve({ status: 13, value: 'OS/Browser/Selenium combination is not supported' })); + await expectAsync(automateProvider.percyScreenshotBegin('abc')).toBeRejectedWithError('OS/Browser/Selenium combination is not supported'); + }); + + it('throw "Got invalid error resposne" if result.value does not exists', async () => { + const automateProvider = new AutomateProvider('1234', 'https://localhost/command-executor', { platform: 'win' }, {}, {}, 'client', 'environment', {}, percyBuildInfo); + await automateProvider.createDriver(); + spyOn(Driver.prototype, 'executeScript').and.returnValue(Promise.resolve({ status: 13 })); + await expectAsync(automateProvider.percyScreenshotBegin('abc')).toBeRejectedWithError('Got invalid error response'); + }); + + it('mark percy sesssion as failure', async () => { + const automateProvider = new AutomateProvider('1234', 'https://localhost/command-executor', { platform: 'win' }, {}, {}, 'client', 'environment', {}, percyBuildInfo); + await automateProvider.createDriver(); + // eslint-disable-next-line prefer-promise-reject-errors + spyOn(Driver.prototype, 'executeScript').and.returnValue(Promise.reject({ response: { body: JSON.stringify({ value: { error: 'OS/Browser/Selenium combination is not supported', message: 'OS/Browser/Selenium combination is not supported' } }) } })); + await expectAsync(automateProvider.percyScreenshotBegin('abc')).toBeRejectedWithError('OS/Browser/Selenium combination is not supported'); + }); + + it('catch direct response error', async () => { + const automateProvider = new AutomateProvider('1234', 'https://localhost/command-executor', { platform: 'win' }, {}, {}, 'client', 'environment', {}, percyBuildInfo); + await automateProvider.createDriver(); + // eslint-disable-next-line prefer-promise-reject-errors + spyOn(Driver.prototype, 'executeScript').and.returnValue(Promise.reject('Random Error')); + await expectAsync(automateProvider.percyScreenshotBegin('abc')).toBeRejectedWithError('Random Error'); + }); }); describe('percyScreenshotEnd', () => {