diff --git a/index.js b/index.js index 0ed3bac..eb30385 100644 --- a/index.js +++ b/index.js @@ -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 @@ -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; @@ -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(); +}; diff --git a/test/index.test.mjs b/test/index.test.mjs index e11b305..ea8f496 100644 --- a/test/index.test.mjs +++ b/test/index.test.mjs @@ -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', () => { @@ -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', () => { @@ -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 () => { @@ -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'); @@ -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'); + }); });