Back to Repositories

Validating Chrome Socket Permissions and Connectivity in NW.js

This test suite validates Chrome socket permissions and connectivity in NW.js applications using Selenium WebDriver. It ensures proper socket connections can be established with the correct permissions in a mixed-context environment.

Test Coverage Overview

The test suite covers Chrome socket permission validation in NW.js applications, focusing on mixed-context environments.

Key areas tested include:
  • Socket connection establishment
  • Permission handling in mixed-context mode
  • HTTP server interaction
  • Dynamic port allocation

Implementation Analysis

The implementation uses Selenium WebDriver to automate browser interactions and validate socket connectivity. It employs a Python-based approach with dynamic template generation and port allocation.

Key patterns include:
  • Dynamic HTML template substitution
  • Automated Chrome driver configuration
  • Asynchronous server management
  • Explicit wait conditions for UI elements

Technical Details

Testing tools and configuration:
  • Selenium WebDriver for browser automation
  • Python HTTP server for socket testing
  • Chrome Options for NW.js app configuration
  • Dynamic port allocation using Selenium utils
  • Template-based HTML generation
  • ChromeDriver with verbose logging

Best Practices Demonstrated

The test implements several testing best practices for robust socket permission validation.

Notable practices include:
  • Proper resource cleanup in finally block
  • Dynamic port allocation to prevent conflicts
  • Explicit wait conditions for reliability
  • Separation of template and test logic
  • Detailed logging configuration

nwjs/nwJs

test/sanity/issue4143-chrome-sockets-permission/test.py

            
import time
import os
import subprocess

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_argument("mixed-context")

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

port = str(utils.free_port())
server = subprocess.Popen(['python3', 'http-server.py', port])

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

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

driver = webdriver.Chrome(executable_path=os.environ['CHROMEDRIVER'], chrome_options=chrome_options, service_log_path="log", service_args=["--verbose"])
try:
    print(driver.current_url)
    driver.implicitly_wait(10)
    driver.find_element_by_id('socket-connect').click()
    result = driver.find_element_by_id('result').get_attribute('innerHTML')
    print(result)
    assert("success" in result)
finally:
    server.terminate()
    driver.quit()