Back to Repositories

Testing Remote Window Management Implementation in NW.js

This test suite validates remote window opening functionality in NW.js using Selenium WebDriver and Python. It tests the interaction between local and remote windows while verifying window object properties and access restrictions.

Test Coverage Overview

The test suite provides comprehensive coverage of NW.js window management functionality.

Key areas tested include:
  • Remote window opening via nw.Window.open API
  • Window object type verification
  • Cross-window communication and property access
  • Security restrictions on remote windows

Implementation Analysis

The implementation uses Selenium WebDriver with Python to automate browser interactions and validate window behaviors. It employs a local HTTP server to simulate remote content access, while utilizing custom wait conditions and window switching logic to ensure reliable test execution.

Notable patterns include:
  • Dynamic port allocation for test server
  • Window context switching validation
  • Asynchronous window operation handling

Technical Details

Testing tools and configuration:
  • Selenium WebDriver with Chrome
  • Python HTTP server for remote content
  • Custom nw_util helper functions
  • Chrome options with nwapp argument
  • Page load strategy configuration
  • Dynamic HTML content generation

Best Practices Demonstrated

The test implementation showcases several testing best practices.

Notable examples include:
  • Proper test cleanup and resource management
  • Explicit wait conditions for async operations
  • Robust error handling and assertions
  • Modular test setup with utility functions
  • Clear separation of test setup and execution

nwjs/nwJs

test/sanity/open-remote/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
from selenium.webdriver.common import utils

chrome_options = Options()
chrome_options.add_argument("nwapp=" + os.path.dirname(os.path.abspath(__file__)))

capabilities = {"pageLoadStrategy": "none"}

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

port = str(utils.free_port())
server = subprocess.Popen(['python3', 'http-server.py', port])

html = open('index.html', 'w')
html.write('''
<script>
window.name = 'local';
nw.Window.open('http://localhost:%s/remote.html', function(win) {
  document.write('<h1 id="res">returned window is ' + typeof win + '</h1>');
  win.y = 0;
});
</script>
''' % (port))
    
html.close()

driver = webdriver.Chrome(executable_path=os.environ['CHROMEDRIVER'], chrome_options=chrome_options, desired_capabilities = capabilities)
try:
    wait_switch_window_name(driver, 'local')
    print(driver.current_url)
    result = wait_for_element_id(driver, 'res')
    print('result=' + result)
    assert("object" in result)
    wait_switch_window_name(driver, 'remote')
    for id in ['res', 'res2', 'res3']:
        result = wait_for_element_id(driver, id)
        print(result)
        assert("DISABLED" in result)
finally:
    server.terminate()
    driver.quit()