Principle:Getgauge Taiko Browser Closure
| Knowledge Sources | |
|---|---|
| Domains | Browser_Automation, Resource_Management |
| Last Updated | 2026-02-12 00:00 GMT |
Overview
Process of gracefully terminating a browser instance and cleaning up associated resources after automation completes.
Description
Browser closure ensures clean teardown of all resources allocated during a browser automation session. This includes closing the Chrome DevTools Protocol (CDP) connection, sending a termination signal to the browser process, waiting for the process to exit, and removing any temporary user data directories that were created at launch time.
Graceful shutdown is critical for preventing resource leaks. Each browser automation session spawns a Chromium process that consumes significant memory and CPU. Without proper closure, orphaned browser processes accumulate on the host system, eventually exhausting available resources. This is especially problematic in CI/CD environments where many test runs execute sequentially on the same machine.
The closure process implements a timeout-based escalation strategy. First, a SIGTERM signal requests the browser to shut down gracefully, allowing it to flush pending writes and release file locks. If the process does not exit within the timeout period, a SIGKILL signal forcefully terminates it. This ensures that automation scripts never hang indefinitely due to a misbehaving browser process.
Temporary user data directories are created for each browser session to provide isolation between test runs. These directories store browser profiles, caches, and session data. Cleaning them up on closure prevents disk space exhaustion over many test runs.
Usage
Use browser closure at the end of every automation session:
- After test suite completion -- Call
closeBrowser()in the teardown/afterAll hook to ensure cleanup regardless of test outcomes. - After script execution -- Any standalone automation script should close the browser before exiting.
- In error recovery -- Wrap browser operations in try/finally blocks to guarantee closure even when exceptions occur.
- Between test scenarios -- When test isolation requires a fresh browser state, close and reopen the browser between scenarios.
Theoretical Basis
The browser closure process follows this sequence:
- Close CDP connection -- Disconnect the WebSocket connection to the browser's DevTools Protocol endpoint. This stops all ongoing communication with the browser.
- Send SIGTERM -- Send a termination signal to the Chromium process, requesting a graceful shutdown. The browser attempts to close all tabs, flush pending data, and release resources.
- Wait for exit -- Monitor the process for a configurable timeout period, allowing it time to complete its shutdown procedure.
- SIGKILL fallback -- If the process has not exited after the timeout, send
SIGKILLto forcefully terminate it. This handles cases where the browser is unresponsive or stuck in a shutdown loop. - Cleanup temp directories -- Remove the temporary user data directory that was created when the browser launched. This includes profile data, cache files, and any downloaded content stored in the temp location.
On Windows, the termination strategy differs slightly: taskkill commands are used instead of Unix signals, and process tree termination ensures that all child processes (e.g., renderer processes, GPU processes) are also terminated.