How to resize or maximize current browser window and get window size in Selenium Webdriver using java.

Get current window size
 WebDriver driver = new FirefoxDriver();
 driver.manage().window().getSize();
Resize current browser window on width and height
WebDriver driver = new FirefoxDriver();
Dimension dimension=new Dimension(500, 600);
driver.manage().window().setSize(dimension);
Move browser window to specific point base on x and y coordinate
 WebDriver driver = new FirefoxDriver();
Point point=new Point(100, 100)
driver.manage().window().setPosition(point);
Maximize current window size
 WebDriver driver = new FirefoxDriver();
 driver.manage().window().maximize();
OR
WebDriver driver = new FirefoxDriver();
Toolkit toolkit = Toolkit.getDefaultToolkit();
int Width = (int) toolkit.getScreenSize().getWidth();
int Height = (int) toolkit.getScreenSize().getHeight();
Dimension screenResolution = new Dimension(Width, Height);
driver.manage().window().setSize(screenResolution);

Example:
import java.awt.Toolkit;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.Point;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;


public class Resizewindow {

public static void main(String args[]) throws InterruptedException {
// Initialize firefox  driver      
WebDriver dr = new FirefoxDriver();

//Get current window size
System.out.println(dr.manage().window().getSize());

// Resize browser window base on width and height
dr.manage().window().setSize(new Dimension(500, 600));
System.out.println(dr.manage().window().getSize());
Thread.sleep(4000);

//Move browser window to specific point base on x and y coordinate
dr.manage().window().setPosition(new Point(20, 50));
System.out.println(dr.manage().window().getSize());
Thread.sleep(4000);

//Maximize browser window       
dr.manage().window().maximize();
System.out.println(dr.manage().window().getSize());
Thread.sleep(4000);

dr.manage().window().setPosition(new Point(0, 0));
dr.manage().window().setSize(new Dimension(500, 600));
Thread.sleep(4000);

//Maximize browser window  
Toolkit toolkit = Toolkit.getDefaultToolkit();
int Width = (int) toolkit.getScreenSize().getWidth();
int Height = (int) toolkit.getScreenSize().getHeight();
Dimension screenResolution = new Dimension(Width, Height);
dr.manage().window().setSize(screenResolution);
System.out.println(dr.manage().window().getSize());

//close firefox browser  
dr.quit();
    }

}

1 comment:

  1. Hi, I'm struggling with an interesting problem:
    driver.manage().window().setSize(960, 720);
    ...
    Dimension d = driver.manage().window().getSize();
    and d=(1200,900)
    The factor, i.e.1200/960 and 960/720 is 1.25.
    Also, there're similar problems with .setPosition.

    Thanks

    Viktor
    I'm running Chrome browser.

    ReplyDelete