Back to Repositories

Testing Multiple Instance Event Handling in NW.js

This test suite validates the app-open-event functionality in NW.js by launching multiple application instances and verifying event handling. It utilizes Selenium WebDriver to automate browser interactions and verify proper event propagation between application instances.

Test Coverage Overview

The test coverage focuses on validating the application open event handling mechanism in NW.js. Key functionality tested includes:

  • Multiple instance launch detection
  • Event propagation between instances
  • User data directory handling
  • Chrome driver integration

Implementation Analysis

The testing approach employs Selenium WebDriver with Python to automate browser instance creation and verification. The implementation utilizes Chrome options for NW.js app configuration and manages user data directories for isolated testing environments.

Key patterns include subprocess management for multiple instances and wait conditions for event synchronization.

Technical Details

Testing tools and configuration:

  • Selenium WebDriver with Chrome driver
  • Python subprocess management
  • Custom nw_util helper functions
  • Chrome options configuration for NW.js
  • User data directory isolation
  • Verbose logging enabled

Best Practices Demonstrated

The test implementation showcases several testing best practices:

  • Proper resource cleanup in finally block
  • Isolation of test environment using separate user data directories
  • Explicit wait conditions for async operations
  • Structured logging and debugging support
  • Modular test utility functions

nwjs/nwJs

test/sanity/app-open-event/test.py

            
import time
import os
import subprocess
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()

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

chrome_options.add_argument("nwapp=" + testdir)
chrome_options.add_argument("user-data-dir=" + os.path.join(testdir, 'userdata'))

driver = webdriver.Chrome(executable_path=os.environ['CHROMEDRIVER'], chrome_options=chrome_options, service_log_path="log", service_args=["--verbose"])
try:
    print((driver.current_url))
    second_instance = subprocess.Popen(['python3', 'second_instance.py'])
    result = wait_for_element_id_content(driver, 'result', 'success')
    print(result)
finally:
    driver.quit()
    #second_instance.kill()