Back to Repositories

Testing Chrome DevTools Extension Integration in NW.js

This test suite validates the Chrome DevTools Extensions functionality within NW.js webviews using Selenium WebDriver. It specifically tests the integration of custom DevTools tabs and window handling in a controlled testing environment.

Test Coverage Overview

The test suite provides comprehensive coverage of DevTools extension integration in NW.js webviews.

Key areas tested include:
  • Window handle management and switching
  • DevTools URL verification
  • Custom tab presence and selection
  • Timeout handling and assertions

Implementation Analysis

The testing approach utilizes Selenium WebDriver with Chrome options to automate webview interactions. The implementation employs a polling mechanism to verify DevTools window presence and custom tab accessibility.

Notable patterns include:
  • Chrome Options configuration for webview testing
  • Window handle iteration and validation
  • JavaScript execution for DevTools UI interaction
  • Explicit timeout and retry logic

Technical Details

Testing tools and configuration:
  • Selenium WebDriver with Chrome driver
  • Custom Chrome Options for NW.js app testing
  • Page load strategy configuration
  • Python URL parsing utilities
  • Environment-specific ChromeDriver path
  • Verbose logging setup

Best Practices Demonstrated

The test implementation showcases robust testing practices for complex UI automation scenarios.

Quality aspects include:
  • Proper resource cleanup with try-finally blocks
  • Flexible timeout handling
  • Explicit wait conditions
  • Clear assertion points
  • Modular path handling

nwjs/nwJs

test/sanity/webview-cdt-ext/test.py

            
import time
import os
import urllib.parse, urllib.request, urllib.parse, urllib.error
import sys
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from nw_util import *

def path2url(path):
        return urllib.parse.urljoin(
                  'file:', urllib.request.pathname2url(path))

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

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

chrome_options = Options()
chrome_options.add_argument("nwapp=" + testdir)
chrome_options.add_experimental_option("windowTypes", ["webview"])

capabilities = {"pageLoadStrategy": "none"}

driver = webdriver.Chrome(executable_path=os.environ['CHROMEDRIVER'], chrome_options=chrome_options, desired_capabilities = capabilities, service_log_path="log", service_args=["--verbose"])
driver.implicitly_wait(5)
try:
    print(driver.current_url)
    #driver.find_element_by_id('testbtn').click()
    timeout = 10
    found = False
    while timeout > 0 and not found:
        for handle in driver.window_handles:
            driver.switch_to.window(handle)
            if driver.current_url.startswith('devtools://'):
                found = True
                break
        timeout = timeout - 1
        time.sleep(1)
    assert(found)

    timeout = 3
    found = False
    title = ''
    while timeout > 0 and not found:
        try:
            driver.execute_script('UI.inspectorView.tabbedPane.selectTab(UI.inspectorView.tabbedPane.tabs[10].id)')
            title = driver.execute_script('return UI.inspectorView.tabbedPane.tabs[10].title')
        except:
            pass
        print("title:", title)
        found = ('custom' in title)
        time.sleep(1)
        timeout = timeout - 1
    print("title:", title)
    assert('custom' in title)
finally:
    driver.quit()