Back to Repositories

Testing CORS Implementation and Remote Access Validation in NW.js

This test suite implements CORS (Cross-Origin Resource Sharing) validation for NW.js applications using Selenium WebDriver and Python. It verifies proper remote access permissions and XHR requests across different origins in a webview context.

Test Coverage Overview

The test suite provides comprehensive coverage of CORS functionality in NW.js applications:

  • Remote access validation through webview navigation
  • XHR request handling across origins
  • WebDriver session management and cleanup
  • HTTP server interaction for CORS testing

Implementation Analysis

The testing approach utilizes Selenium WebDriver with Chrome options specifically configured for NW.js webview testing. The implementation leverages Python’s subprocess management for controlling the test HTTP server and handles platform-specific cleanup processes.

Key patterns include dynamic port detection, implicit wait mechanisms, and element content verification.

Technical Details

  • Selenium WebDriver with Chrome options
  • Python subprocess management
  • Custom HTTP server implementation
  • Platform-specific process termination
  • Element ID content validation utilities
  • Implicit wait timeouts

Best Practices Demonstrated

The test implementation showcases several testing best practices:

  • Proper resource cleanup and process termination
  • Cross-platform compatibility handling
  • Robust wait mechanisms for async operations
  • Structured error handling and cleanup in finally blocks
  • Modular test server setup

nwjs/nwJs

test/sanity/issue7182-cors/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)

try:
    os.remove('port.txt')
except:
    pass
server = subprocess.Popen(['python', '../http-server-node.py'])

while not os.path.exists('port.txt') :
    time.sleep(1)

driver = webdriver.Chrome(executable_path=os.environ['CHROMEDRIVER'], chrome_options=chrome_options, service_log_path="log", service_args=["--verbose"])
driver.implicitly_wait(5)
try:
    print(driver.current_url)
    result = wait_for_element_id_content(driver, 'remote_access', 'yes')
    print('remote_access: %s' % result)
    elems = driver.find_element_by_tag_name('a')
    elems.click()
    print(driver.current_url)
    result = wait_for_element_id_content(driver, 'remote_access', 'yes')
    print('remote_access: %s' % result)
    result = wait_for_element_id_content(driver, 'remote_xhr', 'yes')
    print('remote_access with xhr: %s' % result)
finally:
    import platform
    if platform.system() == 'Windows':
        subprocess.call(['taskkill', '/F', '/T', '/PID', str(server.pid)])
    else:
        server.terminate()
    driver.quit()