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', () => {