Handling Browser Window

Understanding Selenium WebDriver Interface

WebDriver Interface in Selenium

The WebDriver interface is the core of Selenium WebDriver, used to control the web browser. It allows you to perform various browser actions such as maximizing the browser window, navigating through browser history, and opening specific URLs of the application under test (AUT). Without this interface, automation would not be possible, which is why it is named Selenium WebDriver.

Key Methods for Browser Control

  • get(String url)
  • getTitle()
  • getCurrentUrl()
  • getPageSource()
  • close()
  • quit()
  • navigate()
  • manage()
  • getWindowHandles()
  • getWindowHandle()
  • switchTo()

Additionally, the WebDriver interface inherits two methods from the SearchContext interface:

  • findElement(By by) - Finds a single web element.
  • findElements(By by) - Finds a list of web elements.

Method Details

1. The get() Method

The get() method is used to navigate to a specified URL in the browser.

  • Accepts a fully qualified URL (e.g., "https://www.example.com").
  • Waits for the page to load completely.
  • Returns void.
public void get(String URL)

Usage:

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

Note: If the protocol (http, https) is not specified, it throws an InvalidArgumentException.

2. The getCurrentUrl() Method

This method fetches the URL of the current webpage.

  • Used to verify the current webpage's URL.
  • Returns the URL as a String.
  • No arguments required.
public String getCurrentUrl()

Usage:

String actualUrl = driver.getCurrentUrl();

3. The getTitle() Method

The getTitle() method retrieves the title of the current webpage.

  • Useful for verifying the page title.
  • Returns the title as a String.
  • No arguments required.
public String getTitle()

Usage:

String actualTitle = driver.getTitle();

4. The getPageSource() Method

This method returns the source code of the current webpage.

  • Helps verify if the page contains specific information.
  • Returns the source code as a String.
  • No arguments required.
public String getPageSource()

Usage:

String source = driver.getPageSource();

5. The close() Method

The close() method closes the current browser window.

  • Closes only one window at a time.
  • No arguments required.
  • Returns void.
public void close()

Usage:

driver.close();

Note: This method is typically used to close the parent window.

6. The quit() Method

The quit() method closes all browser windows and stops the WebDriver session.

  • Closes both parent and child windows.
  • No arguments required.
  • Returns void.
public void quit()

Usage:

driver.quit();

Difference between close() and quit():

  • close(): Closes the current window and does not stop the server.
  • quit(): Closes all windows and stops the server.

7. The navigate() Method

The navigate() method is used for browser navigation.

  • Supports navigation to sub-URLs, back, forward, and refresh.
  • Returns a Navigation type object.
public Navigation navigate()

Usage:


driver.navigate().to("https://www.example.com");
driver.navigate().back();
driver.navigate().forward();
driver.navigate().refresh();
        

Difference between get() and navigate():

  • get(): Used for navigating to the main URL.
  • navigate(): Used for sub-URLs and browser history navigation.

8. The manage() Method

The manage() method is used to manage browser settings.

  • Performs window operations, script timeouts, and cookie management.
  • Returns an Options type object.
public Options manage()

Usage:


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

Common Window Operations:

  • maximize(): Maximizes the browser window.
  • getSize(): Gets the dimensions of the window.
  • setSize(): Sets the dimensions of the window.

9. The switchTo() Method

The switchTo() method shifts the driver's focus to different areas like windows, frames, or alerts.

  • Used to handle multiple windows, frames, and alerts.
  • Returns a TargetLocator type object.
public TargetLocator switchTo()

Usage:


driver.switchTo().window(windowHandle);
driver.switchTo().frame(frameName);
driver.switchTo().alert().accept();
        

10. The getWindowHandle() Method

This method retrieves the unique identifier (handle) of the current window.

  • Useful for handling multiple windows.
  • Returns the window handle as a String.
public String getWindowHandle()

Usage:

String currentWindowId = driver.getWindowHandle();

11. The getWindowHandles() Method

This method retrieves the handles of all open windows.

  • Returns a set of window handles (Set).
public Set getWindowHandles()

Usage:

Set allWindowIds = driver.getWindowHandles();

Handling Multiple Windows:

  • Capture window IDs.
  • Switch control to the desired window.
  • Perform actions on the window.

Author: Akash Deb (SDET)

Comments

Popular posts from this blog

Introduction to Automation

Introduction To Selenium