From c55f8198a63f8ed3ac00b6cf5da300f8c7207c47 Mon Sep 17 00:00:00 2001 From: prklm10 Date: Fri, 20 Oct 2023 11:18:51 +0530 Subject: [PATCH 1/4] Sending failed event logs to cli --- index.js | 4 ++++ percy/util/postFailedEvents.js | 18 ++++++++++++++++++ test/index.test.mjs | 6 +++++- 3 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 percy/util/postFailedEvents.js diff --git a/index.js b/index.js index a9c394b..a9f46b8 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,8 @@ const { AppiumDriver } = require('./percy/driver/driverWrapper'); const { ProviderResolver } = require('./percy/providers/providerResolver'); const { TimeIt } = require('./percy/util/timing'); + +const postFailedEvents = require('./percy/util/postFailedEvents'); const percyOnAutomate = require('./percy/percyOnAutomate'); const log = require('./percy/util/log'); @@ -61,6 +63,7 @@ module.exports = async function percyScreenshot(driver, name, options = {}) { [driver, name] = [browser, driver]; } catch (e) { // ReferenceError: browser is not defined. driver = undefined; + await postFailedEvents(e); } }; if (!driver) throw new Error('The WebdriverIO `browser` object or wd `driver` object is required.'); @@ -105,6 +108,7 @@ module.exports = async function percyScreenshot(driver, name, options = {}) { } catch (e) { log.error(`[${name}] failed to take screenshot`); log.debug(`[${name}] ${e}, \n ${e.stack}`); + await postFailedEvents(e); if (!(await driver.getPercyOptions()).ignoreErrors) throw e; } }); diff --git a/percy/util/postFailedEvents.js b/percy/util/postFailedEvents.js new file mode 100644 index 0000000..3bf2384 --- /dev/null +++ b/percy/util/postFailedEvents.js @@ -0,0 +1,18 @@ +const utils = require('@percy/sdk-utils'); + +// Collect client and environment information +const sdkPkg = require('../../package.json'); +const CLIENT_INFO = `${sdkPkg.name.split('/')[1]}-js/${sdkPkg.version}`; + +module.exports = async function postFailedEvents(error) { + let options = { + clientInfo: CLIENT_INFO, + errorMessage: error + }; + + return await module.exports.request(options); +}; + +module.exports.request = async function request(data) { + await utils.request.post('/percy/events', data); +}; // To mock in test case diff --git a/test/index.test.mjs b/test/index.test.mjs index eee86ad..8cb966d 100644 --- a/test/index.test.mjs +++ b/test/index.test.mjs @@ -5,6 +5,7 @@ import wdioDriver from './mocks/appium/wdio_driver.js'; import { Cache } from '../percy/util/cache.js'; import utils from '@percy/sdk-utils'; import percyOnAutomate from '../percy/percyOnAutomate.js'; +import postFailedEvents from '../percy/util/postFailedEvents.js'; describe('percyScreenshot', () => { let driver; @@ -25,6 +26,7 @@ describe('percyScreenshot', () => { }); it('throws an error when a driver is not provided', async () => { + spyOn(postFailedEvents, 'request').and.callFake(() => {}); await expectAsync(percyScreenshot()) .toBeRejectedWithError('The WebdriverIO `browser` object or wd `driver` object is required.'); }); @@ -52,8 +54,8 @@ describe('percyScreenshot', () => { describe('errors', () => { describe('with percy:options.ignoreErrors false', () => { it('logs errors if any', async () => { + spyOn(postFailedEvents, 'request').and.callFake(() => {}); driver.takeScreenshot = jasmine.createSpy().and.throwError(new Error('Screenshot failed')); - await expectAsync(percyScreenshot(driver, 'Screenshot 1')) .toBeRejectedWithError('Screenshot failed'); }); @@ -62,6 +64,7 @@ describe('percyScreenshot', () => { describe('with percy:options.ignoreErrors true', () => { it('logs errors if any', async () => { driver = wdDriver({ ignoreErrors: true }); + spyOn(postFailedEvents, 'request').and.callFake(() => {}); driver.takeScreenshot = jasmine.createSpy().and.throwError(new Error('Screenshot failed')); await percyScreenshot(driver, 'Screenshot 1'); @@ -73,6 +76,7 @@ describe('percyScreenshot', () => { describe('wdio standalone context', () => { describe('with browser not defined', () => { it('throws an error when a driver is not provided', async () => { + spyOn(postFailedEvents, 'request').and.callFake(() => {}); await expectAsync(percyScreenshot('Screenshot 1')) .toBeRejectedWithError('The WebdriverIO `browser` object or wd `driver` object is required.'); }); From 59d7f863d58ce41e247a4ccc66cdc135a4670439 Mon Sep 17 00:00:00 2001 From: prklm10 Date: Fri, 20 Oct 2023 15:45:55 +0530 Subject: [PATCH 2/4] sending failed event by post failed event method --- percy/util/postFailedEvents.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/percy/util/postFailedEvents.js b/percy/util/postFailedEvents.js index 3bf2384..3f4029f 100644 --- a/percy/util/postFailedEvents.js +++ b/percy/util/postFailedEvents.js @@ -14,5 +14,7 @@ module.exports = async function postFailedEvents(error) { }; module.exports.request = async function request(data) { - await utils.request.post('/percy/events', data); + try { + await utils.postFailedEvent(data); + } catch {} }; // To mock in test case From a96724fe4dd856504e346d420994e0f50c928753 Mon Sep 17 00:00:00 2001 From: prklm10 Date: Wed, 25 Oct 2023 11:54:37 +0530 Subject: [PATCH 3/4] adding more info --- percy/util/postFailedEvents.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/percy/util/postFailedEvents.js b/percy/util/postFailedEvents.js index 3f4029f..ead0c7a 100644 --- a/percy/util/postFailedEvents.js +++ b/percy/util/postFailedEvents.js @@ -7,7 +7,8 @@ const CLIENT_INFO = `${sdkPkg.name.split('/')[1]}-js/${sdkPkg.version}`; module.exports = async function postFailedEvents(error) { let options = { clientInfo: CLIENT_INFO, - errorMessage: error + message: error, + errorKind: 'sdk' }; return await module.exports.request(options); @@ -15,6 +16,6 @@ module.exports = async function postFailedEvents(error) { module.exports.request = async function request(data) { try { - await utils.postFailedEvent(data); + await utils.postBuildEvents(data); } catch {} }; // To mock in test case From c379382f0f6799cd5bfefb6b4ebf243c10a22364 Mon Sep 17 00:00:00 2001 From: prklm10 Date: Wed, 25 Oct 2023 12:05:50 +0530 Subject: [PATCH 4/4] removing extra line --- index.js | 1 - 1 file changed, 1 deletion(-) diff --git a/index.js b/index.js index a9f46b8..1d6cf21 100644 --- a/index.js +++ b/index.js @@ -1,7 +1,6 @@ const { AppiumDriver } = require('./percy/driver/driverWrapper'); const { ProviderResolver } = require('./percy/providers/providerResolver'); const { TimeIt } = require('./percy/util/timing'); - const postFailedEvents = require('./percy/util/postFailedEvents'); const percyOnAutomate = require('./percy/percyOnAutomate');