Back to Repositories

Testing Node.js Main Script Integration in NW.js

This test suite implements an automated Selenium-based verification of node-main functionality in NW.js applications. It validates the proper initialization and execution of Node.js main scripts through browser automation and dynamic template processing.

Test Coverage Overview

The test provides comprehensive coverage of node-main script execution in NW.js applications.

Key areas tested include:
  • Dynamic port allocation and template processing
  • Browser initialization with custom NW.js arguments
  • Node.js main script execution verification
  • Asynchronous result validation
Integration points span both the Node.js runtime and Chrome browser environments.

Implementation Analysis

The testing approach utilizes Selenium WebDriver to automate Chrome browser interactions with NW.js applications.

Key implementation patterns include:
  • Dynamic template substitution for port configuration
  • Chrome WebDriver setup with NW.js-specific options
  • Implicit wait mechanisms for async operations
  • Polling-based result verification

Technical Details

Testing tools and configuration:
  • Selenium WebDriver with Chrome options
  • Template-based test file generation
  • Environment-configured ChromeDriver path
  • Dynamic port allocation utilities
  • Implicit wait timeouts of 10 seconds
  • Maximum retry counter of 10 iterations

Best Practices Demonstrated

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

Notable practices include:
  • Proper resource cleanup with driver quit
  • Dynamic test environment configuration
  • Robust async operation handling
  • Clear success criteria definition
  • Structured test file organization

nwjs/nwJs

test/sanity/node-main/test.py

            
import time
import os

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

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

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

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

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

driver = webdriver.Chrome(executable_path=os.environ['CHROMEDRIVER'], chrome_options=chrome_options)
try:
    print(driver.current_url)
    driver.implicitly_wait(10)
    driver.find_element_by_tag_name('button').click()
    result = 'node-main test'
    counter = 0
    while not 'success' in result and counter < 10:
        time.sleep(1)
        result = driver.find_element_by_id('result').get_attribute('innerHTML')
        print(result)
        counter = counter + 1
    assert('success' in result)
finally:
    driver.quit()