Back to Repositories

Testing WebView JavaScript Injection Functionality in NW.js

This test suite validates JavaScript injection functionality in WebView components within NW.js applications. It specifically tests the proper injection of JavaScript code into both main window and iframe contexts, ensuring consistent behavior across different window environments.

Test Coverage Overview

The test suite provides comprehensive coverage of WebView JavaScript injection scenarios.

Key areas tested include:
  • JavaScript injection in main window context
  • JavaScript injection in iframe context
  • Window handle management
  • Element verification post-injection

Implementation Analysis

The test implements a Selenium WebDriver approach using Python to automate WebView testing. It utilizes Chrome options for WebView configuration and implements explicit wait patterns for reliable element detection.

Notable patterns include:
  • Dynamic port allocation for test server
  • Template-based HTML generation
  • Window handle switching logic
  • Cleanup procedures for both Windows and Unix systems

Technical Details

Testing tools and configuration:
  • Selenium WebDriver with Chrome
  • Python HTTP server for serving test content
  • ChromeDriver with custom options
  • Dynamic port allocation
  • Template-based test file generation
  • Cross-platform process management

Best Practices Demonstrated

The test demonstrates robust testing practices for WebView applications.

Key quality aspects include:
  • Proper resource cleanup
  • Cross-platform compatibility handling
  • Explicit wait patterns for reliability
  • Separate test server implementation
  • Dynamic test content generation
  • Error handling and process termination

nwjs/nwJs

test/sanity/issue4877-inject-webview/test.py

            
import time
import os
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

chrome_options = Options()
chrome_options.add_argument("nwapp=" + os.path.dirname(os.path.abspath(__file__)))
chrome_options.add_experimental_option("windowTypes", ["webview"])

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

port_n = utils.free_port()
port = str(port_n)
server = subprocess.Popen(['python', '../http-server-node.py', port])

time.sleep(1)
tpl = open('index.tpl', 'r')
content = tpl.read().replace('{port}', port)
tpl.close()

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

if not wait_net_service("127.0.0.1", port_n, 30):
    import platform
    if platform.system() == 'Windows':
        subprocess.call(['taskkill', '/F', '/T', '/PID', str(server.pid)])
    else:
        server.terminate()
    raise Exception('Timeout when waiting for http server')

driver = webdriver.Chrome(executable_path=os.environ['CHROMEDRIVER'], chrome_options=chrome_options, service_log_path="log", service_args=["--verbose"])
driver.implicitly_wait(5)
try:
    wait_window_handles(driver, 2)
    print(driver.current_url)
    result = wait_for_element_id(driver, 'inject_start')
    print('inject_js_start: %s' % result)
    assert('success' in result)
    elems = driver.find_elements_by_tag_name('h1')
    assert(len(elems) == 1)
    driver.switch_to.window(driver.window_handles[1])
    result = wait_for_element_id(driver, 'inject_start')
    print('inject_js_start in iframe: %s' % result)
    assert('success' in result)
    elems = driver.find_elements_by_tag_name('h1')
    assert(len(elems) == 1)
finally:
    driver.quit()
    import platform
    if platform.system() == 'Windows':
        subprocess.call(['taskkill', '/F', '/T', '/PID', str(server.pid)])
    else:
        server.terminate()