Site icon TestingDocs.com

What are different Window functions available in Selenium?

Overview

Selenium WebDriver has extensive API support for performing different browser window functions. Actions such as resizing the window, locating the window, and switching windows, etc.

The maximize() method lets you maximize the current browser window size. The following is the syntax of this function:

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

The switchTo.window() method transfers control from one browser window to
another.It allows you to switch control from a parent window to a child window etc.

driver.SwitchTo().window(anotherWindow);

getWindowHandle() and getWindowHandles()

The getWindowHandle() method handles the current browser window. It allows you to handle a browser after switching a specific window being tested.

String parentWindow = driver.getWindowHandle();
driver.switchTo().window(parentWindow);

The getWindowHandles() method handles all the browser windows and allows you to switch control between the parent window and the child windows.

Sample Code

Sample snippet to get a handle to the child windows is shown below. To handle the child windows, getWindowHandles() method returns list of objects i.e child windows.

// Sample usage of getWindowHandles..
 String parentWindow = driver.getWindowHandle();
 Set<String> windowHandles = driver.getWindowHandles();
 for(String handle : windowHandles)
 {
 if(!handle.equals(parentWindow))
 {
 driver.switchTo().window(handle);
 // perform some action or test on the window
 driver.switchTo().window(parentWindow);
 }
 }

 

Selenium Tutorial on this website:

https://www.testingdocs.com/selenium-webdriver-tutorial/

For more information on Selenium, visit the official website:

https://www.selenium.dev/

Exit mobile version