fix(Page.screenshot): prioritize screenshot type over filename extension (#1526)
For the `page.screenshot` command, this patch starts prioritizing `options.type` option over the type inferred from `options.path` Fixes #1492.
This commit is contained in:
parent
9fc39a4c97
commit
16320b7ac2
13
lib/Page.js
13
lib/Page.js
@ -614,7 +614,12 @@ class Page extends EventEmitter {
|
|||||||
*/
|
*/
|
||||||
async screenshot(options = {}) {
|
async screenshot(options = {}) {
|
||||||
let screenshotType = null;
|
let screenshotType = null;
|
||||||
if (options.path) {
|
// options.type takes precedence over inferring the type from options.path
|
||||||
|
// because it may be a 0-length file with no extension created beforehand (i.e. as a temp file).
|
||||||
|
if (options.type) {
|
||||||
|
console.assert(options.type === 'png' || options.type === 'jpeg', 'Unknown options.type value: ' + options.type);
|
||||||
|
screenshotType = options.type;
|
||||||
|
} else if (options.path) {
|
||||||
const mimeType = mime.lookup(options.path);
|
const mimeType = mime.lookup(options.path);
|
||||||
if (mimeType === 'image/png')
|
if (mimeType === 'image/png')
|
||||||
screenshotType = 'png';
|
screenshotType = 'png';
|
||||||
@ -622,11 +627,7 @@ class Page extends EventEmitter {
|
|||||||
screenshotType = 'jpeg';
|
screenshotType = 'jpeg';
|
||||||
console.assert(screenshotType, 'Unsupported screenshot mime type: ' + mimeType);
|
console.assert(screenshotType, 'Unsupported screenshot mime type: ' + mimeType);
|
||||||
}
|
}
|
||||||
if (options.type) {
|
|
||||||
console.assert(!screenshotType || options.type === screenshotType, `Passed screenshot type '${options.type}' does not match the type inferred from the file path: '${screenshotType}'`);
|
|
||||||
console.assert(options.type === 'png' || options.type === 'jpeg', 'Unknown options.type value: ' + options.type);
|
|
||||||
screenshotType = options.type;
|
|
||||||
}
|
|
||||||
if (!screenshotType)
|
if (!screenshotType)
|
||||||
screenshotType = 'png';
|
screenshotType = 'png';
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user