WebDriver commands – Part 4 (Browser window handling Commands)

In this blogpost, we are understand about the bunch of commands which deal with Browser’s window.

These are the commands defined in Windows interface of WebDriver API.

WebDriver is an interface, that contains manage() method, which returns Options interface’s reference. Options interface is used for managing stuff you would do in a browser menu.

Options interface contains many methods including window(), which returns the reference of Window interface.

And Window interface contains methods for managing browser window specific stuff.

Confusing ?

Have a look at the below code to have clear understanding.

WebDriver driver = new FirefoxDriver();

driver.get(“http://google.com”);

// EITHER

// getting Option interface’s reference by calling manage() of WebDriver interface

Options options = driver.manage();

// getting Window interface’s reference by calling window() of Options interface

Window window = options.window();

// calling maximize() method of Window interface’ reference

window.maximize();

// OR

// no need getting reference of Options and Window interfaces separately

driver.manage().window().maximize();

 

Window1Window2

So, in order to call any of the methods related to browser window manipulation,
driver.manage().window() is common.

maximize command :

Usage : Used to maximize the current browser window.

Syntax : void  maximize();

Return type : void (returns nothing)

Parameters : Nothing

Example :

driver.manage().window().maximize();


get size command :

Usage : Used to get the size (height & width) of the window. It returns the outer window dimension.

Syntax : Dimension getSize();

Return type : Dimension (An instance of Dimension class)

Parameters : Nothing

Example :

Dimension dimension = driver.manage().window().getSize();
System.out.println(dimension.height);
System.out.println(dimension.width);


get position command :

Usage : Used to get the position of the current window, relative to the upper left corner of the screen.

Syntax : Point getPosition();

Return type : Point (An instance of Point class)

Parameters : Nothing

Example :

Point point = driver.manage().window().getPosition();
System.out.println(“X offset : “+point.x);
System.out.println(“Y offset : “+ point.y);


set size command :

Usage : Used to set the size of the current window. This will change outer window dimension.

Syntax : void setSize(Dimension targetSize);

Return type : void (Nothing)

Parameters : Dimension class’ instance (specifies the target size of the window.)

Example :

// new Dimension (width, height);

Dimension newDimension = new Dimension(700, 600);
driver.manage().window().setSize(newDimension);


set position command :

Usage : Used to set the position of the current window, relative to the upper left corner of the screen.

Syntax : void setPosition(Point targetPosition);

Return type : void (Nothing)

Parameters : Point class’ instance (specifies the target position of the window.)

Example :

// new Point (xOffset, yOffset);

Point newPoint = new Point(300, 500);
driver.manage().window().setPosition(newPoint);

Advertisements

#browser-window-handling-commands, #getposition, #getsize, #maximize, #setposition, #setsize, #webdriver-commands