diff --git a/index.js b/index.js index 50ad1da..232fad1 100644 --- a/index.js +++ b/index.js @@ -13,12 +13,7 @@ const utils = require('@percy/sdk-utils'); const { DriverMetadata } = require('./driverMetadata'); const log = utils.logger('selenium-webdriver'); -const CDP_SUPPORT_SELENIUM = seleniumPkg.version && !isNaN(seleniumPkg.version.charAt(0)) && parseInt(seleniumPkg.version.charAt(0), 10) >= 4; - -const getWidthsForMultiDOM = (widths, eligibleWidths) => { - let userPassedWidths = []; - if (widths.length !== 0) userPassedWidths = widths; - +const getWidthsForMultiDOM = (userPassedWidths, eligibleWidths) => { // Deep copy of eligible mobile widths let allWidths = []; if (eligibleWidths?.mobile?.length !== 0) { @@ -35,7 +30,7 @@ const getWidthsForMultiDOM = (widths, eligibleWidths) => { async function changeWindowDimensionAndWait(driver, width, height, resizeCount) { try { - if (CDP_SUPPORT_SELENIUM && driver?.capabilities?.browserName === 'chrome') { + if (typeof driver?.executeCdpCommand === 'function' && driver?.capabilities?.browserName === 'chrome') { await driver?.executeCdpCommand('Emulation.setDeviceMetricsOverride', { height, width, @@ -68,20 +63,9 @@ async function captureResponsiveDOM(driver, options) { let currentWidth = windowSize.width; let currentHeight = windowSize.height; let lastWindowWidth = currentWidth; let resizeCount = 0; - // Setup the resizeCount listener if not present /* istanbul ignore next: no instrumenting injected code */ - await driver.executeScript(` - if (!window.resizeCount) { - let resizeTimeout = false; - window.addEventListener('resize', () => { - if (resizeTimeout) clearTimeout(resizeTimeout); - resizeTimeout = setTimeout(() => window.resizeCount++, 100); - }); - } - window.resizeCount = 0; - `); - + await driver.executeScript('PercyDOM.waitForResize()'); for (let width of widths) { if (lastWindowWidth !== width) { resizeCount++; @@ -90,7 +74,7 @@ async function captureResponsiveDOM(driver, options) { } if (process.env.RESPONSIVE_CAPTURE_SLEEP_TIME) { - await new Promise(resolve => setTimeout(resolve, parseInt(process.env.RESPONSIVE_CAPTURE_SLEEP_TIME))); + await new Promise(resolve => setTimeout(resolve, parseInt(process.env.RESPONSIVE_CAPTURE_SLEEP_TIME) * 1000)); } let domSnapshot = await captureSerializedDOM(driver, options); @@ -109,11 +93,15 @@ async function captureSerializedDOM(driver, options) { /* eslint-disable-next-line no-undef */ domSnapshot: PercyDOM.serialize(options) }), options); - domSnapshot.cookies = await driver.manage().getCookies(); + /* istanbul ignore next: no instrumenting injected code */ + domSnapshot.cookies = await driver.manage().getCookies() || {}; return domSnapshot; } function isResponsiveDOMCaptureValid(options) { + if (utils.percy?.config?.percy?.deferUploads) { + return false; + } return options?.responsive_snapshot_capture || options?.responsiveSnapshotCapture || false; } diff --git a/package.json b/package.json index d147405..eac62cf 100644 --- a/package.json +++ b/package.json @@ -30,11 +30,11 @@ "test:types": "tsd" }, "dependencies": { - "@percy/sdk-utils": "^1.29.3", + "@percy/sdk-utils": "^1.29.5-beta.0", "node-request-interceptor": "^0.6.3" }, "devDependencies": { - "@percy/cli": "^1.29.3", + "@percy/cli": "^1.29.5-beta.0", "@types/selenium-webdriver": "^4.0.9", "cross-env": "^7.0.2", "eslint": "^8.27.0", diff --git a/test/index.test.mjs b/test/index.test.mjs index 0b80293..4922c7a 100644 --- a/test/index.test.mjs +++ b/test/index.test.mjs @@ -25,7 +25,8 @@ describe('percySnapshot', () => { width: 1024, height: 768 })) - }) + }), + getCookies: jasmine.createSpy('getCookies').and.returnValue(Promise.resolve({})) }), executeScript: jasmine.createSpy('executeScript').and.returnValue(Promise.resolve(1)), wait: jasmine.createSpy('wait').and.returnValue(Promise.resolve(1)) @@ -101,7 +102,6 @@ describe('percySnapshot', () => { it('posts snapshots to percy server with responsiveSnapshotCapture true', async () => { await driver.manage().window().setRect({ width: 1380, height: 1024 }); await percySnapshot(driver, 'Snapshot 1', { responsiveSnapshotCapture: true, widths: [1380] }); - expect(await helpers.get('logs')).toEqual(jasmine.arrayContaining([ 'Snapshot found: Snapshot 1', `- url: ${helpers.testSnapshotURL}`, @@ -136,6 +136,18 @@ describe('percySnapshot', () => { ])); }); + it('multiDOM should not run when deferUploads is true', async () => { + spyOn(percySnapshot, 'isPercyEnabled').and.returnValue(Promise.resolve(true)); + utils.percy.config = { percy: { deferUploads: true } }; + spyOn(mockedDriver, 'executeCdpCommand').and.callThrough(); + spyOn(mockedDriver.manage().window(), 'setRect').and.callThrough(); + + await percySnapshot(mockedDriver, 'Test Snapshot', { responsiveSnapshotCapture: true }); + + expect(mockedDriver.executeCdpCommand).not.toHaveBeenCalled(); + expect(mockedDriver.manage().window().setRect).not.toHaveBeenCalled(); + }); + it('should call executeCdpCommand for chrome and not setRect', async () => { spyOn(mockedDriver, 'executeCdpCommand').and.callThrough(); spyOn(mockedDriver.manage().window(), 'setRect').and.callThrough(); @@ -177,7 +189,7 @@ describe('percySnapshot', () => { }); it('should wait if RESPONSIVE_CAPTURE_SLEEP_TIME is set', async () => { - process.env.RESPONSIVE_CAPTURE_SLEEP_TIME = '1000'; + process.env.RESPONSIVE_CAPTURE_SLEEP_TIME = 1; spyOn(global, 'setTimeout').and.callThrough(); await percySnapshot(mockedDriver, 'Test Snapshot', { responsiveSnapshotCapture: true });