Back to Repositories

Testing NW.js Package Loading and Deployment Scenarios in nw.js

This test suite validates NW.js package handling across different packaging methods and platforms. It tests package loading, compression, and binary appending functionality using Selenium WebDriver for automated verification.

Test Coverage Overview

The test suite covers three critical package deployment scenarios for NW.js applications:
  • Basic package loading from directory structure
  • Compressed package loading using ZIP format
  • Binary-appended package loading (non-Darwin platforms)
Each scenario verifies successful application loading and execution through DOM element validation.

Implementation Analysis

The implementation utilizes Selenium WebDriver for automated testing, with platform-specific logic handling package creation and verification. The test employs cross-platform compatibility checks and filesystem operations to prepare test environments, demonstrating robust package management across different OS configurations.

Technical Details

Key technical components include:
  • Selenium WebDriver with Chrome integration
  • Python’s zipfile module for package compression
  • Platform-specific path handling
  • File system operations for package preparation
  • Binary file manipulation for package embedding

Best Practices Demonstrated

The test implementation showcases several testing best practices:
  • Proper resource cleanup and error handling
  • Platform-aware test configuration
  • Isolated test environments for each scenario
  • Automated verification of success criteria
  • Comprehensive coverage of deployment scenarios

nwjs/nwJs

test/sanity/package/test.py

            
import time
import os
import shutil
import zipfile
import platform

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

testdir = os.path.dirname(os.path.abspath(__file__))
nwdist = os.path.join(os.path.dirname(os.environ['CHROMEDRIVER']), 'nwdist')

appdir =  os.path.join(testdir, 'app')
pkg1 = os.path.join(testdir, 'pkg1')
pkg2 = os.path.join(testdir, 'pkg2')
pkg3 = os.path.join(testdir, 'pkg3')

try:
    shutil.rmtree(pkg1)
    shutil.rmtree(pkg2)
    shutil.rmtree(pkg3)
except:
    pass

def compress(from_dir, to_file):
    from_dir = os.path.normpath(from_dir)
    to_file = os.path.normpath(to_file)
    
    z = zipfile.ZipFile(to_file, 'w', compression=zipfile.ZIP_DEFLATED)
    for root, dirs, files in os.walk(from_dir):
        for f in files:
            _path = os.path.join(root, f)
            z.write(_path, _path.replace(from_dir+os.sep, ''))
    z.close()

def copytree(src, dst, symlinks=False, ignore=None):
    if not os.path.exists(dst):
        os.makedirs(dst)
    for item in os.listdir(src):
        s = os.path.join(src, item)
        d = os.path.join(dst, item)
        if os.path.isdir(s):
            copytree(s, d, symlinks, ignore)
        else:
            if not os.path.exists(d) or os.stat(s).st_mtime - os.stat(d).st_mtime > 1:
                shutil.copy2(s, d)
os.mkdir(pkg1)
print("copying %s to %s" % (nwdist, pkg1))
copytree(nwdist, pkg1)
if platform.system() == 'Darwin':
    appnw = os.path.join(pkg1, 'nwjs.app', 'Contents', 'Resources', 'app.nw')
    print("copying %s to %s" % (appdir, appnw))
    copytree(appdir, appnw)
else:
    print("copying %s to %s" % (appdir, pkg1))
    copytree(appdir, pkg1)

#chrome_options = Options()
#chrome_options.add_argument("nwapp=" + pkg1)
driver_path=os.path.join(pkg1, 'chromedriver')
driver = webdriver.Chrome(executable_path=driver_path)
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:
    driver.quit()


######## test compressed package

os.mkdir(pkg2)
print("copying %s to %s" % (nwdist, pkg2))
copytree(nwdist, pkg2)
if platform.system() == 'Darwin':
    appnw = os.path.join(pkg2, 'nwjs.app', 'Contents', 'Resources', 'app.nw')
    print("compressing %s to %s" % (appdir, appnw))
    compress(appdir, appnw)
else:
    package_nw = os.path.join(pkg2, 'package.nw')
    print("compressing %s to %s" % (appdir, package_nw))
    compress(appdir, package_nw)

driver_path=os.path.join(pkg2, 'chromedriver')
driver2 = webdriver.Chrome(executable_path=driver_path)
driver2.implicitly_wait(5)
try:
    print(driver2.current_url)
    result = driver2.find_element_by_id('result')
    print(result.get_attribute('innerHTML'))
    assert("success" in result.get_attribute('innerHTML'))
finally:
    driver2.quit()

######## test appending-to-binary package

if platform.system() != 'Darwin':
    os.mkdir(pkg3)
    print("copying %s to %s" % (nwdist, pkg3))
    copytree(nwdist, pkg3)
    package_nw = os.path.join(pkg3, 'package.nw')
    print("compressing %s to %s" % (appdir, package_nw))
    compress(appdir, package_nw)
    if platform.system() == 'Linux':
        nwbin = os.path.join(pkg3, 'nw')
    else:
        nwbin = os.path.join(pkg3, 'nw.exe')
    with open(nwbin, "ab") as myfile, open(package_nw, "rb") as file2:
            myfile.write(file2.read())
    os.remove(package_nw)

    driver_path=os.path.join(pkg3, 'chromedriver')
    driver3 = webdriver.Chrome(executable_path=driver_path)
    time.sleep(1)
    try:
        print(driver3.current_url)
        result = driver3.find_element_by_id('result')
        print(result.get_attribute('innerHTML'))
        assert("success" in result.get_attribute('innerHTML'))
    finally:
        driver3.quit()