Back to Repositories

Testing Window Opening and Size Management in NW.js

This test suite validates window handling behavior in NW.js, focusing on window size management and event handling for newly opened windows. It uses Selenium WebDriver to automate browser interactions and verify window dimensions across different opening methods.

Test Coverage Overview

The test suite provides comprehensive coverage of window opening mechanisms and size verification in NW.js.

Key areas tested include:
  • Window creation via window.open()
  • Window creation through target=’_blank’ links
  • Custom window size specifications
  • New window policy event handling

Implementation Analysis

The implementation uses a Selenium WebDriver approach with Python to automate window interaction testing.

Notable patterns include:
  • Modular test helper function click_and_assert()
  • Chrome WebDriver configuration with NW.js specifics
  • Window handle management and switching
  • Dynamic size verification with tolerance

Technical Details

Testing infrastructure includes:
  • Selenium WebDriver with Chrome options
  • Python test framework
  • Custom NW.js utility functions
  • ChromeDriver with verbose logging
  • Window handle management utilities

Best Practices Demonstrated

The test implementation showcases several testing best practices.

Notable examples include:
  • Proper test cleanup in finally block
  • Explicit waits for window handles
  • Flexible size assertions with tolerance
  • Modular helper functions
  • Clear test case separation

nwjs/nwJs

test/sanity/issue4221-win-open-manifest/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

def click_and_assert(driver, id, exp_w, exp_h):
    driver.find_element_by_id(id).click()
    wait_window_handles(driver, 2)
    print('switch to opened window')
    driver.switch_to.window(driver.window_handles[-1])
    driver.find_element_by_id('getwinsize').click()
    result = driver.find_element_by_id('result').get_attribute('innerHTML')
    print('window size: %s' % result)
    expect = str(exp_w) + ',' + str(exp_h)
    expect2 = str(exp_w + 1) + ',' + str(exp_h + 1)
    assert (expect in result or expect2 in result)
    driver.close()
    wait_window_handles(driver, 1)
    driver.switch_to.window(driver.window_handles[0])

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, service_log_path="log", service_args=["--verbose"])
try:
    print(driver.current_url)
    driver.implicitly_wait(10)
    print('open new window with `window.open()`')
    click_and_assert(driver, 'winopen', 400, 300)
    print('open new window by `window.open()` with width and height')
    print('newly opended window should have size of 320,350')
    click_and_assert(driver, 'winopenwithsize', 320, 350)
    print('open new window with <a target="_blank">')
    click_and_assert(driver, 'linkopen', 400, 300)
    print('bind new-win-policy and newly opened window should have size of 388,300')
    driver.find_element_by_id('bindnewwinpolicy').click()
    print('open new window with `window.open()` after new-win-policy')
    click_and_assert(driver, 'winopen', 388, 300)
    print('open new window with <a target="_blank"> after new-win-policy')
    click_and_assert(driver, 'linkopen', 388, 300)
finally:
    driver.quit()