Friday 26 May 2017

Selenium WebDriver : Setting / Getting Browser Window Dimensions

What's up Gangsters ? Welcome to another post in which we will see how we can set the dimensions of our browser window as required. Either we can set the dimensions to some absolute values or we can maximize the window.



Setting Browser window dimensions :


Following code snippet shows how to set the dimensions using absolute values after initializing Web Browser :



System.setProperty("webdriver.gecko.driver", "E:\\path\\to\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
org.openqa.selenium.Dimension d = new org.openqa.selenium.Dimension(400, 500);
driver.manage().window().setSize(d);



Note :  org.openqa.selenium is the package name that includes Dimension as one of the classes to be used to set dimensions in Selenium and driver is the WebDriver instance.


Another way to set the dimensions of Browser window is to maximize the size of the window. Following code snippet shows how to maximize the browser window:



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




The above methods can be used to set the dimensions of the Browser window based on the requirement.




Getting Browser window dimensions :


Let's now see how we can get the dimensions of the window at any given point. Believe me it's really simple (Just one line of code :P). Following code snippet can be used to get the dimensions of the Browser window and print it on console :



System.out.println(driver.manage().window().getSize());



Told you it's going to be just one line of code, not so complex right ? :)


To conclude, in this post we have seen how we can set the dimensions of the Browser window after we initialize the driver instance, either by using the absolute values or maximizing to the size of the desktop. Also we have seen how we can get the dimensions of the window at any given point.


Feel free to comment if you have any doubts / suggestions.
Happy Learning.

YouTube Channel : Click Here