Open Bug 1901211 Opened 2 years ago Updated 7 months ago

setViewport hangs if the page is zoomed

Categories

(Remote Protocol :: WebDriver BiDi, defect, P2)

defect
Points:
3

Tracking

(Not tracked)

People

(Reporter: alexrudenko, Unassigned)

References

Details

(Whiteboard: [webdriver:backlog])

Attachments

(2 external files)

Only on Windows and Linux:

import puppeteer from "puppeteer";

const browser = await puppeteer.launch({
  product: 'firefox',
  protocol: 'webDriverBiDi',
  headless: false,
});

const page = await browser.newPage();

await page.goto('https://example.com');

await page.keyboard.down('Control');
await page.mouse.wheel({deltaY: -100});
await page.keyboard.up('Control');


console.log('setting viewport');

await page.setViewport({
  width: 400,
  height: 400,
});

console.log('done');

await browser.close();

Expected:
The script resolves.

Actual:
The script hangs when setting the viewport.
Underlying command is

{"id":11,"method":"browsingContext.setViewport","params":{"context":"9fdd7636-2a04-411b-a0f7-2fcacd64cf01","viewport":{"width":400,"height":400},"devicePixelRatio":null}}

This is an issue as noticed on https://github.com/puppeteer/puppeteer/pull/12547. So we have basically two issues here:

  1. Given that the BiDi spec doesn't have support for full zoom we may want to disable full zoom until we can make sure to handle viewport sizing correctly. This can be done by setting browser.zoom.full to false.

  2. If we want to keep it enabled (which would cause breakage for this mentioned test) we should at least limit the feature to the current tab to prevent side-effects. By default the feature is site-specific and as such would be applied cross different tabs if the same origin is loaded. We can turn it off with the preference browser.zoom.siteSpecific set to false.

I would prefer option 1 and disable it for now.

Oh, and browser.zoom.siteSpecific is a static pref and needs to be set before Firefox launches, while browser.zoom.full can be changed at any time.

Practically speaking, the issue is that the setViewport command hangs forever while waiting for window dimensions to match the arguments from the user. But when a zoom is set, the reported window innerHeight/innerWidth are affected by the zoom value so we will never match the values provided by the client.

An easy solution here is just to factor in the zoom before calling _awaitViewportDimensions. We can simply divide the expected width/height by the context.fullZoom value. Note that we need to account for rounding issues in that case.

I'm a bit reluctant to disable the fullZoom behavior when webdriver is enabled only because of the hang with setViewport.

Assignee: nobody → jdescottes
Status: NEW → ASSIGNED

Hi Emilio,

At the moment when we resize the viewport using WebDriver BiDi, we set the height/width on the browser element and then we wait until the content window's innerHeight/Width match the expected height/width.

However when the browsingContext has fullZoom != 1, those values don't match.

First I tried to divide the expected width/height by the context's fullZoom. It works in some cases (eg fullzoom 0.5, 1.5, 2 etc...) but fails in other cases (eg 1.33). I realized that the effective zoom does not always match the requested zoom, and we do have deviceFullZoomForTest available at https://searchfox.org/mozilla-central/source/docshell/base/nsIDocumentViewer.idl#271 which gives us the actual zoom value we can use for our tests.

However, it's labeled as "test only" so I'm not sure it's fine for us to use? If this is not ok to rely on do you have any other suggestion on how we could wait for the content window to have been correctly resized after updating the browser element's width and height?

Thanks!

Flags: needinfo?(emilio)

The right thing to use is just window.devicePixelRatio. If you need pixel differences between the parent window and the page you can use device pixels as the common unit, something like this:

diff --git a/remote/cdp/domains/content/Emulation.sys.mjs b/remote/cdp/domains/content/Emulation.sys.mjs
index 41bb0c76ea22b..8a6c0683f5da4 100644
--- a/remote/cdp/domains/content/Emulation.sys.mjs
+++ b/remote/cdp/domains/content/Emulation.sys.mjs
@@ -31,7 +31,8 @@ export class Emulation extends ContentProcessDomain {
     await lazy.AnimationFramePromise(win);

     const checkBrowserSize = () => {
-      if (win.innerWidth === width && win.innerHeight === height) {
+      // Should probably be fuzzy, see https://github.com/w3c/csswg-drafts/issues/5260
+      if (win.innerWidth * win.devicePixelRatio === width && win.innerHeight * win.devicePixelRatio === height) {
         resized();
       }
     };
diff --git a/remote/cdp/domains/parent/Emulation.sys.mjs b/remote/cdp/domains/parent/Emulation.sys.mjs
index 21aaf2f965013..dba772fcf9363 100644
--- a/remote/cdp/domains/parent/Emulation.sys.mjs
+++ b/remote/cdp/domains/parent/Emulation.sys.mjs
@@ -87,8 +87,8 @@ export class Emulation extends Domain {

     // Wait until the viewport has been resized
     await this.executeInChild("_awaitViewportDimensions", {
-      width: targetWidth,
-      height: targetHeight,
+      width: targetWidth * browser.ownerGlobal.devicePixelRatio,
+      height: targetHeight * browser.ownerGlobal.devicePixelRatio,
     });
   }

(untested)

But also some zoom values are going to give you rounded dimensions, and that's unavoidable, see the issue linked in the comment.

Flags: needinfo?(emilio)

Perfect, devicePixelRatio works as expected here, thanks Emilio!

Attachment #9406965 - Attachment description: Bug 1901211 - [bidi] Use context.fullZoom when awaiting dimensions in browsingContext.setViewport → Bug 1901211 - [bidi] Use window.devicePixelRatio when awaiting dimensions in browsingContext.setViewport
Severity: -- → S3
Points: --- → 3
Priority: -- → P2
Whiteboard: [webdriver:backlog]
Assignee: jdescottes → nobody
Status: ASSIGNED → NEW
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: