Skip to content Skip to sidebar Skip to footer

How Do You Set The Browser Inner Window/viewport Size?

If I call the function: browser.driver.manage().window().setSize(1000, 1000); It sets my window size to 1000x1000 and my inner window/viewport size to 990x918. By inner window siz

Solution 1:

I don't think that there's any build-in method to resize the viewport to a given size. However it can be done by setting an outter size that will match the targeted inner size:

var webdriver = require('./node_modules/protractor/node_modules/selenium-webdriver');

// extension method to set the inner size
webdriver.WebDriver.prototype.setViewportSize = function(width, height){
  const JS_GET_PADDING = "return {"
    + "w: window.outerWidth - window.innerWidth,"
    + "h: window.outerHeight - window.innerHeight };";

  varself = this;
  self.executeScript(JS_GET_PADDING).then(function(pad){
    self.manage().window().setSize(width + pad.w, height + pad.h);
  });
};

// start the browservar driver = new webdriver.Builder().withCapabilities({browserName: 'firefox'}).build();

// set the window inner size to 800 x 600
driver.setViewportSize(800, 600);

// quit
driver.sleep(5000);
driver.quit();

Solution 2:

Here is the C# Version @Florent B. answer:

publicstaticvoidSetViewportSize(RemoteWebDriver driver, int width, int height)
    {            
        var jsGetPadding = @"return [ window.outerWidth - window.innerWidth,window.outerHeight - window.innerHeight ];";
        var paddingArray = driver.ExecuteScript(jsGetPadding) as ReadOnlyCollection<object>; 
        driver.Manage().Window.Size = new Size(width + int.Parse(paddingArray[0].ToString()), height + int.Parse(paddingArray[1].ToString()));
    
    }

Usage:

RemoteWebDriverdriver=newEdgeDriver();
        SetViewportSize(driver,800, 800);

Post a Comment for "How Do You Set The Browser Inner Window/viewport Size?"