Changing the default browser window size

Here’s a super simple example of how to change the browser window size:

$browser.manage().window().setSize(640, 480).then(function() {
    $browser.get("http://whatsmy.browsersize.com"); 
});

Setting window size doesn’t work (anymore ?) . An exception is thrown instead. Do you know of another way to test responsive websites ?

Hi, @peter.even1: Looks like this is an issue with recent versions of Selenium and Chrome. I am not sure how to change the window size from Selenium in Chrome 72. Does anyone have a suggestion?

OK, figured it out! This seems to work:

const { By, until } = $driver;

// Request dummy page for main browser window
$browser.get('https://httpbin.org/get')
  .then(function() {
    // Open target URL in popup window of desired size
    return $browser.executeScript('window.open("http://setmy.browsersize.com/", "myPopup", "height=480,width=640")')
      .then(function() {
        // HACK: Wait for popup to load
        return $browser.sleep(1000)
          .then(function() { 
            // Switch to popup window and interact with page
            return $browser.switchTo().window('myPopup')
              .then(function() { 
                $browser.findElement(By.id('info_ww'))
                  .then(function(element) {
                    return element.getText();
                  })
                  .then(function(text) {
                    console.log('width: ' + text);
                  });
                return $browser.findElement(By.id('info_wh'))
                  .then(function(element) {
                    element.click();
                    return element.getText();
                  })
                  .then(function(text) {
                    console.log('height: ' + text);
                  });
                });
           });
      });
  });

1 Like