Back to Repositories

Testing Command-Line Argument Processing Workflow in NW.js

This test suite validates command-line argument handling in NW.js applications using Selenium WebDriver. It specifically examines how the application processes and exposes command-line arguments through nw.App.argv and nw.App.fullArgv properties.

Test Coverage Overview

The test ensures proper handling of command-line arguments in NW.js applications.

Key areas covered include:
  • Verification of filtered arguments in nw.App.argv
  • Validation of complete argument list in nw.App.fullArgv
  • Checking proper filtering of internal ‘nwapp=’ argument

Implementation Analysis

The test utilizes Selenium WebDriver with Chrome options to automate the testing process. It implements a straightforward approach by launching a Chrome instance with specific NW.js arguments and verifying the exposed values through DOM elements.

The implementation leverages Selenium’s element location capabilities and assertion patterns to validate argument handling behavior.

Technical Details

Tools and configurations used:
  • Selenium WebDriver for browser automation
  • Chrome Options for NW.js app configuration
  • Python’s assertion mechanism for validation
  • Environment variable integration for ChromeDriver path
  • Implicit wait timing of 5 seconds

Best Practices Demonstrated

The test demonstrates several testing best practices:

  • Proper resource cleanup with try-finally block
  • Clear separation of setup and verification steps
  • Explicit assertion messages
  • Robust element location strategy
  • Configurable driver path through environment variables

nwjs/nwJs

test/sanity/app-argv/test.py

            
import time
import os

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument("nwapp=" + os.path.dirname(os.path.abspath(__file__)))

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-argv').get_attribute('innerHTML')
    print('nw.App.argv = %s' % result)
    assert('nwapp=' not in result)
    result = driver.find_element_by_id('result-fullargv').get_attribute('innerHTML')
    print('nw.App.fullArgv = %s' % result)
    assert('nwapp=' in result)
finally:
    driver.quit()