Maximize Browser Window or Set Browser Size using Selenium Web driver - Java

                               Often we end up having to test the website on how it behaves in different screen resolutions.Here selenium provides us with two things one is to set the browser to a maximum size of the screen the browser is open on and the other is to provide the dimensions and set it.


It is recommended to set the position of the browser before setting the screen size .Below is a sample program on how to set screen size of the browser using selenium web driver.



package com.selenium_basics;

import org.openqa.selenium.Dimension;
import org.openqa.selenium.Point;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;

public class SetBrowserWindowSize {
 
 @Test
 public void setBrowserWindowSize() throws InterruptedException{
  WebDriver driver=new FirefoxDriver();
  driver.get("http://www.learn-selenium.net/");
  driver.manage().window().maximize(); // Maximize the Browser Window.
  Thread.sleep(5000);
  driver.manage().window().setPosition(new Point(0, 0)); //Set position of browser on the screen
  Dimension dim=new Dimension(1024,768);
  driver.manage().window().setSize(dim); // set size of the screen 1024x768
  Thread.sleep(5000);
  dim=new Dimension(500, 700); 
  driver.manage().window().setSize(dim); //set size of screen 500x700 width x height
  
  driver.close();
 }

}