How to Set and Get Window size/position using Selenium Webdriver

|
| By Webner

How to Set and Get Window size/position using Selenium Webdriver

In website testing, most of the time, we need to set window position and size or get window size and position to test the website responsiveness. Selenium Webdriver has many applicable methods using which we can resize a web browser window for a different purpose. One of them is window().maximize(); which we can utilize to maximize the browser window size. Selenium Webdriver also has methods to set and get the window size and position.

Here are the methods:

Method to set the window height and width:
driver.manage().window().setSize(new Dimension(width, height));

Method to get the window height:
driver.manage().window().getSize().getHeight();

Method to get the window width:
driver.manage().window().getSize().getWidth();

Method to set the window position:
driver.manage().window().setPosition(new Point( x coordinate, y coordinate));

Method to get the window position of the X coordinate:
driver.manage().window().getPosition().getX();

Method to get the window position of the Y coordinate:
driver.manage().window().getPosition().getY();

Code:

package Testing;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.Point;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import io.github.bonigarcia.wdm.WebDriverManager;
public class windowSize {
WebDriver driver;
@BeforeTest
public void setup() throws Exception {
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://webnersolutions.com/");
}
@Test(priority=1)
public void setWindowSize(){
//Set size method used to set window size width and height.
driver.manage().window().setSize(new Dimension(400,600));
//Get size method used to get window width and height.
int windowHeight = driver.manage().window().getSize().getHeight();
int windowWidth = driver.manage().window().getSize().getWidth();
System.out.println("Window height is "+ windowHeight);
System.out.println("Window width is "+ windowWidth);
}
@Test(priority=2)
public void setWindowPosition() throws InterruptedException{
Thread.sleep(2000);
//Set position method used to set window position on the x coordinate and y coordinate.
driver.manage().window().setPosition(new Point(100,100));
//WebDriver getPosition method used to get window position x,y coordinates.
int X = driver.manage().window().getPosition().getX();
int Y = driver.manage().window().getPosition().getY();
System.out.println("Window position X coordinates is "+ X);
System.out.println("Window position Y coordinates is "+ Y);
}
@AfterTest
public void closeWindow() throws InterruptedException{
driver.close();
}
}

Screenshot:

Selenium

Leave a Reply

Your email address will not be published. Required fields are marked *