Back to Repositories

Testing Window Navigation State Preservation in NW.js

This test suite validates window navigation behavior in NW.js applications using Selenium WebDriver. It specifically tests the preservation of window state and event handling across navigation actions.

Test Coverage Overview

The test ensures proper window handling and navigation in NW.js applications.

Key areas covered include:
  • Window state preservation during navigation
  • Event handler persistence across page loads
  • Button click event handling
  • Navigation through anchor tags

Implementation Analysis

The test utilizes Selenium WebDriver with Chrome options configured for NW.js testing.

Implementation features:
  • Chrome WebDriver integration with custom options
  • Implicit wait handling for element loading
  • DOM element interaction validation
  • Assertion-based success verification

Technical Details

Testing tools and configuration:
  • Selenium WebDriver for browser automation
  • Chrome Options for NW.js app configuration
  • Custom utility functions from nw_util
  • Environment-based ChromeDriver path configuration
  • Try-finally block for proper driver cleanup

Best Practices Demonstrated

The test exemplifies robust automation practices with proper resource management and error handling.

Notable practices:
  • Proper driver initialization and cleanup
  • Explicit element waiting strategies
  • Clear assertion conditions
  • Modular test structure with utility imports
  • Environment-aware configuration

nwjs/nwJs

test/sanity/issue4157-nav-win-lost/test.py

            
import time
import os
import sys

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

sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from nw_util import *

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(5)
try:
    print(driver.current_url)
    driver.find_element_by_tag_name('button').click()
    result = driver.find_element_by_id('result').get_attribute('innerHTML')
    print(result)
    assert('success' in result)
    driver.find_element_by_tag_name('a').click()
    wait_for_element_tag(driver, 'button')
    driver.find_element_by_tag_name('button').click()
    result = driver.find_element_by_id('result').get_attribute('innerHTML')
    print(result)
    assert('success' in result)
finally:
    driver.quit()