Skip to content

Commit

Permalink
Added type check in percy Snapshot and Screenshot
Browse files Browse the repository at this point in the history
  • Loading branch information
chinmay-browserstack committed Sep 22, 2023
1 parent d1d40cf commit aa44223
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
16 changes: 14 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@ const withDefaultInterceptors = require('node-request-interceptor/lib/presets/de
module.exports = 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 utils.isPercyEnabled())) return;
if (!(await module.exports.isPercyEnabled())) return;
let log = utils.logger('selenium-webdriver');
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://docs.percy.io/docs/integrate-functional-testing-with-visual-testing');
}

try {
// Inject the DOM serialization script
Expand Down Expand Up @@ -64,8 +67,11 @@ 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 utils.isPercyEnabled())) return;
if (!(await module.exports.isPercyEnabled())) return;
let log = utils.logger('selenium-webdriver');
if (utils.percy?.type === 'web') {
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://docs.percy.io/docs/end-to-end-testing');
}

try {
let sessionId, capabilities, commandExecutorUrl;
Expand Down Expand Up @@ -123,3 +129,9 @@ module.exports.percyScreenshot = async function percyScreenshot(driver, name, op
log.error(error.stack);
}
};

// jasmine cannot mock individual functions, hence adding isPercyEnabled to the exports object
// also need to define this at the end of the file or else default exports will over-ride this
module.exports.isPercyEnabled = async function isPercyEnabled() {
return await utils.isPercyEnabled();
};
30 changes: 30 additions & 0 deletions test/index.test.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import webdriver from 'selenium-webdriver';
import helpers from '@percy/sdk-utils/test/helpers';
import percySnapshot from '../index.js';
import utils from '@percy/sdk-utils';
const { percyScreenshot } = percySnapshot;

describe('percySnapshot', () => {
Expand Down Expand Up @@ -62,6 +63,19 @@ describe('percySnapshot', () => {
'[percy] Could not take DOM snapshot "Snapshot 1"'
]));
});

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

let error = null;
try {
await percySnapshot(driver, 'Snapshot 2');
} catch (e) {
error = e.message;
}
expect(error).toEqual('Invalid function call - percySnapshot(). Please use percyScreenshot() function while using Percy with Automate. For more information on usage of percyScreenshot, refer https://docs.percy.io/docs/integrate-functional-testing-with-visual-testing');
});
});

describe('percyScreenshot', () => {
Expand All @@ -88,6 +102,8 @@ describe('percyScreenshot', () => {

beforeEach(async () => {
await helpers.setupTest();
spyOn(percySnapshot, 'isPercyEnabled').and.returnValue(Promise.resolve(true));
utils.percy.type = 'automate';
});

it('throws an error when a driver is not provided', async () => {
Expand All @@ -101,6 +117,7 @@ describe('percyScreenshot', () => {
});

it('disables snapshots when the healthcheck fails', async () => {
spyOn(percySnapshot, 'isPercyEnabled').and.callThrough();
await helpers.test('error', '/percy/healthcheck');

await percyScreenshot(driver, 'Snapshot 1');
Expand Down Expand Up @@ -174,4 +191,17 @@ describe('percyScreenshot', () => {
'[percy] Could not take Screenshot "Snapshot 1"'
]));
});

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

let error = null;
try {
await percyScreenshot(driver, 'Snapshot 2');
} catch (e) {
error = e.message;
}
expect(error).toEqual('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://docs.percy.io/docs/end-to-end-testing');
});
});

0 comments on commit aa44223

Please sign in to comment.