Back to Repositories

Testing Window State Persistence Implementation in NW.js

This test suite validates window ID persistence and maximization behavior in NW.js across multiple application launches. It ensures proper state management and window position restoration functionality through automated browser testing.

Test Coverage Overview

The test coverage focuses on window state persistence and maximization handling in NW.js applications.

  • Validates window ID preservation across application restarts
  • Tests window position state management
  • Verifies data directory handling and cleanup
  • Covers cross-platform compatibility (MacOS, Linux, Windows)

Implementation Analysis

The implementation utilizes Selenium WebDriver with Python to automate browser interactions and verify window behaviors.

  • Leverages Chrome WebDriver for NW.js automation
  • Implements platform-specific executable path handling
  • Uses file-based verification mechanisms
  • Manages test data directory lifecycle

Technical Details

  • Python test framework with Selenium WebDriver integration
  • Chrome Options configuration for NW.js testing
  • Platform-specific path handling for different OS environments
  • File-based state verification using succeed.txt and last_pos.txt
  • Custom user data directory management

Best Practices Demonstrated

The test implementation showcases robust testing practices for desktop applications.

  • Proper test cleanup and initialization
  • Cross-platform compatibility handling
  • Deterministic state verification
  • Isolated test environment using separate user data directory
  • Clear assertion points for behavior validation

nwjs/nwJs

test/sanity/window-id-maximize/test.py

            
import os
import sys
import shutil
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

testdir = os.path.dirname(os.path.abspath(__file__))
os.chdir(testdir)

datadir = os.path.join(testdir, 'userdata')
try:
    shutil.rmtree(datadir)
except:
    pass

try:
    os.remove('last_pos.txt')
except:
    pass
try:
    os.remove('succeed.txt')
except:
    pass
try:
    os.remove('failed.txt')
except:
    pass

pkg1 = os.path.dirname(os.environ['CHROMEDRIVER'])

if platform.system() == 'Darwin':
    exe = os.path.join(pkg1, 'nwjs.app', 'Contents', 'MacOS', 'nwjs')
elif platform.system() == 'Linux':
    exe = os.path.join(pkg1, 'nw')
else:
    exe = os.path.join(pkg1, 'nw.exe')


p = subprocess.call([exe, "--user-data-dir="+datadir, "."])

assert(os.path.exists('last_pos.txt'))

p = subprocess.call([exe, "--user-data-dir="+datadir, "."])

assert(os.path.exists('succeed.txt'))