Back to Repositories

Testing Remote File Access Pattern Validation in NW.js

This test suite validates remote file access patterns in NW.js applications using Selenium WebDriver for automated browser testing. It specifically examines URL pattern matching and security restrictions for file access permissions.

Test Coverage Overview

The test suite covers URL pattern matching functionality for file access permissions in NW.js applications.

Key areas tested include:
  • Universal URL pattern matching (‘all_urls’)
  • Empty pattern handling
  • File protocol pattern matching
  • Non-existent directory pattern validation

Implementation Analysis

The testing approach utilizes Selenium WebDriver with Chrome options to automate browser interactions. It implements a pattern-based testing strategy using a template system to validate different URL access scenarios.

Technical implementation includes:
  • Dynamic package.json generation from templates
  • URL path conversion utilities
  • Automated Chrome driver configuration

Technical Details

Testing tools and configuration:
  • Selenium WebDriver for browser automation
  • Chrome driver with custom NW.js app configuration
  • Python’s urllib for URL handling
  • Template-based test case generation
  • Custom utility functions for element waiting and verification

Best Practices Demonstrated

The test implementation showcases several testing best practices including proper test isolation, clear test case organization, and robust cleanup.

Notable practices:
  • Proper resource cleanup with driver.quit()
  • Parameterized test patterns
  • Clear test case documentation
  • Consistent error handling

nwjs/nwJs

test/sanity/issue7183-node-remote-file/test.py

            
import time
import os
import urllib.parse, urllib.request, urllib.parse, urllib.error
import sys
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from nw_util import *

def path2url(path):
        return urllib.parse.urljoin(
                  'file:', urllib.request.pathname2url(path))

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

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

chrome_options = Options()
chrome_options.add_argument("nwapp=" + testdir)

htmlfile = os.path.join(testdir, 'index.html')
localurl = path2url(htmlfile)

tpl = open('package.json.tpl', 'r')
template = tpl.read().replace('{localurl}', localurl)
tpl.close()

def test_pattern(pattern, expect_nw):
    content = template.replace('{pattern}', pattern)

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

    driver = webdriver.Chrome(executable_path=os.environ['CHROMEDRIVER'], chrome_options=chrome_options)
    driver.implicitly_wait(5)
    try:
        print("testing pattern %s, expect nw: %s" % (pattern, expect_nw))
        result = wait_for_element_id_content(driver, 'nw_access', expect_nw)
    finally:
        driver.quit()

test_pattern('"<all_urls>"', 'yes')
test_pattern('', 'no')
test_pattern('"file://*"', 'yes')
test_pattern('"file:///dir/not/exists/*"', 'no')