Back to Repositories

Testing macOS Command+Q Application Termination in NW.js

This test suite validates the macOS application quit functionality in NW.js using Selenium WebDriver and PyAutoGUI. It specifically tests the Command+Q keyboard shortcut behavior and ensures proper application termination.

Test Coverage Overview

The test provides comprehensive coverage of the macOS-specific quit command implementation in NW.js applications.

Key areas tested include:
  • Platform-specific execution (Darwin/macOS only)
  • Command+Q keyboard shortcut functionality
  • Process termination verification
  • Application cleanup on exit

Implementation Analysis

The testing approach combines Selenium WebDriver for application control with PyAutoGUI for system-level keyboard input simulation.

Notable implementation patterns include:
  • Chrome WebDriver configuration with custom NW.js app arguments
  • Automated keyboard shortcut simulation
  • Process termination verification
  • Platform-specific test targeting

Technical Details

Testing tools and configuration:
  • Selenium WebDriver with Chrome options
  • PyAutoGUI for keyboard simulation
  • Custom Chrome driver configuration
  • Platform detection for macOS-specific execution
  • Process monitoring utilities
  • Implicit wait timing controls

Best Practices Demonstrated

The test implementation showcases several testing best practices for desktop application automation.

Key practices include:
  • Proper test cleanup and driver management
  • Platform-specific test isolation
  • Explicit timing controls for UI operations
  • Error handling with try/finally blocks
  • Modular test utility integration

nwjs/nwJs

test/full/issue4118-mac-quit/test.py

            
import time
import os
import pyautogui
import platform
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

if platform.system() != "Darwin":
    sys.exit(0)

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(10)
time.sleep(1)
try:
    print driver.current_url
    pyautogui.hotkey('command', 'q', interval=0.1)
    print 'key sent'
    time.sleep(3) # wait for quit
    assert(no_live_process(driver))
finally:
    driver.quit()