Skip to content

Commit

Permalink
Feat: Throw Error if PERCY_RAISE_ERROR is True (#585)
Browse files Browse the repository at this point in the history
* Feat: Throw Error if PERCY_RAISE_ERROR is True

* Making test message correct

* Throwing error in case of healthcheck failures

* Adding cases to fix test

* Fixing TestCases

* Adding testcases
  • Loading branch information
Amit3200 authored Dec 4, 2024
1 parent c0b6b9d commit bf6a263
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 2 deletions.
22 changes: 20 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,13 @@ async function currentURL(driver, options) {
const percySnapshot = async function percySnapshot(driver, name, options) {
if (!driver) throw new Error('An instance of the selenium driver object is required.');
if (!name) throw new Error('The `name` argument is required.');
if (!(await module.exports.isPercyEnabled())) return;
if (!(await module.exports.isPercyEnabled())) {
if (process.env.PERCY_RAISE_ERROR === 'true') {
throw new Error('Percy is not running, disabling snapshots.');
} else {
return;
}
}
if (utils.percy?.type === 'automate') {
throw new Error('Invalid function call - percySnapshot(). Please use percyScreenshot() function while using Percy with Automate. For more information on usage of percyScreenshot, refer https://www.browserstack.com/docs/percy/integrate/functional-and-visual');
}
Expand All @@ -159,6 +165,9 @@ const percySnapshot = async function percySnapshot(driver, name, options) {
// Handle errors
log.error(`Could not take DOM snapshot "${name}"`);
log.error(error);
if (process.env.PERCY_RAISE_ERROR === 'true') {
throw error;
}
}
};

Expand Down Expand Up @@ -187,7 +196,13 @@ module.exports.percyScreenshot = async function percyScreenshot(driver, name, op

if (!driver) throw new Error('An instance of the selenium driver object is required.');
if (!name) throw new Error('The `name` argument is required.');
if (!(await module.exports.isPercyEnabled())) return;
if (!(await module.exports.isPercyEnabled())) {
if (process.env.PERCY_RAISE_ERROR === 'true') {
throw new Error('Percy is not running, disabling snapshots.');
} else {
return;
}
}
if (utils.percy?.type !== 'automate') {
throw new Error('Invalid function call - percyScreenshot(). Please use percySnapshot() function for taking screenshot. percyScreenshot() should be used only while using Percy with Automate. For more information on usage of PercySnapshot(), refer doc for your language https://www.browserstack.com/docs/percy/integrate/overview');
}
Expand Down Expand Up @@ -226,6 +241,9 @@ module.exports.percyScreenshot = async function percyScreenshot(driver, name, op
// Handle errors
log.error(`Could not take Screenshot "${name}"`);
log.error(error.stack);
if (process.env.PERCY_RAISE_ERROR === 'true') {
throw error;
}
}
};

Expand Down
64 changes: 64 additions & 0 deletions test/index.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ describe('percySnapshot', () => {
});

beforeEach(async () => {
delete process.env.PERCY_RAISE_ERROR;
await helpers.setupTest();
await driver.get(helpers.testSnapshotURL);
});
Expand Down Expand Up @@ -195,6 +196,37 @@ describe('percySnapshot', () => {
expect(setTimeout).toHaveBeenCalled();
delete process.env.RESPONSIVE_CAPTURE_SLEEP_TIME;
});

it('throw error in SDK if PERCY_RAISE_ERROR is true', async () => {
process.env.PERCY_RAISE_ERROR = 'true';
await helpers.test('error', '/percy/healthcheck');
let error = null;
try {
await percySnapshot(driver, 'Snapshot 1');
} catch (e) {
error = e;
}

expect(helpers.logger.stdout).toEqual(jasmine.arrayContaining([
'[percy] Percy is not running, disabling snapshots'
]));
expect(error).toBeInstanceOf(Error);
});

it('handles snapshot failures if PERCY_RAISE_ERROR is true', async () => {
process.env.PERCY_RAISE_ERROR = 'true';
await helpers.test('error', '/percy/snapshot');
let error = null;
try {
await percySnapshot(driver, 'Snapshot 1');
} catch (e) {
error = e;
}
expect(helpers.logger.stderr).toEqual(jasmine.arrayContaining([
'[percy] Could not take DOM snapshot "Snapshot 1"'
]));
expect(error).toBeInstanceOf(Error);
});
});

describe('percyScreenshot', () => {
Expand All @@ -220,6 +252,7 @@ describe('percyScreenshot', () => {
});

beforeEach(async () => {
delete process.env.PERCY_RAISE_ERROR;
await helpers.setupTest();
spyOn(percySnapshot, 'isPercyEnabled').and.returnValue(Promise.resolve(true));
utils.percy.type = 'automate';
Expand Down Expand Up @@ -324,6 +357,37 @@ describe('percyScreenshot', () => {
]));
});

it('throw error in SDK if PERCY_RAISE_ERROR is true', async () => {
process.env.PERCY_RAISE_ERROR = 'true';
spyOn(percySnapshot, 'isPercyEnabled').and.callThrough();
await helpers.test('error', '/percy/healthcheck');
let error = null;
try {
await percyScreenshot(driver, 'Snapshot 1');
} catch (e) {
error = e;
}
expect(helpers.logger.stdout).toEqual(jasmine.arrayContaining([
'[percy] Percy is not running, disabling snapshots'
]));
expect(error).toBeInstanceOf(Error);
});

it('handles snapshot failures if PERCY_RAISE_ERROR is true', async () => {
process.env.PERCY_RAISE_ERROR = 'true';
await helpers.test('error', '/percy/automateScreenshot');
let error = null;
try {
await percyScreenshot(driver, 'Snapshot 1');
} catch (e) {
error = e;
}
expect(helpers.logger.stderr).toEqual(jasmine.arrayContaining([
'[percy] Could not take Screenshot "Snapshot 1"'
]));
expect(error).toBeInstanceOf(Error);
});

it('throws error for web session', async () => {
spyOn(percySnapshot, 'isPercyEnabled').and.returnValue(Promise.resolve(true));
utils.percy.type = 'web';
Expand Down

0 comments on commit bf6a263

Please sign in to comment.