Back to Repositories

Testing Window Creation Policies and Event Handling in NW.js

This test suite validates window creation policies and event handling in NW.js applications using Selenium WebDriver. It focuses on testing new window creation, size configurations, and keyboard modifier interactions across different platforms.

Test Coverage Overview

The test suite provides comprehensive coverage of window management functionality in NW.js applications. It verifies:

  • Basic new window creation and event sequencing
  • Window creation with specific size parameters
  • Modifier key interactions (Shift/Control) for window opening
  • Cross-platform compatibility handling
  • Window handle management and switching

Implementation Analysis

The implementation uses Selenium WebDriver with Python to automate browser interactions and validate window behaviors. The testing approach employs action chains for keyboard events and explicit window handle management for multi-window scenarios.

Key patterns include wait mechanisms for window creation, DOM element verification, and platform-specific control flow.

Technical Details

Testing tools and configuration:

  • Selenium WebDriver with Chrome Options
  • Custom utility functions for window handling
  • Platform-specific modifier key implementations
  • DOM element selection and attribute verification
  • Environment-based ChromeDriver configuration

Best Practices Demonstrated

The test implementation showcases several testing best practices:

  • Proper test cleanup with try/finally blocks
  • Explicit wait conditions for async operations
  • Cross-platform compatibility handling
  • Comprehensive assertion coverage
  • Structured window handle management

nwjs/nwJs

test/sanity/new-win-policy/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
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys

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)
try:
    print(driver.current_url)
    link = driver.find_element_by_link_text('new window')
    print(link.text)
    link.click()
    #raw_input("Press Enter to continue...")
    wait_window_handles(driver, 2)
    print(driver.window_handles)
    driver.switch_to.window(driver.window_handles[-1])
    output = driver.find_element_by_id('output')
    assert(output.get_attribute('value') == '["inject-js-start","body-start","inject-js-end","onload-dom"]')
    output2 = driver.find_element_by_id('output2').get_attribute('innerHTML')
    assert("Success" in output2)
    assert("432, 345" in output2)
    driver.close()
    
    driver.switch_to.window(driver.window_handles[0])
    link = driver.find_element_by_link_text('new window with size')
    print(link.text)
    link.click()
    wait_window_handles(driver, 2)
    driver.switch_to.window(driver.window_handles[-1])
    output = driver.find_element_by_id('output').get_attribute('innerHTML')
    assert("321, 123" in output)
    driver.close()

    driver.switch_to.window(driver.window_handles[0])
    link = driver.find_element_by_link_text('new window')
    if platform.system() == 'Darwin':
        ActionChains(driver).key_down(Keys.SHIFT).click(link).key_up(Keys.SHIFT).perform()
    else:
        ActionChains(driver).key_down(Keys.CONTROL).click(link).key_up(Keys.CONTROL).perform()
    wait_window_handles(driver, 2)
    driver.switch_to.window(driver.window_handles[-1])
    output = driver.find_element_by_id('output')
    assert(output.get_attribute('value') == '["inject-js-start","body-start","inject-js-end","onload-dom"]')
    output2 = driver.find_element_by_id('output2').get_attribute('innerHTML')
    assert("Success" in output2)
   
finally:
    driver.quit()