Back to Repositories

Testing WebView Node.js Integration in NW.js

This test suite validates the integration of Node.js functionality within WebView components of NW.js applications. It specifically tests the ability to access Node.js version information from within a WebView context, ensuring proper communication between the WebView and Node.js runtime.

Test Coverage Overview

The test coverage focuses on verifying WebView-Node.js integration functionality in NW.js applications. Key areas tested include:
  • WebView initialization and loading
  • Node.js version access from WebView context
  • Window handling and URL navigation
  • Browser automation with Selenium WebDriver

Implementation Analysis

The testing approach utilizes Selenium WebDriver with Chrome options specifically configured for NW.js WebView testing. It implements a robust automation pattern that:
  • Configures Chrome WebDriver with WebView-specific settings
  • Manages window switching and URL verification
  • Implements proper test cleanup and resource management
  • Uses Python’s urllib for file path handling

Technical Details

Testing tools and configuration include:
  • Selenium WebDriver for browser automation
  • Chrome Options for WebView configuration
  • Custom path2url conversion utility
  • Environment-specific ChromeDriver setup
  • Implicit wait timing configurations
  • Custom nw_util helper functions

Best Practices Demonstrated

The test implementation showcases several testing best practices:
  • Proper exception handling and cleanup
  • Modular test utility functions
  • Clear assertion conditions
  • Flexible path handling for cross-platform compatibility
  • Structured setup and teardown process
  • Detailed logging configuration

nwjs/nwJs

test/sanity/webview-node/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"}

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_url(driver, 'webview.html')
    result = driver.find_element_by_id('ret').get_attribute('innerHTML')
    print(result)
    assert('version = v' in result)
finally:
    driver.quit()