Back to Repositories

Testing Window Management and HTTP Request Handling in NW.js

This test suite validates window handling and HTTP request functionality in NW.js applications using Selenium WebDriver. It specifically tests the ability to open and close windows while maintaining proper HTTP communication.

Test Coverage Overview

The test suite covers critical window management operations and HTTP request handling in NW.js.

Key areas tested include:
  • Window opening and closing operations
  • HTTP server communication verification
  • Window handle state management
  • Request success validation after window operations

Implementation Analysis

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

Key patterns include:
  • Dynamic port allocation for HTTP server
  • Template-based HTML content generation
  • Explicit wait conditions for window handles
  • Chrome WebDriver configuration with NW.js specific options

Technical Details

Testing tools and configuration:
  • Selenium WebDriver with Chrome options
  • Python HTTP server for request testing
  • Custom utility functions for window handle management
  • Template-based test page generation
  • Mixed-context mode configuration
  • ChromeDriver with verbose logging

Best Practices Demonstrated

The test demonstrates robust testing practices for desktop applications.

Notable practices include:
  • Proper resource cleanup in finally block
  • Dynamic port allocation to prevent conflicts
  • Implicit wait implementation for stability
  • Modular test page template approach
  • Comprehensive error logging setup

nwjs/nwJs

test/sanity/issue4130-win-open-close-fail-http/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__)))
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('open-win').click()
    wait_window_handles(driver, 2)
    driver.find_element_by_id('close-win').click()
    wait_window_handles(driver, 1)
    driver.find_element_by_id('test-request').click()
    result = driver.find_element_by_id('result').get_attribute('innerHTML')
    print(result)
    assert("success" in result)
finally:
    server.terminate()
    driver.quit()