Back to Repositories

Testing Node Remote Access Restrictions in NW.js

This test suite validates node remote functionality in NW.js by implementing a Python-based integration test that verifies remote node access restrictions. The test uses Selenium WebDriver to automate browser interactions and validates security boundaries for node remote access.

Test Coverage Overview

The test suite provides comprehensive coverage of node remote access restrictions in NW.js applications.

Key areas tested include:
  • Remote URL loading via package.json configuration
  • Node integration security boundaries
  • HTTP server interaction validation
  • Cross-origin restrictions verification

Implementation Analysis

The testing approach utilizes Selenium WebDriver with Python to automate browser interactions and validate node remote functionality.

Key implementation patterns include:
  • Dynamic port allocation for HTTP server
  • Runtime package.json configuration
  • Automated Chrome instance management
  • Cross-platform process handling

Technical Details

Testing tools and configuration:
  • Selenium WebDriver for browser automation
  • Python HTTP server for serving test content
  • Chrome options configuration for NW.js testing
  • Platform-specific process management
  • Custom utility functions for element waiting

Best Practices Demonstrated

The test implementation showcases several testing best practices for NW.js applications.

Notable practices include:
  • Proper test cleanup and resource management
  • Cross-platform compatibility handling
  • Dynamic configuration generation
  • Explicit wait patterns for reliable testing
  • Structured error handling and logging

nwjs/nwJs

test/sanity/node-remote-negtive/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 *
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__)))

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

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

manifest = open('package.json', 'w')
manifest.write('''
{
  "name":"test-node-remote",
  "main":"http://localhost:%s/index.html"
}
''' % (port))
manifest.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)
    result = wait_for_element_id_content(driver, 'result', 'success')
    print(result)
finally:
    import platform
    if platform.system() == 'Windows':
        subprocess.call(['taskkill', '/F', '/T', '/PID', str(server.pid)])
    else:
        server.terminate()
    driver.quit()