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:
Mickael GOETZ 2017-12-04 23:04:36 +01:00 committed by Andrey Lushnikov
parent 9fc39a4c97
commit 16320b7ac2

View File

@ -614,7 +614,12 @@ class Page extends EventEmitter {
*/
async screenshot(options = {}) {
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);
if (mimeType === 'image/png')
screenshotType = 'png';
@ -622,11 +627,7 @@ class Page extends EventEmitter {
screenshotType = 'jpeg';
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)
screenshotType = 'png';