Back to Repositories

Testing Window Management and Closure Implementation in NW.js

This test suite validates window handling and closure functionality in NW.js applications using Selenium WebDriver. It specifically tests the behavior of closing multiple windows and verifying content state across local and remote contexts.

Test Coverage Overview

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

Key areas tested include:
  • Multiple window handle management
  • Window closure functionality
  • Content verification across different window contexts
  • Local and remote content state validation

Implementation Analysis

The testing approach utilizes Selenium WebDriver with Python to automate window interactions. The implementation employs helper functions for window management and leverages Chrome options for NW.js app testing.

Notable patterns include:
  • Window handle iteration and switching
  • Dynamic window title matching
  • Element state verification
  • Automated cleanup with try-finally blocks

Technical Details

Testing tools and configuration:
  • Selenium WebDriver with Chrome driver
  • Python test framework
  • Custom NW.js utility functions
  • Chrome options for app context
  • Implicit wait timing configuration
  • Environment-based driver path configuration

Best Practices Demonstrated

The test implementation showcases several testing best practices for window management scenarios.

Notable practices include:
  • Proper resource cleanup
  • Explicit window handle management
  • Robust element selection
  • Clear assertion conditions
  • Modular helper functions
  • Error handling with try-finally blocks

nwjs/nwJs

test/sanity/issue7503-close/test.py

            
import time
import os
import sys

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from nw_util import *

def close_window(title):
    for handle in driver.window_handles:
        driver.switch_to.window(handle)
        if title in driver.title:
            driver.find_element_by_id("close").click()
            break

def switch_to_window(title):
    for handle in driver.window_handles:
        driver.switch_to.window(handle)
        if title == driver.title:
            break

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)
driver.implicitly_wait(5)

try:
    wait_window_handles(driver, 3)
    close_window("local file");
    close_window("Example");
    switch_to_window("main window")

    result_local = driver.find_element_by_id('result_local').get_attribute('innerHTML')
    result_remote = driver.find_element_by_id('result_remote').get_attribute('innerHTML')

    assert(result_local == "local")
    assert(result_remote == "remote")
finally:
    driver.quit()