Back to Repositories

Testing MacOS Window Title Management in NW.js

This test suite validates the window title functionality for macOS applications in NW.js, focusing specifically on issue #4186. It implements automated testing using Selenium WebDriver and Apple’s NSAppleScript to verify correct window title display and handling.

Test Coverage Overview

The test coverage focuses on macOS-specific window title verification, ensuring proper integration between NW.js and native macOS window management.

  • Platform-specific execution checks for macOS
  • Window title retrieval and validation
  • System-level window management testing
  • Error handling for non-Mac environments

Implementation Analysis

The testing approach combines Selenium WebDriver with native macOS AppleScript integration to validate window title functionality.

  • Uses NSAppleScript for native macOS window title access
  • Implements Chrome WebDriver for application control
  • Handles platform-specific execution logic
  • Employs assertion-based validation

Technical Details

  • Selenium WebDriver with Chrome options
  • NSAppleScript for macOS system integration
  • Python test framework
  • Platform detection utilities
  • Environment-specific ChromeDriver configuration
  • Implicit wait timing controls

Best Practices Demonstrated

The test implementation showcases robust cross-platform testing practices with proper error handling and resource cleanup.

  • Proper test isolation and cleanup
  • Platform-specific conditional execution
  • Error handling for AppleScript execution
  • Resource management using try-finally blocks
  • Clear assertion conditions

nwjs/nwJs

test/full/issue4186-mac-title/test.py

            
import time
import os
import platform
import sys

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

if platform.system() != 'Darwin':
    print 'skipped for non Mac system'
    sys.exit(0)

def get_title():
    from Foundation import NSAppleScript

    src = '''
    global frontApp, frontAppName, windowTitle
    tell application "System Events"
        set frontApp to first application process whose frontmost is true
        set frontAppName to name of frontApp
        tell process frontAppName
            tell (1st window whose value of attribute "AXMain" is true)
                set windowTitle to value of attribute "AXTitle"
            end tell
        end tell
    end tell
    return windowTitle
    '''

    s = NSAppleScript.alloc().initWithSource_(src)
    r, e = s.executeAndReturnError_(None)
    if e:
        print e['NSAppleScriptErrorBriefMessage']
        raise Exception(e['NSAppleScriptErrorBriefMessage'])
    return r.stringValue()

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)
time.sleep(1)
try:
    print driver.current_url
    title = get_title()
    print title
    assert(title == "issue4186-mac-title")
finally:
    driver.quit()