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
- Launch the Chrome browser
- Open Google on the browser
- Maximize the browser window
- Print the browser window title
- Minimize the browser window for 3 seconds
- Again Maximize the browser window
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
import java.util.concurrent.TimeUnit; import org.openqa.selenium.Point; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; class MinimizeBrowserWindowInSelenium { public static void main(String[] args) throws InterruptedException { String BrowserDriverPath= "C:\\SeleniumBrowserDrivers\\chromedriver.exe"; System.setProperty("webdriver.chrome.driver",BrowserDriverPath); WebDriver driver = new ChromeDriver(); driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); driver.navigate().to("https://www.google.com/"); //Maximize the browser window driver.manage().window().maximize(); //Printing Browser Title System.out.println("Browser Title is: "+driver.getTitle()); // Minimize the browser window driver.manage().window().setPosition(new Point(0, -1000)); Thread.sleep(3000); //Maximizing browser after 3 seconds driver.manage().window().maximize(); } } |
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
- How To Resize Browser In Selenium WebDriver
- Actions Class in Selenium WebDriver
- How to Use AutoIT In Selenium WebDriver
- Handle Dynamic Web Tables In Selenium WebDriver
- Click a Button in Selenium WebDriver and Carry out Other Validations
- Double Click and Right Click in Selenium with Examples
- 3 Ways Of Drag and Drop Action in Selenium With Examples
- Page Object Model (POM) & Page Factory in Selenium WebDriver