Back to Repositories

Testing HTTP Download and Garbage Collection Integration in NW.js

This test suite validates HTTP download functionality and garbage collection in NW.js applications using Selenium WebDriver. It creates a local HTTP server, downloads an image file, and verifies proper memory management through garbage collection.

Test Coverage Overview

The test ensures reliable file downloading and memory management in NW.js applications.

Key areas covered:
  • HTTP file download functionality
  • Stream handling and file writing
  • Garbage collection triggering
  • Local HTTP server interaction

Implementation Analysis

The test implements a Python-Selenium based approach using Chrome WebDriver for browser automation. It creates a temporary HTTP server, generates a test HTML file dynamically, and validates the download process through DOM element checking.

Notable patterns:
  • Dynamic HTML content generation
  • Asynchronous download handling
  • WebDriver wait conditions
  • Resource cleanup management

Technical Details

Testing tools and configuration:
  • Selenium WebDriver with Chrome Options
  • Python HTTP server for file serving
  • Custom port allocation for server
  • NW.js require() for Node.js functionality
  • File system operations for test setup
  • Garbage collection verification

Best Practices Demonstrated

The test exemplifies robust testing practices for browser-based applications.

Quality aspects:
  • Proper resource cleanup in finally block
  • Dynamic port allocation to prevent conflicts
  • Explicit wait conditions for async operations
  • Isolation of test environment
  • Clear success criteria verification

nwjs/nwJs

test/sanity/issue4096-download/test.py

            
import time
import os
import subprocess
import sys

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

sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from nw_util import *

chrome_options = Options()
chrome_options.add_argument("nwapp=" + os.path.dirname(os.path.abspath(__file__)))

capabilities = {"pageLoadStrategy": "none"}

testdir = os.path.dirname(os.path.abspath(__file__))
os.chdir(testdir)

port = str(utils.free_port())
server = subprocess.Popen(['python3', 'http-server.py', port])

html = open('index.html', 'w')
html.write('''
<script>
var http = nw.require('http');
var fs = nw.require('fs');

function testgc() {
  gc();
  document.write('<h1 id="res">success</h1>');
}

var file = fs.createWriteStream("out.png");
var req = http.get("http://127.0.0.1:%s/g.png", function (res) {
    res.pipe(file);
    setTimeout(testgc, 1000);
});
</script>
''' % (port))
    
html.close()

driver = webdriver.Chrome(executable_path=os.environ['CHROMEDRIVER'], chrome_options=chrome_options, desired_capabilities = capabilities, service_log_path="log", service_args=["--verbose"])
try:
    print(driver.current_url)
    result = wait_for_element_id(driver, 'res')
    print(result)
    assert("success" in result)
finally:
    server.terminate()
    driver.quit()