Back to Repositories

Validating Node-Remote Integration Testing in NW.js

This test suite validates the node-remote functionality in NW.js using Selenium WebDriver with Python. It tests the ability to load remote content and execute Node.js code within the NW.js environment, ensuring proper integration between web and Node.js contexts.

Test Coverage Overview

The test suite provides comprehensive coverage of node-remote configuration and execution.

Key areas tested include:
  • Remote content loading via localhost server
  • Node.js integration with web context
  • Package manifest configuration
  • Cross-platform compatibility

Implementation Analysis

The implementation uses Selenium WebDriver with Python to automate browser testing.

Notable patterns include:
  • Dynamic port allocation for test server
  • Custom Chrome options configuration
  • Automated manifest generation
  • Platform-specific process management

Technical Details

Testing tools and configuration:
  • Selenium WebDriver with Chrome
  • Python HTTP server for serving test content
  • Dynamic package.json generation
  • Chrome options with nwapp argument
  • Platform-specific process termination handling

Best Practices Demonstrated

The test implementation showcases several testing best practices.

Notable practices include:
  • Proper resource cleanup in finally block
  • Cross-platform compatibility handling
  • Dynamic port allocation to prevent conflicts
  • Explicit waits for element presence
  • Structured test setup and teardown

nwjs/nwJs

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

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()

driver = webdriver.Chrome(executable_path=os.environ['CHROMEDRIVER'], chrome_options=chrome_options, service_log_path="log", service_args=["--verbose"])
driver.implicitly_wait(5)
try:
    print(driver.current_url)
    result = driver.find_element_by_id('result')
    print(result.get_attribute('innerHTML'))
    assert("success" in result.get_attribute('innerHTML'))
finally:
    import platform
    if platform.system() == 'Windows':
        subprocess.call(['taskkill', '/F', '/T', '/PID', str(server.pid)])
    else:
        server.terminate()
    driver.quit()