Skip to content

Commit

Permalink
Adding Specs
Browse files Browse the repository at this point in the history
  • Loading branch information
Amit3200 committed Oct 1, 2024
1 parent 42aa3b4 commit ce6a65f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 26 deletions.
30 changes: 9 additions & 21 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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,
Expand Down Expand Up @@ -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++;
Expand All @@ -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);
Expand All @@ -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;
}

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
18 changes: 15 additions & 3 deletions test/index.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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}`,
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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 });
Expand Down

0 comments on commit ce6a65f

Please sign in to comment.