question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Puppeteer: resizeWindow() does not change window bounds

See original GitHub issue

What are you trying to achieve?

I want to resize the browser window

What do you get instead?

I get a bigger viewport size, but the browser window size stays the same.

See also https://github.com/GoogleChrome/puppeteer/issues/1183

Details

  • CodeceptJS version: 1.1.5
  • NodeJS Version: 8.9.4
  • Operating System: Windows
  • Puppeteer (1.0.0)

I would recommend considering the following code for the resizeWindow function which seems to work well


async resizeWindow(width, height) {
    await this.page.setViewport({height, width});

    // Window frame - probably OS and WM dependent.
    height += 85;
    
    // Any tab.
    const {targetInfos: [{targetId}]} = await this.browser._connection.send(
      'Target.getTargets'
    );
    
    // Tab window. 
    const {windowId} = await this.browser._connection.send(
      'Browser.getWindowForTarget',
      {targetId}
    );
    
    // Resize.
    await this.browser._connection.send('Browser.setWindowBounds', {
      bounds: {height, width},
      windowId
    });    
  }

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:18
  • Comments:5

github_iconTop GitHub Comments

2reactions
dvdvdmtcommented, Dec 28, 2020

Here is a slightly improved version of @hubidu’s snippet:

  async resizeWindow(width: number, height: number) {
    const page = await this.getActiveTab();
    const session = await page.target().createCDPSession();
    await page.setViewport({height, width});
    const {windowId} = await session.send('Browser.getWindowForTarget');
    await session.send('Browser.setWindowBounds', {
      bounds: {height, width},
      windowId,
    });
  }

I removed usages of private properties and call to ‘Target.getTargets’, because ‘Browser.getWindowForTarget’ returns the window with active CDP session (docs).

1reaction
jan-swieckicommented, Apr 25, 2020

Modified version for puppeteer with slight code update (it works with chrome app mode):

async function resizeWindow(browser, page, width, height) {
  await page.setViewport({height, width})

  // Window frame - probably OS and WM dependent.
  height += 85
  
  // Any tab.
  const targets = await browser._connection.send(
    'Target.getTargets'
  )

  // modified code
  const target = targets.targetInfos.filter(t => t.attached === true && t.type === 'page')[0]
  
  // Tab window. 
  const {windowId} = await browser._connection.send(
    'Browser.getWindowForTarget',
    {targetId: target.targetId}
  )
  const {bounds} = await browser._connection.send(
    'Browser.getWindowBounds',
    {windowId}
  )
  
  const resize = async () => {
    await browser._connection.send('Browser.setWindowBounds', {
      bounds: {width: width, height: height},
      windowId
    })
  }

  if(bounds.windowState === 'normal') {
    await resize()
  } else {
    await browser._connection.send('Browser.setWindowBounds', {
      bounds: {windowState: 'minimized'},
      windowId
    })
    await resize()
  }
}

const browser = await puppeteer.launch({
  executablePath: '/path/to/chrome',
  args: ['--app=http://localhost:8080'],
  headless: false,
  defaultViewport: null
})

const page = (await browser.pages())[0]

setTimeout(() => {
  console.log('resize')
  resizeWindow(browser, page, 800, 600)
}, 1000)
Read more comments on GitHub >

github_iconTop Results From Across the Web

Puppeteer: resizeWindow() does not change window bounds
I get a bigger viewport size, but the browser window size stays the same. See also puppeteer/puppeteer#1183. Details. CodeceptJS version: 1.1.5 ...
Read more >
javascript - resize browser with puppeteer - Stack Overflow
What I've found works for setting the browser size (but not the viewport size) is to set the following chrome command line switch...
Read more >
Puppeteer Window Size - CodeceptJS Community
resizeWindow just resizes the viewport for us. Some discussions say it is not possible to change window size with puppeteer and JS but...
Read more >
Puppeteer - CodeceptJS
Puppeteer does not control the window of a browser so it can't adjust its real size. It also can't maximize a window.
Read more >
Detecting and adjusting for a window resize - JavaScript
So now, I'm going to make my window smaller again and I'll refresh click on this and as I re-size the window, the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found