Back to Repositories

Testing WebView Node Remote Integration in NW.js

This test suite validates WebView node remote functionality in NW.js, ensuring proper communication between WebView components and Node.js environments. It verifies version information retrieval and window handling through Selenium WebDriver integration.

Test Coverage Overview

The test suite covers WebView integration with Node.js remote components, focusing on version information verification and window state management.

  • WebView initialization and loading
  • Node.js version information retrieval
  • Window switching and URL validation
  • Cross-component communication verification

Implementation Analysis

The implementation utilizes Selenium WebDriver with Chrome options for WebView testing, incorporating Python’s subprocess management for server control. The test employs dynamic port allocation and template-based HTML generation for reliable test execution.

  • Dynamic server port configuration
  • Template-based test page generation
  • Chrome WebDriver customization
  • Cross-platform process management

Technical Details

  • Selenium WebDriver with Chrome options
  • Python subprocess management
  • Dynamic port allocation using utils.free_port()
  • HTML template processing
  • Platform-specific process termination
  • Implicit wait timing configuration
  • Custom Chrome capabilities for page loading strategy

Best Practices Demonstrated

The test implementation showcases robust testing practices with proper resource cleanup and cross-platform compatibility.

  • Proper test cleanup and process termination
  • Dynamic resource allocation
  • Platform-agnostic implementation
  • Explicit assertion checking
  • Structured error handling

nwjs/nwJs

test/sanity/webview-node-remote/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 *

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

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

svrprog = os.path.abspath(os.path.join(os.path.dirname(testdir), 'http-server-node.py'))

port = str(utils.free_port())
server = subprocess.Popen(['python', svrprog, port])

tpl = open('window.tpl', 'r')
content = tpl.read().replace('{port}', port)
tpl.close()

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

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:
    import platform
    if platform.system() == 'Windows':
        subprocess.call(['taskkill', '/F', '/T', '/PID', str(server.pid)])
    else:
        server.terminate()
    driver.quit()