Skip to main content

await browser.close()

Purpose:

The .close() method closes the current window or tab that the driver is currently focused on. This method is essential for managing multiple windows and tabs, especially when automating interactions with pop-up windows, iframes, or modal dialogs.

Parameters:

  • None

Returns:

  • Promise<void>: A promise that resolves when the window has been closed successfully.

Usage Examples:

1. Closing a Pop-up Window:

When your test interacts with a page that opens a pop-up window, you might need to close it after completing your actions.

// Navigate to a page that opens a pop-up window
await browser.get('http://example.com/with-popup');

await browser.switchTo().window(popup.handle);

console.log('Interacting with popup window...');
// Perform actions within the pop-up, such as clicking a button or filling out forms

// Close the pop-up window
await browser.close();

2. Closing an iFrame:

Sometimes you need to interact with elements inside an iframe and then switch back to the main content. After completing your actions, it’s necessary to close the iframe.

// Locate and switch to the iframe using its locator
const iframeElement = await browser.switchTo().frame(await element(by.id('myIframe')));

console.log('Interacting with iFrame...');
// Perform actions within the iframe (e.g., clicking a button)
await element(by.css('.submit-button')).click();

// Switch back to the main content and close the iframe
await browser.switchTo().defaultContent();
console.log('iFrame interaction complete. Closing...');
await browser.close();

3. Closing Multiple Windows:

In some scenarios, you might need to close multiple windows or tabs in a sequence.

// Get all open windows handles before closing any of them
const allHandles = await browser.getAllWindowHandles();

for (let handle of allHandles) {
if (handle !== browser.getWindowHandle()) {
// Switch to the other window and close it
console.log('Switching to another window...');
await browser.switchTo().window(handle);
console.log('Closing another window...');
await browser.close();
// Optionally, switch back to the main window after closing others
await browser.switchTo().window(browser.getWindowHandle());
}
}

console.log('All non-main windows have been closed.');

4. Error Handling:

It’s important to handle any potential errors that may occur when using .close().

try {
// Navigate to a page that opens a pop-up window
await browser.get('http://example.com/with-popup');

await browser.switchTo().window(popup.handle);

console.log('Popup interaction complete. Closing...');
await browser.close();
} catch (error) {
console.error("Error handling close operation:", error);
}

Additional Notes:

  • Timing: Ensure that you give enough time for the new window or iframe to load before attempting to close it. Use wait methods like waitForWindowToOpen to ensure elements are ready for interaction.

  • Multiple Contexts: If you are working within multiple frames or windows, make sure you switch contexts appropriately using switchTo() before closing the window.

When to Use:

  • After interacting with a popup window or modal dialog.
  • After completing actions within an iframe and switching back to the main content.
  • When you need to close all non-main windows in your session.

Final Thoughts

The .close() method is a crucial part of managing multiple windows and frames effectively. By ensuring that you handle timing, context management, and error handling appropriately, you can make your automation scripts robust and reliable. If you have any specific questions or need further assistance, feel free to reach out!

This documentation should help you understand how to use the .close() method in Selenium WebDriver for web automation tasks. If you encounter any issues or have additional questions, please consult the official Selenium Documentation and other external resources.