Saturday, March 3, 2018

How to Change the Size of Browser Window while Working with WebDriver?

Today we will discuss how to change window size of browser in selenium. When you start the browser in WebDriver, it takes the default settings. However, while testing it is required to change the size of the window. This is especially required during the testing of responsive web design, just to check the response of each element with varying browser window size.

How to Resize Browser Window in WebDriver, How to Change the Size of Browser Window, Set browser width and height in Selenium Webdriver, How to change the size of the browser window



Code Snippet to Resize Browser Window in selenium :

// Whatever dimension user want, that could be given as argument. 
Dimension d = new Dimension (300,300); 
driver.manage().window().setSize(d);

Code snippet to get the get window width and height :
//WebDriver used to get window width and height.
System.out.println("Window height Is -> "+driver.manage().window().getSize().getHeight());
System.out.println("Window width Is -> "+driver.manage().window().getSize().getWidth());

Set browser width and height in Selenium Webdriver :

Lets see the complete example, where we resize the browser window after launch. Here we are using setSize method to set width and height of browser window.




SeleniumDemo.java
package demoPackage;

import org.openqa.selenium.Dimension;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class SeleniumDemo {

 public static WebDriver driver;
 
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  
  System.out.println("Launching the chrome driver  ");
  
  // Set the chrome driver exe file path
  System.setProperty("webdriver.chrome.driver","E:\\selenium_sumit\\chromedriver_win32_2.33\\chromedriver.exe");
  
  // Instantiate the chrome driver
  driver = new ChromeDriver();
  
  // set the browser URL in get() to load the webpage
     driver.get("https://www.google.com");
     
     //Maximize the Browser
     driver.manage().window().maximize();     

     // Whatever dimension user want, that could be given as argument. 
     Dimension d = new Dimension (300,300);
     driver.manage().window().setSize(d);

   //WebDriver used to get window width and height.
   System.out.println("Window height Is -> "+driver.manage().window().getSize().getHeight());
   System.out.println("Window width Is -> "+driver.manage().window().getSize().getWidth());
 }

}

This is all about resizing the browser window in selenium. Hope you like this simple example. Thank you for reading this article, and if you have any problem, have a another better useful solution about this article, please write message in the comment section.


No comments:

Post a Comment