Back to Repositories

Testing WebView Local Resource Integration in NW.js

This test suite validates WebView local resource handling in NW.js applications, focusing on proper file URL resolution and resource loading within WebView components. It ensures secure and reliable access to local files through WebView instances.

Test Coverage Overview

The test suite provides comprehensive coverage of WebView’s local resource handling capabilities.

  • Tests URL path conversion for local file access
  • Validates WebView initialization with local content
  • Verifies proper loading of local HTML resources
  • Checks cross-origin resource handling

Implementation Analysis

The implementation utilizes Selenium WebDriver with Chrome options specifically configured for NW.js WebView testing. The test employs dynamic HTML content generation and template-based file manipulation to create test scenarios.

  • Uses Python’s urllib for URL handling
  • Implements custom WebView window management
  • Employs Chrome WebDriver for automation

Technical Details

  • Selenium WebDriver with Chrome integration
  • Python’s urllib for path-to-URL conversion
  • Custom nw_util helper functions
  • Chrome Options with WebView-specific configurations
  • Template-based HTML content generation
  • Platform-specific path handling

Best Practices Demonstrated

The test demonstrates robust error handling and resource cleanup practices in automated testing.

  • Proper driver cleanup in finally block
  • Platform-independent path handling
  • Explicit wait mechanisms for WebView switching
  • Structured test setup and teardown
  • Clear assertion conditions

nwjs/nwJs

test/sanity/webview-localres/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)
chrome_options.add_experimental_option("windowTypes", ["webview"])

capabilities = {"pageLoadStrategy": "none"}

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

tpl = open('index.tpl', 'r')
if sys.platform == 'win32':
    htmlfile = htmlfile.replace('\\', '\\\\')
content = tpl.read().replace('{localfile}', htmlfile)
content = content.replace('{localdirurl}', localurl)
tpl.close()

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

driver = webdriver.Chrome(executable_path=os.environ['CHROMEDRIVER'], chrome_options=chrome_options, desired_capabilities = capabilities, service_log_path="log", service_args=["--verbose"])
driver.implicitly_wait(5)
try:
    print(driver.current_url)
    wait_switch_window_name(driver, 'webview1')
    result = driver.find_element_by_id('result').get_attribute('innerHTML')
    print(result)
    assert('success' in result)
finally:
    driver.quit()