Back to Repositories

Testing Worker Binary Compilation and Execution in NW.js

This test suite validates the functionality of worker binaries in NW.js using Selenium WebDriver for automation. It focuses on compiling JavaScript files to binary format and verifying their proper execution in a worker context.

Test Coverage Overview

The test coverage focuses on the binary compilation and execution workflow for NW.js workers.

  • Validates JavaScript to binary compilation using nwjc
  • Verifies binary file creation and existence
  • Tests worker execution and result verification
  • Handles cross-platform compatibility scenarios

Implementation Analysis

The implementation uses a combination of Python’s subprocess module and Selenium WebDriver to automate the testing process.

  • Utilizes Chrome WebDriver for browser automation
  • Implements file system operations for binary management
  • Employs assertion-based verification
  • Handles platform-specific binary naming conventions

Technical Details

  • Selenium WebDriver for browser automation
  • Python subprocess module for binary compilation
  • Chrome Options configuration for NW.js integration
  • Custom utility functions for element waiting and verification
  • Platform-aware path handling

Best Practices Demonstrated

The test implementation showcases robust error handling and cleanup practices in automated testing.

  • Proper resource cleanup with try-finally blocks
  • Explicit file existence checks
  • Cross-platform compatibility considerations
  • Clear separation of setup, execution, and verification phases

nwjs/nwJs

test/sanity/worker-nwbin/test.py

            
import time
import os
import shutil
import subprocess
import platform
from subprocess import Popen, PIPE
import sys
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from nw_util import *

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
testdir = os.path.dirname(os.path.abspath(__file__))
chrome_options.add_argument("nwapp=" + testdir)
binfile = os.path.join(testdir, "nw.bin")
nwjc = os.path.join(os.path.dirname(os.environ['CHROMEDRIVER']), "nwjc.exe" if os.name == "nt" else "nwjc")
os.chdir(testdir)
try:
  os.remove(binfile)
except:
  pass
assert(False == os.path.isfile(binfile))
subprocess.call([nwjc, "mytest.js", "nw.bin"])
assert(os.path.isfile(binfile))

driver = webdriver.Chrome(executable_path=os.environ['CHROMEDRIVER'], chrome_options=chrome_options)
try:
    print(driver.current_url)
    result = wait_for_element_id_content(driver, 'result', '840')
    print(result)
    assert("840" == result)
finally:
    driver.quit()