Back to Repositories

Testing Window Closure Operations and Handle Management in NW.js

This test suite validates window handling behavior in NW.js applications using Selenium WebDriver, specifically focusing on window closure operations and handle management. It verifies proper cleanup of window resources and handle references when closing child windows programmatically.

Test Coverage Overview

The test suite provides comprehensive coverage of window management functionality in NW.js applications.

Key areas tested include:
  • Window handle tracking and enumeration
  • Child window creation and switching
  • Window closure operations
  • Handle cleanup verification
The test specifically validates that child windows are properly closed and removed from the handle list, preventing resource leaks.

Implementation Analysis

The testing approach utilizes Selenium WebDriver with Chrome options configured specifically for NW.js applications. The implementation follows a sequential pattern of window operations:

  • Initial window handle verification
  • Child window creation via button click
  • Window handle switching and management
  • Programmatic window closure
  • Handle count validation

Technical Details

Testing tools and configuration:

  • Selenium WebDriver with Chrome driver
  • Custom NW.js utility functions
  • Python test framework with assertion checks
  • Chrome options with NW.js app path configuration
  • Environment-specific ChromeDriver path setup

Best Practices Demonstrated

The test implementation showcases several testing best practices:

  • Proper resource cleanup with try/finally blocks
  • Explicit wait conditions for asynchronous operations
  • Clear separation of setup, execution, and verification steps
  • Robust handle management and validation
  • Effective use of timeouts and assertions

nwjs/nwJs

test/sanity/issue4138-win-not-close/test.py

            
import time
import os
import sys
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from nw_util import *

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument("nwapp=" + os.path.dirname(os.path.abspath(__file__)))

driver = webdriver.Chrome(executable_path=os.environ['CHROMEDRIVER'], chrome_options=chrome_options)
try:
    print(driver.current_url)
    driver.find_element_by_tag_name('button').click()
    print('wait for window open')
    wait_window_handles(driver, 2)
    print(driver.window_handles)
    print('switch_to')
    driver.switch_to.window('child')
    print('try close')
    driver.close()
    print('wait for window close')
    wait_window_handles(driver, 1, timeout=5)
    assert (len(driver.window_handles) == 1)
finally:
    driver.quit()