From d23ba9d7a643e6dcce4c303b5069541a16fd4078 Mon Sep 17 00:00:00 2001 From: Pradum Kumar Date: Fri, 27 Oct 2023 11:23:26 +0530 Subject: [PATCH] Sending failed event logs to cli (#194) * Sending failed event logs to cli * sending failed event by post failed event method * adding more info * removing extra line --- index.js | 3 +++ percy/util/postFailedEvents.js | 21 +++++++++++++++++++++ test/index.test.mjs | 6 +++++- 3 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 percy/util/postFailedEvents.js diff --git a/index.js b/index.js index a9c394b..1d6cf21 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,7 @@ 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 +62,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 +107,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..ead0c7a --- /dev/null +++ b/percy/util/postFailedEvents.js @@ -0,0 +1,21 @@ +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, + message: error, + errorKind: 'sdk' + }; + + return await module.exports.request(options); +}; + +module.exports.request = async function request(data) { + try { + await utils.postBuildEvents(data); + } catch {} +}; // 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.'); });