Skip to content

Commit

Permalink
Fix for chrome cdp command execution
Browse files Browse the repository at this point in the history
  • Loading branch information
chinmay-browserstack committed Oct 4, 2024
1 parent 9586d3b commit 86a8b43
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 17 deletions.
9 changes: 5 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ const getWidthsForMultiDOM = (userPassedWidths, eligibleWidths) => {

async function changeWindowDimensionAndWait(driver, width, height, resizeCount) {
try {
if (typeof driver?.executeCdpCommand === 'function' && driver?.capabilities?.browserName === 'chrome') {
await driver?.executeCdpCommand('Emulation.setDeviceMetricsOverride', {
const caps = await driver.getCapabilities();
if (typeof driver?.sendDevToolsCommand === 'function' && caps.getBrowserName() === 'chrome') {
await driver?.sendDevToolsCommand('Emulation.setDeviceMetricsOverride', {
height,
width,
deviceScaleFactor: 1,
Expand All @@ -41,7 +42,7 @@ async function changeWindowDimensionAndWait(driver, width, height, resizeCount)
await driver.manage().window().setRect({ width, height });
}
} catch (e) {
log.warn(`Resizing using CDP failed, falling back to driver resize for width ${width}`, e);
log.debug(`Resizing using CDP failed, falling back to driver resize for width ${width}`, e);
await driver.manage().window().setRect({ width, height });
}

Expand All @@ -51,7 +52,7 @@ async function changeWindowDimensionAndWait(driver, width, height, resizeCount)
await driver.executeScript('return window.resizeCount') === resizeCount;
}, 1000);
} catch (e) {
log.warn(`Error while window resize event for width ${width}`, e);
log.debug(`Timed out waiting for window resize event for width ${width}`, e);
}
}

Expand Down
24 changes: 11 additions & 13 deletions test/index.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,8 @@ describe('percySnapshot', () => {
.forBrowser('firefox').build();

mockedDriver = {
capabilities: {
browserName: 'chrome'
},
executeCdpCommand: jasmine.createSpy('executeCdpCommand').and.returnValue(Promise.resolve()),
getCapabilities: jasmine.createSpy('sendDevToolsCommand').and.returnValue({ getBrowserName: () => 'chrome'}),
sendDevToolsCommand: jasmine.createSpy('sendDevToolsCommand').and.returnValue(Promise.resolve()),
manage: jasmine.createSpy('manage').and.returnValue({
window: jasmine.createSpy('window').and.returnValue({
setRect: jasmine.createSpy('setRect').and.returnValue(Promise.resolve()),
Expand Down Expand Up @@ -139,23 +137,23 @@ 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, 'sendDevToolsCommand').and.callThrough();
spyOn(mockedDriver.manage().window(), 'setRect').and.callThrough();

await percySnapshot(mockedDriver, 'Test Snapshot', { responsiveSnapshotCapture: true });

expect(mockedDriver.executeCdpCommand).not.toHaveBeenCalled();
expect(mockedDriver.sendDevToolsCommand).not.toHaveBeenCalled();
expect(mockedDriver.manage().window().setRect).not.toHaveBeenCalled();
});

it('should call executeCdpCommand for chrome and not setRect', async () => {
spyOn(mockedDriver, 'executeCdpCommand').and.callThrough();
it('should call sendDevToolsCommand for chrome and not setRect', async () => {
spyOn(mockedDriver, 'sendDevToolsCommand').and.callThrough();
spyOn(mockedDriver.manage().window(), 'setRect').and.callThrough();
utils.percy.widths = { mobile: [], widths: [1280] };

await percySnapshot(mockedDriver, 'Test Snapshot', { responsiveSnapshotCapture: true });

expect(mockedDriver.executeCdpCommand).toHaveBeenCalledWith('Emulation.setDeviceMetricsOverride', {
expect(mockedDriver.sendDevToolsCommand).toHaveBeenCalledWith('Emulation.setDeviceMetricsOverride', {
height: jasmine.any(Number),
width: jasmine.any(Number),
deviceScaleFactor: 1,
Expand All @@ -164,23 +162,23 @@ describe('percySnapshot', () => {
expect(mockedDriver.manage().window().setRect).not.toHaveBeenCalled();
});

it('should fall back to setRect when executeCdpCommand fails', async () => {
it('should fall back to setRect when sendDevToolsCommand fails', async () => {
const windowManager = mockedDriver.manage().window();
spyOn(mockedDriver, 'executeCdpCommand').and.rejectWith(new Error('CDP Command Failed'));
spyOn(mockedDriver, 'sendDevToolsCommand').and.rejectWith(new Error('CDP Command Failed'));
spyOn(windowManager, 'setRect').and.callThrough();
utils.percy.widths = { mobile: [1125], widths: [375, 1280] };

await percySnapshot(mockedDriver, 'Test Snapshot', { responsiveSnapshotCapture: true });

expect(mockedDriver.executeCdpCommand).toHaveBeenCalled();
expect(mockedDriver.sendDevToolsCommand).toHaveBeenCalled();
expect(windowManager.setRect).toHaveBeenCalledWith({
width: jasmine.any(Number),
height: jasmine.any(Number)
});
});

it('should log a timeout error when resizeCount fails', async () => {
spyOn(mockedDriver, 'executeCdpCommand').and.rejectWith(new Error('TimeoutError'));
spyOn(mockedDriver, 'sendDevToolsCommand').and.rejectWith(new Error('TimeoutError'));
utils.percy.widths = { mobile: [1125], widths: [375, 1280, 1280] };

await percySnapshot(mockedDriver, 'Test Snapshot', { responsiveSnapshotCapture: true });
Expand Down

0 comments on commit 86a8b43

Please sign in to comment.