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

Added type check in percy Snapshot and Screenshot #387

Merged
merged 2 commits into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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');
});
});
Loading