Skip to content

Commit

Permalink
Sending failed event logs to cli (#194)
Browse files Browse the repository at this point in the history
* Sending failed event logs to cli

* sending failed event by post failed event method

* adding more info

* removing extra line
  • Loading branch information
prklm10 authored Oct 27, 2023
1 parent f180ebf commit d23ba9d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
3 changes: 3 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -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');
Expand Down Expand Up @@ -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.');
Expand Down Expand Up @@ -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;
}
});
Expand Down
21 changes: 21 additions & 0 deletions percy/util/postFailedEvents.js
Original file line number Diff line number Diff line change
@@ -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
6 changes: 5 additions & 1 deletion test/index.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.');
});
Expand Down Expand Up @@ -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');
});
Expand All @@ -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');
Expand All @@ -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.');
});
Expand Down

0 comments on commit d23ba9d

Please sign in to comment.