How to Minimize Browser in Selenium WebDriver

In this tutorial, I will show you how to minimize the browser in Selenium WebDriver if there is a need to do it.

Minimize Browser in Selenium WebDriver

We know how to maximize the browser window to the full width of the screen using the following command.

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

Having said that what if you would like to minimize the browser window in Selenium. By default, there is no method available in Selenium to minimize the browser window. If in case you want to continue your work on the desktop during the execution of the Selenium script and don’t want to see execution steps rendering on the screen in that case you can either use Headless browsers like HTMLUnitDriver, PhantomJS, etc. However, if you still want to minimize a browser during the automation execution process and don’t want to use the headless browser then you can use the following workaround.

driver.manage().window().setPosition(new Point(0, -1000));

The above code will place the browser in an area that is not within the viewable section of the screen you are working on. Thus it will give an impression that the browser window is in minimized state.

Example of Minimize Browser in Selenium

We will use the following test scenario to understand how to minimize the browser in Selenium.

Test Scenario

  1. Launch the Chrome browser
  2. Open Google on the browser
  3. Maximize the browser window
  4. Print the browser window title
  5. Minimize the browser window for 3 seconds
  6. Again Maximize the browser window

Conclusion

We have learned the workaround how to minimize the browser in Selenium. You can use the same wisely whenever it is required.

Recommended Posts

Leave a Reply