Back to Repositories

Testing Proxy URL Resolution and Configuration in NW.js

This test suite validates the getProxyForURL functionality in NW.js applications using Selenium WebDriver integration. It verifies proxy configuration handling and URL resolution through automated browser testing.

Test Coverage Overview

The test suite provides comprehensive coverage of the getProxyForURL API functionality in NW.js.

Key areas tested include:
  • Dynamic proxy configuration through PAC files
  • URL resolution and proxy determination
  • Multiple window handle management
  • Element interaction and state verification

Implementation Analysis

The testing approach utilizes Selenium WebDriver with Chrome options for NW.js app testing. The implementation follows a sequential verification pattern with explicit waits and assertions.

Key patterns include:
  • Chrome WebDriver initialization with custom NW.js configurations
  • Dynamic port allocation for test isolation
  • Package.json generation for test environment setup
  • Multiple result verification points

Technical Details

Testing tools and configuration:
  • Selenium WebDriver for browser automation
  • Chrome Options for NW.js app configuration
  • Python test runner with sys/os utilities
  • Custom nw_util helper functions
  • Dynamic port allocation using Selenium utils
  • JSON-based app configuration

Best Practices Demonstrated

The test implementation showcases several testing best practices for NW.js applications.

Notable practices include:
  • Proper test cleanup and driver management
  • Explicit wait patterns for UI synchronization
  • Dynamic test environment configuration
  • Isolated test execution through unique ports
  • Clear assertion patterns for result verification

nwjs/nwJs

test/sanity/app-getproxyforurl/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

test_dir = os.path.dirname(os.path.abspath(__file__))
chrome_options = Options()
chrome_options.add_argument("nwapp=" + test_dir)

port = str(utils.free_port())

pkgjson = '''
{
  "name": "app-getproxyforurl",
  "main": "index.html",
  "bg-script": "bg.js",
  "port": "%s"
}
''' % port

with open(os.path.join(test_dir, 'package.json'), 'w') as bg:
  bg.write(pkgjson)

driver = webdriver.Chrome(executable_path=os.environ['CHROMEDRIVER'], chrome_options=chrome_options)
driver.implicitly_wait(2)
try:
    print(driver.current_url)

    result = driver.find_element_by_id('result')
    print(result.get_attribute('innerHTML'))
    assert("success" in result.get_attribute('innerHTML'))

    wait_window_handles(driver, 1)
    switch_to_app(driver)
    driver.find_element_by_id('pac').click()
    wait_window_handles(driver, 1)
    result2 = driver.find_element_by_id('result2')
    assert("success" in result2.get_attribute('innerHTML'))
    wait_window_handles(driver, 1)
    result3 = driver.find_element_by_id('result3')
    assert("success" in result3.get_attribute('innerHTML'))
finally:
    driver.quit()