DEV Community

Mangai Ram
Mangai Ram

Posted on

Part :3 - My Selenium (Technical )Automation Interview Questions in one of leading MNC

Explain the use of the getWindowsHandles() method.

Explanation:

The getWindowsHandles() method in Selenium is used to retrieve a set of unique identifiers (window handles) for all currently open browser windows.

These window handles are crucial for switching between multiple browser windows during test automation.

Use Case:

In our project, we faced a scenario where clicking a link opens a new window with additional details. To automate this, I’ve incorporated the getWindowsHandles method.

After clicking the link, I use this method to capture the window handles, switch to the new window, perform the necessary actions, and then switch back to the main window.

Code Snippet:

// Store the handle of the main window
String mainWindowHandle = driver.getWindowHandle();
// Click a link or perform an action that opens a new window
// Get all window handles
Set windowHandles = driver.getWindowHandles();
// Iterate through handles and switch to the new window
for (String windowHandle : windowHandles) {
if (!windowHandle.equals(mainWindowHandle)) {
driver.switchTo().window(windowHandle);
// Perform actions in the new window
// Close the new window (or perform other actions)
driver.close();
// Switch back to the main window
driver.switchTo().window(mainWindowHandle);
}
}

This code snippet demonstrates the usage of getWindowsHandles() to switch between windows, perform actions in the new window, and then switch back to the main window.

Exception:

An exception that might occur is NoSuchWindowException if the specified window handle is not found. Proper exception handling is essential to maintain the script’s stability.

Challenges:

One challenge was handling scenarios where pop-ups or unrelated windows were present. To address this, I implemented additional checks based on window titles or URLs to filter and switch only to the relevant windows.

Another challenge was ensuring the correct order of switching between windows, especially when dealing with dynamic content loading.

To overcome this, I incorporated explicit waits to ensure that the windows were fully loaded before interacting with them.

In your point of view any add on required means let discuss

To know more selenium Interview questions

these top technical automation interview questions arepublished in testleaf blog with on behalf of them am sharing with my inputs.

Thank you

Top comments (0)