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();
}

}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s