Back to Repositories

Testing Jailed DevTools Elements Interaction in NW.js

This test suite validates the functionality of jailed elements in NW.js using Selenium WebDriver for automated browser testing. It specifically focuses on verifying the proper rendering and accessibility of elements within a sandboxed environment.

Test Coverage Overview

The test ensures proper handling of jailed DevTools elements in NW.js applications.

Key areas covered include:
  • DevTools window initialization and handling
  • Element selection within sandboxed contexts
  • Shadow DOM traversal and content verification
  • Window handle management

Implementation Analysis

The test implementation uses Selenium WebDriver with Chrome options to automate browser interactions. It follows a sequential workflow of opening DevTools, navigating to the Elements panel, and verifying content within shadow DOM.

Notable patterns include:
  • Chrome WebDriver configuration with NW.js app path
  • Explicit wait mechanisms for UI synchronization
  • JavaScript execution for shadow DOM access
  • Window handle management for DevTools interaction

Technical Details

Testing tools and configuration:
  • Selenium WebDriver with Chrome driver
  • Python test framework
  • Custom NW.js utility functions
  • Chrome Options for app initialization
  • Environment variable configuration for ChromeDriver path

Best Practices Demonstrated

The test exemplifies robust automation practices for complex UI testing scenarios.

Notable practices include:
  • Proper test cleanup with driver quit in finally block
  • Explicit waits for UI synchronization
  • Modular utility functions for common operations
  • Clear step logging for debugging
  • Shadow DOM traversal techniques

nwjs/nwJs

test/sanity/issue3780-jailed-elements/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 Elements panel')
    devtools_click_tab(driver, 'elements')
    print('find h1')
    h1 = driver.execute_script('return document.getElementById("elements-content").firstChild.shadowRoot.querySelectorAll(".webkit-html-text-node")[0]').get_attribute('textContent')
    print(h1)
    assert (h1 == 'child')
finally:
    driver.quit()