Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sending failed event logs to cli #194

Merged
merged 5 commits into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading