Back to Repositories

Testing Remote Window Management and DevTools Integration in NW.js

This test suite validates the remote window opening functionality and DevTools integration in NW.js applications. It combines Selenium WebDriver with Python to automate browser interactions and verify window handling behaviors.

Test Coverage Overview

The test suite provides comprehensive coverage of window management and DevTools integration functionality.

  • Window opening with chrome.developerPrivate API
  • Remote window creation and handling
  • Window object type verification
  • Asynchronous operation validation

Implementation Analysis

The test implements a robust approach using Selenium WebDriver for browser automation and Python’s subprocess management. It employs a polling mechanism to handle asynchronous operations and verify window creation results.

  • Custom Chrome options configuration
  • Dynamic port allocation for test server
  • Window switching and element verification
  • Retry logic for reliability

Technical Details

  • Selenium WebDriver with Chrome
  • Python HTTP server for serving test content
  • Chrome DevTools Protocol integration
  • Dynamic HTML content generation
  • Custom page load strategy configuration
  • Environment-aware ChromeDriver setup

Best Practices Demonstrated

The test demonstrates several testing best practices for browser automation and window management scenarios.

  • Resource cleanup and proper teardown
  • Explicit waits and retry mechanisms
  • Isolated test environment setup
  • Dynamic port allocation to prevent conflicts
  • Error handling and exception management

nwjs/nwJs

test/sanity/issue4788-openremote-cdtopen/test.py

            
import time
import os
import subprocess
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
from selenium.common.exceptions import NoSuchElementException

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

capabilities = {"pageLoadStrategy": "none"}

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

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

html = open('index.html', 'w')
html.write('''
<html>
<body>
<script>
chrome.developerPrivate.openDevTools({renderViewId: -1, renderProcessId: -1, extensionId: chrome.runtime.id});
window.name = "main";
setTimeout(function() { 
  nw.Window.open('http://localhost:%s/remote.html', function(win) {
    document.write('<h1 id="res">returned window is ' + typeof win + '</h1>');
  });
}, 2000);
</script>
</body>
</html>
''' % (port))
    
html.close()

run_counter = 6

try:
    while run_counter > 0 :
        run_counter = run_counter - 1
        driver = webdriver.Chrome(executable_path=os.environ['CHROMEDRIVER'], chrome_options=chrome_options, desired_capabilities = capabilities)
        time.sleep(1)
        try:
            driver.switch_to.window("main")
            print(driver.current_url)
            result = ''
            wait_counter = 10
            while wait_counter > 0 :
                try:
                    wait_counter = wait_counter - 1
                    time.sleep(1)
                    result = driver.find_element_by_id('res').get_attribute('innerHTML')
                    break
                except NoSuchElementException:
                    pass
            print(result)
            assert("object" in result)
        finally:
            driver.quit()

finally:
    server.terminate()