Back to Repositories

Validating Node-Remote Array Configuration in NW.js

This test suite validates node-remote array functionality in NW.js using Selenium WebDriver and Python. It tests the configuration of node-remote permissions through package.json and verifies remote URL access patterns.

Test Coverage Overview

The test suite provides comprehensive coverage of node-remote array configuration in NW.js applications. It specifically tests:

  • Dynamic package.json manifest generation with node-remote permissions
  • Local HTTP server setup and port allocation
  • Remote URL access validation through Selenium WebDriver
  • Integration between Node.js runtime and remote content loading

Implementation Analysis

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

  • Dynamic test environment setup with runtime port allocation
  • Chrome WebDriver configuration with NW.js specific arguments
  • Asynchronous server process management
  • Automated cleanup of test resources

Technical Details

Key technical components include:

  • Python 3.x runtime environment
  • Selenium WebDriver with Chrome driver
  • Local HTTP server implementation
  • Custom nw_util helper functions
  • Package.json manifest manipulation
  • Process management for server cleanup

Best Practices Demonstrated

The test implementation showcases several testing best practices:

  • Proper resource cleanup in finally blocks
  • Dynamic port allocation to prevent conflicts
  • Isolation of test environment through directory management
  • Clear assertion conditions for test validation
  • Structured setup and teardown procedures

nwjs/nwJs

test/sanity/node-remote-array/test.py

            
import time
import os
import subprocess
import sys

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",
  "node-remote":["<all_urls>"],
  "main":"http://localhost:%s/index.html"
}
''' % (port))
manifest.close()

sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from nw_util import *

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(driver, 'result')
    print(result)
    assert("success" in result)
finally:
    server.terminate()
    driver.quit()