Back to Repositories

Testing Multiple WebSocket Connection Handling in NW.js

This test suite validates multiple WebSocket connections in NW.js on Linux platforms, ensuring reliable concurrent socket handling. The test verifies the ability to establish and maintain more than two simultaneous WebSocket connections, which was a critical issue addressed in the framework.

Test Coverage Overview

The test suite provides comprehensive coverage of WebSocket connection handling in Linux environments.

Key areas tested include:
  • Multiple concurrent WebSocket connections (up to 4 sockets)
  • Connection state verification for each socket
  • Platform-specific behavior on Linux systems
  • Resource cleanup and connection termination

Implementation Analysis

The implementation utilizes Selenium WebDriver for automated browser testing, with specific focus on Chrome integration.

Notable patterns include:
  • Platform-specific test execution control
  • Custom Chrome options configuration
  • Systematic socket connection verification
  • Proper resource cleanup through driver quit operations

Technical Details

Testing infrastructure includes:
  • Selenium WebDriver with Chrome options
  • Custom nw_util helper functions
  • Platform detection via Python’s platform module
  • Environment-based ChromeDriver configuration
  • Implicit wait mechanisms for reliable testing

Best Practices Demonstrated

The test exemplifies several testing best practices for WebSocket functionality:

  • Proper test isolation and cleanup
  • Platform-specific test handling
  • Explicit assertion checks for each socket
  • Defensive programming with try-finally blocks
  • Clear test case organization and progression

nwjs/nwJs

test/sanity/issue6231-linux-websocket-more-than-2-connections/test.py

            
import time
import os
import shutil
import platform
import sys
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from nw_util import *

if platform.system() != 'Linux':
    print('Skipped for non Linux platform')
    sys.exit(0)

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

chrome_options = Options()
testdir = os.path.dirname(os.path.abspath(__file__))
chrome_options.add_argument('nwapp=' + testdir)

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

    result1 = wait_for_element_id(driver, 'socket1')
    print(result1)
    assert('Socket 1 open' in result1)

    result2 = wait_for_element_id(driver, 'socket2')
    print(result2)
    assert('Socket 2 open' in result2)

    result3 = wait_for_element_id(driver, 'socket3')
    print(result3)
    assert('Socket 3 open' in result3)

    result4 = wait_for_element_id(driver, 'socket4')
    print(result4)
    assert('Socket 4 open' in result4)
finally:
    driver.quit()