'How can we achieve browser zoom in and zoom out in playwright?

Need to find a way to zoom browser in and out during an e2e test run. Below code does not work.

await page.keyboard.down('Control');
  for (let i = 0; i < 7; i++) {
    await page.keyboard.press('+');
  }
  await page.keyboard.up('Control');


Solution 1:[1]

You can set this with Javascript:

 await page.evaluate(() => {
            document.body.style.zoom=1.0;
        });

Set the value to the required zoom:

  • 1.0 is 100%
  • 1.1 is 110%
  • and so on

I don't have things installed to test this but let me know if you hit any issues and I'll look again.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 RichEdwards