Back to Repositories

Testing Jailed DevTools Window Management in NW.js

This test suite validates the functionality of jailed DevTools in NW.js, focusing on window handling and console interactions. It ensures proper isolation and access control when working with DevTools in a sandboxed environment.

Test Coverage Overview

The test suite provides comprehensive coverage of jailed DevTools functionality in NW.js applications. It verifies window handle management, DevTools console access, and pathname validation.

  • Tests DevTools window opening and handling
  • Validates console command execution
  • Verifies pathname access in jailed context
  • Ensures proper isolation of DevTools environment

Implementation Analysis

The implementation utilizes Selenium WebDriver for automated browser interaction and control. The test employs a systematic approach to validate DevTools functionality:

  • Chrome WebDriver configuration with custom NW.js options
  • Sequential window handle management
  • Console interaction automation
  • Asynchronous result verification with timeout handling

Technical Details

  • Selenium WebDriver with Chrome driver integration
  • Custom Chrome options for NW.js app configuration
  • Python test framework with explicit wait conditions
  • CSS selector-based element location
  • Exception handling for element discovery
  • Timeout mechanism for async operations

Best Practices Demonstrated

The test implementation showcases several testing best practices for browser automation and DevTools interaction:

  • Proper resource cleanup in finally block
  • Explicit waits for UI elements
  • Robust error handling with timeouts
  • Modular test utility functions
  • Clear logging of test progress

nwjs/nwJs

test/sanity/issue3780-jailed/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

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

driver = webdriver.Chrome(executable_path=os.environ['CHROMEDRIVER'], chrome_options=chrome_options)
driver.implicitly_wait(2)
try:
    print(driver.current_url)
    time.sleep(1) # wait for window open
    print('click button to show jailed devtools')
    driver.find_element_by_id('showdevtools').click()
    print('wait for devtools open')
    wait_window_handles(driver, 2)
    print('switch to devtools')
    switch_to_devtools(driver, devtools_window=driver.window_handles[-1])
    print('click Console panel')
    devtools_click_tab(driver, 'console')
    wait_for_element_id(driver, 'console-prompt')
    print('send_keys "location.pathname<enter>"')
    devtools_type_in_console(driver, 'location.')
    time.sleep(1)
    devtools_type_in_console(driver, 'pathname')
    devtools_type_in_console(driver, '\n')
    timeout = 10
    while timeout > 0 :
        try:
            pathname = driver.find_element_by_css_selector('.console-user-command-result .console-message-text .object-value-string').get_attribute('textContent')
            print(pathname)
            assert ('/child.html' in pathname)
            break
        except selenium.common.exceptions.NoSuchElementException:
            pass
        time.sleep(1)
        timeout = timeout - 1
        if timeout <= 0:
            raise Exception('Timeout when waiting for result')
finally:
    driver.quit()