Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for viewport option #784

Merged
merged 6 commits into from Oct 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 36 additions & 0 deletions extensions/viewport/viewport.js
@@ -0,0 +1,36 @@
/**
* Provides the --viewport option to set any device resolution and pixel density ratio.
* If the user sets a viewport size as well as a device option (--phone, --tablet, ...),
* we assume that he/she wants to overwrite the device values.
*
* Two syntaxes are supported:
* - 1200x800 will set a 1x pixel density ratio
* - 1200x800x2 will set the given ratio (float values such as 1.5 are accepted)
*/
'use strict';

module.exports = function(phantomas) {

const viewport = phantomas.getParam('viewport');

if (viewport === undefined) {
phantomas.log('No viewport option specified, will use the device default viewport');
return;
}

phantomas.log('Viewport: %s provided', viewport);

const viewportValues = viewport.split('x');
const options = {
width: parseInt(viewportValues[0], 10),
height: parseInt(viewportValues[1], 10),
deviceScaleFactor: parseFloat(viewportValues[2]) || 1
};

phantomas.on('init', async page => {

// @see https://github.com/puppeteer/puppeteer/blob/v1.11.0/docs/api.md#pagesetviewportviewport
await page.setViewport(options);
phantomas.log('page.setViewport() called with options %j', options);
});
};
27 changes: 27 additions & 0 deletions test/integration-spec.yaml
Expand Up @@ -636,6 +636,33 @@
userAgent: 'Mozilla/5.0 (Linux; U; en-us; KFAPWI Build/JDQ39) AppleWebKit/535.19 (KHTML, like Gecko) Silk/3.13 Safari/535.19 Silk-Accelerated=true'
viewport: '1280x800'

# viewport
- url: "/viewport.html"
label: "/viewport.html (with --viewport 1100x700)"
options:
viewport: '1100x700'
metrics:
viewport: '1100x700'
devicePixelRatio: 1

# viewport
- url: "/viewport.html"
label: "/viewport.html (with --viewport 1100x700x2)"
options:
viewport: '1100x700x2'
metrics:
viewport: '1100x700'
devicePixelRatio: 2

# viewport
- url: "/viewport.html"
label: "/viewport.html (with --viewport 1100x700x2.5)"
options:
viewport: '1100x700x2.5'
metrics:
viewport: '1100x700'
devicePixelRatio: 2.5

# https and webfonts
- url: "/https-fonts.html"
options:
Expand Down
9 changes: 9 additions & 0 deletions test/webroot/viewport.html
@@ -0,0 +1,9 @@
<body>
<script>
(phantomas => {
phantomas.setMetric('userAgent', navigator.userAgent);
phantomas.setMetric('viewport', window.innerWidth + 'x' + window.innerHeight);
phantomas.setMetric('devicePixelRatio', window.devicePixelRatio);
})(window.__phantomas);
</script>
</body>