Back to Repositories

Testing Remote Window Opening Functionality in NW.js

This test suite validates remote window opening functionality in NW.js using Selenium WebDriver and Python. It sets up a local HTTP server and tests the window.open() behavior in a mixed-context environment.

Test Coverage Overview

The test ensures proper functionality of remote window opening in NW.js applications.

Key areas covered include:
  • Window opening in mixed-context environment
  • HTTP server interaction
  • Dynamic content generation
  • Cross-window communication verification

Implementation Analysis

The test implements a Selenium WebDriver approach using Python to automate browser interactions. It dynamically generates test content by replacing template variables and validates window opening behavior through DOM manipulation and event handling.

Key patterns include:
  • Dynamic port allocation for test server
  • Template-based HTML generation
  • Chrome WebDriver configuration with NW.js specifics
  • Automated UI interaction and assertion validation

Technical Details

Testing tools and configuration:
  • Selenium WebDriver with Chrome options
  • Python HTTP server for local testing
  • Custom Chrome options for NW.js context
  • Template-based test content generation
  • Implicit wait handling for async operations
  • Resource cleanup through finally block

Best Practices Demonstrated

The test exemplifies robust testing practices for desktop applications.

Notable practices include:
  • Proper resource management and cleanup
  • Dynamic port allocation to prevent conflicts
  • Separation of template and generated content
  • Explicit wait conditions for reliability
  • Error handling and process termination

nwjs/nwJs

test/sanity/issue4180-remote-win-open/test.py

            
import time
import os
import subprocess

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__)))
chrome_options.add_argument("mixed-context")

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

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

tpl = open('index.tpl', 'r')
content = tpl.read().replace('{port}', port)
tpl.close()

html = open('index.html', 'w')
html.write(content)
html.close()

driver = webdriver.Chrome(executable_path=os.environ['CHROMEDRIVER'], chrome_options=chrome_options, service_log_path="log", service_args=["--verbose"])
try:
    print(driver.current_url)
    driver.implicitly_wait(10)
    driver.find_element_by_id('winopen').click()
    result = driver.find_element_by_id('result').get_attribute('innerHTML')
    print(result)
    assert("success" in result)
finally:
    server.terminate()
    driver.quit()