Back to Repositories

Testing Mixed Context Window Handling in NW.js

This test suite validates mixed context handling in NW.js applications using Selenium WebDriver. It specifically tests the interaction between different window contexts and object rendering behaviors.

Test Coverage Overview

The test coverage focuses on verifying window handling and object rendering in mixed contexts.

Key areas tested include:
  • Window spawning and handle management
  • Object rendering in new windows
  • Context switching between multiple windows
  • Element attribute verification

Implementation Analysis

The implementation uses Selenium WebDriver to automate browser interactions and validate mixed context behaviors. The test leverages Chrome options for NW.js app testing and implements explicit window handle management.

Key patterns include:
  • Dynamic path configuration for test resources
  • WebDriver initialization with custom Chrome options
  • Explicit window handle switching
  • Element interaction and attribute verification

Technical Details

Testing tools and configuration:
  • Selenium WebDriver for browser automation
  • Chrome Options for NW.js app configuration
  • Custom utility functions from nw_util
  • Environment-based ChromeDriver path
  • Try-finally block for proper resource cleanup

Best Practices Demonstrated

The test demonstrates robust automation practices for complex UI testing scenarios.

Notable practices include:
  • Proper resource cleanup with driver.quit()
  • Explicit waits for window handles
  • Clear separation of setup and test logic
  • Error handling with try-finally blocks
  • Modular test utility integration

nwjs/nwJs

test/sanity/issue7609-mixed-context/test.py

            
import time
import os
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()
chrome_options.add_argument("nwapp=" + os.path.dirname(os.path.abspath(__file__)))

driver = webdriver.Chrome(executable_path=os.environ['CHROMEDRIVER'], chrome_options=chrome_options)
try:
    print(driver.current_url)
    driver.find_element_by_id('spawnRender').click()
    print('wait for window open')
    wait_window_handles(driver, 2)
    print(driver.window_handles)
    print('switch to 1st window')
    driver.switch_to.window(driver.window_handles[-1])
    result1 = driver.find_element_by_id('result').get_attribute('innerHTML')
    print(result1)
    assert('object' in result1) 
finally:
    driver.quit()