From 1ef090c3ac7f7978c87205ad488beed6b250e6e9 Mon Sep 17 00:00:00 2001 From: Pavel Feldman Date: Tue, 27 Oct 2020 00:02:35 -0700 Subject: [PATCH] fix(screenshot): prioritize passed type over the extension mime (#4251) --- src/client/elementHandle.ts | 6 ++++-- src/client/page.ts | 6 ++++-- test/elementhandle-screenshot.spec.ts | 9 +++++++++ test/page-screenshot.spec.ts | 5 +++++ 4 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/client/elementHandle.ts b/src/client/elementHandle.ts index 4b4af6dfc3..50a5f0622c 100644 --- a/src/client/elementHandle.ts +++ b/src/client/elementHandle.ts @@ -185,8 +185,10 @@ export class ElementHandle extends JSHandle { async screenshot(options: channels.ElementHandleScreenshotOptions & { path?: string } = {}): Promise { return this._wrapApiCall('elementHandle.screenshot', async () => { - const type = determineScreenshotType(options); - const result = await this._elementChannel.screenshot({ ...options, type }); + const copy = { ...options }; + if (!copy.type) + copy.type = determineScreenshotType(options); + const result = await this._elementChannel.screenshot(copy); const buffer = Buffer.from(result.binary, 'base64'); if (options.path) { await mkdirIfNeeded(options.path); diff --git a/src/client/page.ts b/src/client/page.ts index 579c35d564..b5d21beefc 100644 --- a/src/client/page.ts +++ b/src/client/page.ts @@ -457,8 +457,10 @@ export class Page extends ChannelOwner { return this._wrapApiCall('page.screenshot', async () => { - const type = determineScreenshotType(options); - const result = await this._channel.screenshot({ ...options, type }); + const copy = { ...options }; + if (!copy.type) + copy.type = determineScreenshotType(options); + const result = await this._channel.screenshot(copy); const buffer = Buffer.from(result.binary, 'base64'); if (options.path) { await mkdirIfNeeded(options.path); diff --git a/test/elementhandle-screenshot.spec.ts b/test/elementhandle-screenshot.spec.ts index 0fdc616e41..3f6248c9b8 100644 --- a/test/elementhandle-screenshot.spec.ts +++ b/test/elementhandle-screenshot.spec.ts @@ -393,4 +393,13 @@ describe('element screenshot', (suite, parameters) => { await elementHandle.screenshot({path: outputPath}); expect(await fs.promises.readFile(outputPath)).toMatchSnapshot('screenshot-element-bounding-box.png'); }); + + it('should prefer type over extension', async ({page, server}) => { + await page.setViewportSize({width: 500, height: 500}); + await page.goto(server.PREFIX + '/grid.html'); + await page.evaluate(() => window.scrollBy(50, 100)); + const elementHandle = await page.$('.box:nth-of-type(3)'); + const buffer = await elementHandle.screenshot({ path: 'file.png', type: 'jpeg' }); + expect([buffer[0], buffer[1], buffer[2]]).toEqual([0xFF, 0xD8, 0xFF]); + }); }); diff --git a/test/page-screenshot.spec.ts b/test/page-screenshot.spec.ts index bc024235a3..4a87dea021 100644 --- a/test/page-screenshot.spec.ts +++ b/test/page-screenshot.spec.ts @@ -292,6 +292,11 @@ describe('page screenshot', (suite, { browserName, headful }) => { expect(error.message).toContain('path: unsupported mime type "text/plain"'); }); + it('should prefer type over extension', async ({page}) => { + const buffer = await page.screenshot({ path: 'file.png', type: 'jpeg' }); + expect([buffer[0], buffer[1], buffer[2]]).toEqual([0xFF, 0xD8, 0xFF]); + }); + it('should work with large size', (test, { browserName }) => { test.fail(browserName === 'chromium', 'Upstream Chromium bug'); }, async ({ page }) => {