The manage() method is a vital part of Selenium WebDriver’s API for Java, enabling automation scripts to control and configure various browser settings during runtime. This method returns a WebDriver.Options
interface, which offers a suite of functionalities to manage windows, timeouts, cookies, and more.
What is the manage() Method?
- Purpose: Grants access to the browser’s management options.
- Returns: A
WebDriver.Options
object, from which you can further select sub-interfaces to manage windows, timeouts, cookies, etc. - Common Usage: Maximizing/minimizing windows, setting timeouts, handling cookies, and manipulating window positions.
Syntax:
driver.manage().window().maximize();
Here, driver
is your active WebDriver instance.
Key Features and Usage
1. Window Management
Control browser window properties and behavior:
Maximize Window:
driver.manage().window().maximize();
Minimize Window:
driver.manage().window().minimize();
Full Screen:
driver.manage().window().fullscreen();
Set/Get Position:
driver.manage().window().setPosition(new Point(100, 200));
Point pos = driver.manage().window().getPosition();
2. Timeouts Management
Configure implicit waits and other timeouts to make automation more resilient:
Implicit Wait:
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
Sets the default wait time for locating elements.
Page Load Timeout:
driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(30));
Script Timeout:
driver.manage().timeouts().scriptTimeout(Duration.ofSeconds(15));
3. Handling Cookies
Add, retrieve, and delete browser cookies:
Add Cookie:
Cookie cookie = new Cookie("username", "seleniumUser"); driver.manage().addCookie(cookie);
Get Cookie by Name:
Cookie myCookie = driver.manage().getCookieNamed("username");
Delete Cookie:
driver.manage().deleteCookieNamed("username");
Complete Code Example
Below is an end-to-end example demonstrating core uses:
import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.Cookie; import org.openqa.selenium.Point; import java.time.Duration; public class ManageExample { public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver"); WebDriver driver = new ChromeDriver(); // Maximize window driver.manage().window().maximize(); // Set timeouts driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10)); // Add a cookie Cookie userCookie = new Cookie("testUser", "selenium"); driver.manage().addCookie(userCookie); // Set window position driver.manage().window().setPosition(new Point(50, 50)); driver.quit(); } }
Summary Table
Action | Example Syntax | Description |
---|---|---|
Maximize window | driver.manage().window().maximize(); | Enlarges the browser to full screen |
Set Implicit Wait | driver.manage().timeouts().implicitlyWait(...); | Sets default wait time for element discovery |
Add Cookie | driver.manage().addCookie(new Cookie(...)); | Injects a cookie into the session |
Set Position | driver.manage().window().setPosition(new Point(...)); | Places the browser window at given coords |
Best Practices
- Always set necessary timeouts to avoid flaky tests, especially on dynamic web pages.
- Use window controls to standardize browser appearance for screenshots or UI tests.
- Cookie management can help with login flows or stateful testing.
The manage()
method acts as a gateway for browser configuration and control in Selenium Java, making tests more stable and browsers more manageable.