Now You Can Minimize Browser in Selenium WebDriver

Till Selenium 3, we were only able to maximize the window. We were not able to minimize directly. There were ways to do like setPosition() but it was of no use if you can not maximize whenever you want.

Selenium 4 gives you a brand new method named “minimize()” which can be used to minimize the browser just like a user normally do. “minimize” method is in inner interface “Window” of WebDriver interface.

Return type of minimize() method is void and official doc says it ” Minimizes the current window if it is not already minimized “.

Syntax :-

Same as maximize. Just use minimize in stead of miximize.

driver.manage().window().minimize();

Remember to use this method you need to have Selenium 4.0.0-alpha-5 in build path as it is launched in that particular version onward.

It takes screenshot also without any problem.

Sample Program :-

package SeleniumFourFeatures;

import java.io.File;
import java.io.IOException;

import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;

import com.google.common.io.Files;

import io.github.bonigarcia.wdm.WebDriverManager;

public class MinimizeExample {
	
	@Test
	public void doActions() throws IOException {
		
		WebDriverManager.chromedriver().setup();
		WebDriver driver = new ChromeDriver();
		
		driver.get("http://automationpractice.com/index.php?controller=authentication&back=my-account");
		
		// Minimize browser
		driver.manage().window().minimize();
		
		// Locating Email Address field
		WebElement emailAddress = driver.findElement(By.id("email_create"));
		
		// Let's perform same actions multiple times.
		emailAddress.sendKeys("Amod@gmail.com");
		emailAddress.clear();
		emailAddress.sendKeys("Amod1@gmail.com");
		emailAddress.clear();
		emailAddress.sendKeys("Amod2@gmail.com");
		emailAddress.clear();
		
		// Let's refresh the web page
		driver.navigate().refresh();
		
		// Relocating web element
		emailAddress = driver.findElement(By.id("email_create"));
		
		emailAddress.sendKeys("Amod4@gmail.com");
		// Taking screenshot 
		TakesScreenshot ts = (TakesScreenshot)driver;
		File ss = ts.getScreenshotAs(OutputType.FILE);
		File ds = new File(System.getProperty("user.dir")+"/src/test/resources/screenshot.png");
		Files.copy(ss, ds);
		
		emailAddress.clear();
		
		driver.close();
		
		
	}

}

You will see browser will be minimized and execution will be finished silently and you can do you other works. You can see taken screenshots in resource. Cool feature. Thanks developers of Selenium WebDriver.

You can find my Selenium WebDriver Java GIT repo here.

If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.
#ThanksForReading
#HappyLearning

You can find all Selenium related post here.
You can find all API manual and automation related posts here.
You can find frequently asked Java Programs here.

Leave a Reply

Your email address will not be published. Required fields are marked *