From 2e3385203f89a2eda1798f9bc8e3ad573aec372c Mon Sep 17 00:00:00 2001 From: Lars-Dominik Braun Date: Tue, 18 Jun 2019 17:18:31 +0200 Subject: behavior: Fix screenshots MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Chrome’s behavior wrt screeshots changed in some version, so now artificially extending the viewport via device metrics is required. --- crocoite/behavior.py | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) (limited to 'crocoite') diff --git a/crocoite/behavior.py b/crocoite/behavior.py index d079603..bdae5f9 100644 --- a/crocoite/behavior.py +++ b/crocoite/behavior.py @@ -281,13 +281,26 @@ class Screenshot (Behavior): name = 'screenshot' - # see https://github.com/GoogleChrome/puppeteer/blob/230be28b067b521f0577206899db01f0ca7fc0d2/examples/screenshots-longpage.js # Hardcoded max texture size of 16,384 (crbug.com/770769) maxDim = 16*1024 async def onfinish (self): tab = self.loader.tab + # this is required to make the browser render more than just the small + # actual viewport (i.e. entire page). see + # https://github.com/GoogleChrome/puppeteer/blob/45873ea737b4ebe4fa7d6f46256b2ea19ce18aa7/lib/Page.js#L805 + metrics = await tab.Page.getLayoutMetrics () + contentSize = metrics['contentSize'] + + await tab.Emulation.setDeviceMetricsOverride ( + width=0, height=0, deviceScaleFactor=0, mobile=False, + viewport={'x': 0, + 'y': 0, + 'width': contentSize['width'], + 'height': contentSize['height'], + 'scale': 1}) + tree = await tab.Page.getFrameTree () try: url = URL (tree['frameTree']['frame']['url']).with_fragment (None) @@ -295,9 +308,6 @@ class Screenshot (Behavior): self.logger.error ('frame without url', tree=tree) url = None - - metrics = await tab.Page.getLayoutMetrics () - contentSize = metrics['contentSize'] width = min (contentSize['width'], self.maxDim) # we’re ignoring horizontal scroll intentionally. Most horizontal # layouts use JavaScript scrolling and don’t extend the viewport. @@ -308,6 +318,8 @@ class Screenshot (Behavior): data = b64decode (ret['data']) yield ScreenshotEvent (url, yoff, data) + await tab.Emulation.clearDeviceMetricsOverride () + class Click (JsOnload): """ Generic link clicking """ -- cgit v1.2.3