Skip to content

Commit

Permalink
fix(Page.screenshot): prioritize screenshot type over filename extens…
Browse files Browse the repository at this point in the history
…ion (#1526)

For the `page.screenshot` command, this patch starts prioritizing `options.type` option over the type inferred from `options.path`

Fixes #1492.
  • Loading branch information
MrMitch authored and aslushnikov committed Dec 4, 2017
1 parent 9fc39a4 commit 16320b7
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions lib/Page.js
Expand Up @@ -614,19 +614,20 @@ 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';
else if (mimeType === 'image/jpeg')
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';

Expand Down

0 comments on commit 16320b7

Please sign in to comment.