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
Implementation Analysis
Technical Details
Best Practices Demonstrated
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()