How to Manage Browser Dimension and Position in Selenium Webdriver?



If the browser isn't maximized, Selenium won't detect elements on a web application, which might cause the framework to fail. Therefore maximizing the browser is a very essential part of the Selenium Framework. It is a good practice for maximizing browsers by automating any web application. When a user performs any task using the Selenium Framework the browser may not be in the full-screen mode. You should maximize the browser using window maximize in Selenium to see all elements present in the web application. It is essential to learn how to manage browser dimensions and maximize the browser at the start of the script so the script gets executed successfully without any errors.

Here is how to manage browser dimension in selenium:

To maximise the browser in Selenium, use all of the maximise() selenium commands to maximise the driver class's window interface.

void maximize()- This method is used for maximizing the current browser.

How to manage browser dimension in selenium?

The size of the browser could be customized according to the scenario. Selenium WebDriver doesn't provide any method for minimizing the browser so there is no direct method. You are required to use resize method for minimizing the browser.

void setSize() - This method is used for setting the size of the current browser.

Dimension getSize() - This method is used for obtaining the size of the browser in height and width. It will return the web browser dimension of the browser.

Point setPosition() - This technique is used to change the current browser's location.

Sometimes dimensions and positions should be set in a new browser window. There are Selenium dimension class and Selenium position class available with different methods. You can also use the dimension class and position class in Selenium and its methods for setting dimensions and positions of browser Windows. Let's see how web browser dimension and position methods could be used to manage size and position.

Selenium dimension class and selenium position class for managing in browser dimension drawing and position

package TestPack;

import org.openqa.selenium.Dimension;

import org.openqa.selenium.Point;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

 

public class testSetPosition {

 

 public static void main(String[] args) {

  String exePath = "D:\\chromedriver_win32\\chromedriver.exe";

  System.setProperty("webdriver.chrome.driver", exePath);

  WebDriver driver = new ChromeDriver();

 

  //Selenium Dimension Class Set the width and height of your browser window to 300 pixels.

  Dimension dim = new Dimension(300, 300);

  driver.manage().window().setSize(dim);

    

  //Selenium Position Class to Set window position from top left corner of screen.

  Point pnt = new Point(100, 100); 

  driver.manage().window().setPosition(pnt);

 }

}

Maximize and full-screen window using manage() method in Selenium WebDriver

In the above example of Selenium dimension and Selenium position the defined dimension is 300H and 300W and then used is the setSize() method to set a different dimension for the browser window. In the same way, 100 points from the top and hundred points from the left are defined and then the setPosition method is used to set the browser window at the given position.


 

You may also like Creating A Robot Using Java With Selenium For Good Interaction With The Browser


Example for minimizing a browser window using WebDriver

Selenium script with explanation

Script description: In the selenium script shown below shows how to manage browser dimension using the TestNG framework. The steps involved are:

  1. Open the Chrome browser.
  2. Launch the website.
  3. Wait for some time till the view minimize action appears.
  4. Then close the browser.

import org.openqa.selenium.Point;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

 

public class Minimize {

 

public static void main(String args[]) throws InterruptedException

{

WebDriver driver;

System.setProperty("webdriver.chrome.driver","E://Selenium//Selenium_Jars//chromedriver.exe");

driver= new ChromeDriver();

            // Launch the application

      driver.get("https://www.guru99.com/");

       

      Point p = new Point(0,3000);

     

      //Minimize the current window to the set position

              driver.manage().window().setPosition(p);

              

            //To Delay execution for 10 sec. as to view the minimize browser

              //you can view it in the taskbar below the screen.

              Thread.sleep(10000);

              

              //Close the browser

              driver.quit();

}

}

Note: If the user wants to use Firefox browser then they should set property of firefoxdriver and create Firefoxdriver object instead of Chromedriver for their device's operating system:

System.setProperty("webdriver.gecko.driver","E://Selenium//Selenium_Jars//geckodriver.exe ");

 driver= new FirefoxDriver();

Output analysis

Now open the chrome browser, minimize it and wait for a few seconds and then close the browser.

Troubleshooting

  1. Use the latest version of Selenium Jars, chromedriver, marionettedriver, and IE driver.
  2. Check the compatibility of Selenium Jars and in-browser dimension drawing used. 

Combining best generation hardware and managing platforms that deliver the requirements of selenium WebDriver.

The reality of the IT infrastructure is changing fundamentally. Here Selenium comes into play as it is a platform integrated with the required hardware. Synchronization of management platforms and hardware guarantees that all advanced features are accessible readily. Nevertheless, it is also essential to provide support for legacy infrastructure and essential capability that enter fight provides. This complete uniform environment, along with increased degrees of automation, simplifies IT operations and creates a steady infrastructure.

This integrated strategy enables the delivery of the performance required to keep up with the evolving computing landscape. The UCS X-Series and Intersight together enable IT to deploy new technologies that improve performance. Using the newest Intel Xeon processors as soon as they become available will offer performance headroom for demanding applications.

Conclusion

Optimising the browser is an important aspect of the Selenium framework. Selenium won't recognise components on a web application if the browser isn't maximised, which could cause the framework to fail. It's critical to understand how to adjust browser dimensions and maximise the browser at the start of the script to ensure that it runs smoothly. With distinct approaches, selenium dimension class and selenium position class are accessible. The browser's size could be adjusted based on the situation.